0

I'm building a shiny dashboard and am making many plots from the same dataset. Instead of making separate server output$name objects for each series that I select, I would like to have one server object with a variable and then pass a variable to each ui object that indicates which series I want it to display.

ui <- dashboardPage(
    fluidRow(

      column(width = 6,
        box(title = "Net Income", width=NULL,
          dygraphOutput(outputId = "net_income", height = "300px"))
             ),

       column(width = 6,
         box(title = "Accounts Receivable", width=NULL,
            dygraphOutput(outputId = "accounts", height = "300px"))
              )  
) # end ui

Notice the multiple server objects that I'm creating that chooses the series to display from a single object, fcast_data. I'm displaying 10 charts from this dataset, so would have to produce 10 separate server render objects. I would like to create 1 server object with a variable 'series_name' that I could pass from the ui objects to indicate which series I want to display.

server <- function(input, output) { 
  output$net_income <- renderDygraph({ 
    dygraph(fcast_data %>% select(net_income), group = "my_dash")})

  output$accounts_receivable <- renderDygraph({ 
     dygraph(fcast_data %>% select(accounts_receivable), group = "my_dash")})

} # end server

Is there a way that I can make one server object (renderDygraph) that just takes parameters from my ui object (dygraphOutput) to choose different series to display? I know how to do this using selectInputs, but can't figure out how to hard-code variables in the ui object to pass to server.

5
  • Pass the column names in your select functions to a selectInput() function in your UI. Then pass the string to a single plot - you may need to convert to select_() given the quotations from the input string. Commented Aug 2, 2018 at 20:30
  • 1
    this might be of interest: shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html Commented Aug 2, 2018 at 20:32
  • @BigDataScientist Yes, this very much interests me :) I think this is likely exactly what I need. I'll play with this. Thanks! Commented Aug 2, 2018 at 20:38
  • @RyanMorton does the selectInput() object always display in the ui, though? I'll try this and see whether I can use selectInput() without display any kind of ui select element, as I don't want any user input available. Commented Aug 2, 2018 at 20:42
  • @BigDataScientist OMG, yes. This is totally what I needed. Many thanks!!!! Commented Aug 2, 2018 at 21:14

1 Answer 1

1

For sake of completeness: like I mentioned in the comment, an example can be found here: https://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html.

Without being able to test it with the given code, it would be something along the lines of this:

types <- c("net_income", "accounts_receivable")

# server
lapply(1:2, function(nr) {
  output[[paste0("dy_g", nr)]] <- renderDygraph({ 
    dygraph(fcast_data %>% select(get(types[nr])), group = "my_dash")
  })
}

# ui
lapply(1:2, function(nr) {
  column(width = 6,
         box(title = types[nr], width=NULL,
             dygraphOutput(outputId = paste0("dy_g", nr), height = "300px"))
  )
})
Sign up to request clarification or add additional context in comments.

1 Comment

Nailed it. I implemented this yesterday. Solved all my problems. Thanks, again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.