Choose a RData dataset before launching rest of Shiny app

Hi All-

I apologize if this is a basic question. I have searched, but I may just not know how to ask it properly. I have designed a fairly complex shinydashboard which begins by loading an RData file as its datasource into global. This powers reactive selection boxes, charts, data tables, etc, which change day by day. What I have in mind now is to generate a series of dated RData files as source files, and then ask the user to pick a data file to load through the UI prior to rendering the rest of the dashboard. It seems like this should be a fairly simple thing to accomplish, but I am at a loss. Do I need to modularize my shiny app, or can this be accomplished in some standard way? Thank you for any help.

Mike

1 Like

Hi @griswomw. From your description, your UI design will be alternated by the data. If yes, I suggest modularise your app to make it clear to code. But if only data change and UI design not change, modules will make the app complicated.

I don't think this has anything to do with modularisation. You can do it fine in a single file app.

You will need to

  • read the file names (you can do this in the global section)
  • offer the file list to the user in the ui along with other default ui elements.
  • respond to the user's file selection in the server by loading the file, e.g. using observeEvent(input$chosen_file, {}), and then update the ui elements, e.g., updateSelectInput

Thank you, I appreciate the feedback from both of you. Simon-I will attempt to implement what you suggested and report back.

Regards,

Mike

Simon-

I've been able to use this to update UI elements like SelectInput, but I've been unable to figure out how to rerender the plotlyOutput with the new data. Any advice on this?

Thank you,

Mike

Hi Mike

I always use proxy for updating plotly (and DT). The main challenge is that plotlyProxyInvoke takes plotly arguments structured in the list structure of the underlying javascript, which was slightly modified in R-plotly. Basically you need to pass all arguments as nested lists. And if you get the list structure wrong you don't get an error, just nothing.

  output$bull_plot <- renderPlotly({
    plot_ly(...)
  })

  bull_plot_proxy <- plotlyProxy("bull_plot", session = session)

  observeEvent(..., {
    plotlyProxyInvoke(
          bull_plot_proxy, ...
    )
  })

Just closing the loop here. After struggling with this for quite some time, I found a really elegant solution online:

Define the following function and reactive values variable:

my_data <- reactiveValues()

updateData <- function(LoadString) {
vars <- load(file = LoadString, envir = .GlobalEnv)
for (var in vars)
my_data[[var]] <- get(var, .GlobalEnv)
}

Use SelectInputs to build the path to the RData file that we want to load, call updateData function to load user selected data (for me, I created a "Load" button for the user to click after all of the relevant SelectInputs had been updated).

Hope this can help someone else in the future.

Regards,

Mike

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.