Run
Run

Reputation: 57286

Shiny - How to separate the Rendering functions into different files?

How can I separate the Rendering functions into different files?

For instance,

I have this in my server.R,

shinyServer(function(input, output, session) {

   output$text <- renderUI({...})

   output$annotations <- renderDataTable({...})

   output$plot <- renderPlot({...})

}))

Can I put output$text, output$annotations, and output$plot into separate r files and then import them in?

My attempt,

source('source/server/getRenderUI.R', local = TRUE)
output$text <- getRenderUI()

Result,

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Upvotes: 2

Views: 1584

Answers (1)

ysfseu
ysfseu

Reputation: 676

I don't think you need to call getRenderUI(), you could try this

In getRenderUI.R

  output$text <- renderUI({...})

In server.R 

  shinyServer(function(input, output, session) {

    source('...../getRenderUI.R', local = TRUE)$value

 }))

For me , this works well.

Note: Pay attention not to use the same name for output$xxx, because all variables is in the same scope

Upvotes: 2

Related Questions