使用get()从卸载的包中调用函数,而无需使用库 [英] With get() call a function from an unloaded package without using library

查看:67
本文介绍了使用get()从卸载的包中调用函数,而无需使用库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将函数名称存储在列表中来从已卸载的包中调用函数.

I want to call a function from an unloaded package by having the function name stored in a list.

通常我只会使用:

library(shiny)

pagelist <- list("type" = "p") # object with the function name (will be loaded from .txt file)
get(pagelist$type[1])("Display this text")

但是由于编写程序包时不允许加载库,因此我不得不使用类似的

but since when writing a package you're not allowed to load the library I'd have to use something like

get(shiny::pagelist$type[1])("Display this text")

这不起作用.有没有一种方法可以从列表中存储的函数名称中调用函数,而不必加载库?请注意,应该可以像这样调用许多不同的函数(全部来自同一包),因此只需使用例如

which doesn't work. Is there a way to call the function from the function name stored in the list, without having to load the library? Note that it should be possible to call many different functions like this (all from the same package), so just using e.g.

if (pagelist$type[1] == "p"){
  shiny::p("Display this text")
 }

将需要很长的if else政治家清单.

would require a quite long list of if else statemens.

推荐答案

使用getExportedValue:

getExportedValue("shiny",pagelist$type[1])("Display this text")
#<p>Display this text</p>

这篇关于使用get()从卸载的包中调用函数,而无需使用库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆