简体   繁体   中英

R shiny: Why am I unable to access functions defined in another file from server.R?

How do I define server.R's local functions in different files and access the functions in Server file? Everytime I try defining a reactive in another file, I get the following error:

Error in reactive:plotScatter: object 'input' not found

I don't receive this error when the function is defined and declared in server.R file so I am assuming that it's because of how I am dividing my file.

This is how I am accessing the functions though;

source("filename.R")

I am assuming that I should be able to access all the modules defined in filename.R without doing anything else. I am very new to shiny, any help would be appreciated.

It is difficult to say much about your specific situation. What I can do is showing you how it works on my system. I have a larger app that is divided into several files.

In "app.R" I have two source calls to the files containing the server and the ui function.

# ...
source("getui.R", local = TRUE)
source("getserver.R", local = TRUE)
#RUN
shinyApp(ui = ui, server = server)

In the "getserver.R" file I source additional parts of the server. The sourced files contain all the code including observers and reactive values. Only now I access the content of the source file with source(...)$value . I also set local = TRUE . local determines to which environment the sourced code will be added. In this case you need it inside the environment of your server function.

server <- shinyServer(function(input, output, session) {
  source(file.path("server", "tab_setup_srv.R"), local = TRUE)$value
  source(file.path("server", "tab_about_srv.R"), local = TRUE)$value
  
  # further code
})

I tried to find out more about the $value element. However, there is no mention of it in the manual and no direct hits in Google. All I can tell is that it works this way and it does not when I remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM