Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of all output elements in R Shiny

How the get a list of reactive output elements?

what works without problems is: observe(print(reactiveValuesToList(input)) )

what doesn't work for reasons i don't understand is: observe( print(reactiveValuesToList(output)) )

For a large complex app, I am writing a smart code that in part A saves all reactivevalues and all inputs as rds files with saveRDS my reactivevalues are build this way; values <- reactivevalues()

Part B loads all these values with the use of readRDS. reactive values$x are reassigned and input$x are updated through for instance updateTextinput(session, inputID, value), which for some weird reason also works for selectInput elements, but that is not relevant for the problem I face.

My app also contains renderUI elements and renderText elements

to update these elements, I need to set the option :

outputOptions(output, x, suspendWhenHidden = FALSE)}

where x is the name of the element. I have however dozens of these elements, and want to apply the setting of this suspendWhenHidden option to a list. However, trying to get the list of outputs doesn't work:

observe( print(reactiveValuesToList(output)) )

How do I get the list of all rendered elements / all output elements?

A demo app to show how it works for input, but doesn't work for output elements

library(shiny) 

rm(list = ls(), envir = globalenv())              ## to prevent cross over from old runs

ui <- dashboardPage(
  dashboardHeader(title = "Dummy App"),
  dashboardSidebar(
    sidebarMenu(id = "tabs",
                menuItem("HOME", tabName = "Home", icon = icon("home")),
                menuItem("Page", tabName = "page1", icon = icon("pie-chart"))
    ) ),
  dashboardBody(
   tabItems(
      tabItem(tabName = 'Home', 
                              br(), 
                              h1("WELCOME TO THE TEST APP", style = 'text-align: center;' )

      ),
      tabItem(tabName = 'page1',
       fluidRow(
         uiOutput("BatchName")
    )))))



server <- function(input, output, session) {

output$BatchName <- renderUI({ textInput(inputId ="BatchName", label = NULL , placeholder = "start") })


observe(print(reactiveValuesToList(input)) )
observe( print(reactiveValuesToList(output)) )

outputOptions(output, "BatchName", suspendWhenHidden = FALSE)  ## without this line updating elements on page 2 and higher doesn't work as they are suspenWhenHidden = True by default
updateTextInput(session, inputId = "BatchName", value = "Updated") 

    }
shinyApp(ui, server)
like image 833
Mark Avatar asked Jan 24 '18 21:01

Mark


People also ask

Is R Shiny difficult?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.

How do you show a message in Shiny?

Simply call shinyalert() with the desired arguments, such as a title and text, and a modal will show up. In order to be able to call shinyalert() in a Shiny app, you must first call useShinyalert() anywhere in the app's UI.

What is input in R Shiny?

2.2. 1 Common structure. All input functions have the same first argument: inputId . This is the identifier used to connect the front end with the back end: if your UI has an input with ID "name" , the server function will access it with input$name .


1 Answers

Looking at the documentation for ?outputOptions, if you don't pass in a name you'll get a list for all the possible output options. Then you can iterate that list to get set all the properties you want.

outs <- outputOptions(output)
lapply(names(outs), function(name) {
  outputOptions(output, name, suspendWhenHidden = FALSE) 
})
like image 55
MrFlick Avatar answered Sep 20 '22 14:09

MrFlick