Search code examples
reval

With get() call a function from an unloaded package without using library


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

Normally I would just use:

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")
 }

would require a quite long list of if else statemens.


Solution

  • Use getExportedValue:

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