Search code examples
ruser-interfaceshinyrendering

How to render dynamic UIs based on user selection with shiny


Given the set of shiny UIs and their differings arguments (to be read from a rdf, here given as explicit lists) how can the user select a desired type of input (for a data-model with many different inputs, all presetted with defaults) to be changed?

library(shiny)
library(shinyWidgets)
library(DT)
library(purrr)
library(dplyr)
library(data.table)
#-----------------someWidgetsAndArguments-------------------.
inputWidget <- list("selectInput", "sliderInput", "textInput", "numericInput")
inpWidgArgs <- list(list(inputId = "inpUI01", label = "seInp01", choices = "seq(1,20,1)", selected = 10),
                    list(inputId = "inpUI02", label = "slInp02", min= 0, max = 100, value = "c(25,75)" ),
                    list(inputId = "inpUI03", label = "txInp03", value = "enter some text"),
                    list(inputId = "inpUI04", label = "nrInp04", value = 1000000) )
#----------------presetPickerInput---------------------
if (interactive()) {
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        pickerInput(inputId = "pkInp01",
                    label = "Select CF-Model Inputs for change",
                    choices =  inputWidget,
                    selected = inputWidget[1:2],
                    multiple = TRUE,
                    options = list(`actions-box` = TRUE,
                                   `selected-text-format`= "count",
                                   `count-selected-text` = "{0} inputs of {1} selected") ),
       uiOutput("inpUI"),
        ),
      mainPanel(
        dataTableOutput("table01")
        )
      )
    )
  #-----------------generateArguments4invoke_map---------------------------. 
  server <- function(input, output, session) {
    #B: obs <- reactiveValues(
    #A: pckdWdgt <- inputWidget[match(input$pkInp01, inputWidget)],
    #A: wdgtArgs <- inpWidgArgs[match(input$pkInp01, inputWidget)]
    #B: )
    #B: observe({
    #B:   obs$pW01 = inputWidget[match(input$pkInp01, inputWidget)]
    #B:   obs$wA02 = inpWidgArgs[match(input$pkInp01, inputWidget)]
    #B:   })
    #------------------renderAsManyInputUisAsPicked------------
    output$inpUI <- renderUI({
      #A: invoke_map(match.fun(pckdWdgt), wdgtArgs)
      #B: invoke_map(match.fun(obs$pW01), obs$WA02)
      invoke_map(list(selectInput, sliderInput), list(
        list(inputId = "inpUI01", label = "selectInput01", choices = seq(1,20,1), selected = 10),
        list(inputId = "inpUI02", label = "sliderInput02", min= 0, max = 100, value = c(25,75) )
        )
        )
    })
  }
}
#-----------------------------------------------------
shinyApp(ui, server)

With map() or invoke_map() it should be possible to pass the type of function/UIinput and its arguments (compare: https://hadley.shinyapps.io/ms-render-palette-full).

Two approaches (A: and B:) below fail (possible reason: environment/namespace?) Any suggestion highly appreciated.

Many thanks in advance


Solution

  • I cleaned some of your code and created the solution. To start a few minor things: The choices argument in seInp01 shouldn't be between quotations. The same goes for the value argument in slInp01. Lastly there is a trailing comma behind your uiOutput argument in the UI. For the functionality of the code I just put some codes that you already came up with in the right place, you had the right idea.

    The code:

    library(shiny)
    library(shinyWidgets)
    library(DT)
    library(purrr)
    library(dplyr)
    library(data.table)
    #-----------------someWidgetsAndArguments-------------------.
    inputWidget <- list("selectInput", "sliderInput", "textInput", "numericInput")
    inpWidgArgs <- list(list(inputId = "inpUI01", label = "seInp01", choices = seq(1,20,1), selected = 10),
                        list(inputId = "inpUI02", label = "slInp02", min= 0, max = 100, value = c(25,75) ),
                        list(inputId = "inpUI03", label = "txInp03", value = "enter some text"),
                        list(inputId = "inpUI04", label = "nrInp04", value = 1000000) )
    #----------------presetPickerInput---------------------
    if (interactive()) {
      ui <- fluidPage(
        sidebarLayout(
          sidebarPanel(
            pickerInput(inputId = "pkInp01",
                        label = "Select CF-Model Inputs for change",
                        choices =  inputWidget,
                        selected = inputWidget[1:2],
                        multiple = TRUE,
                        options = list(`actions-box` = TRUE,
                                       `selected-text-format`= "count",
                                       `count-selected-text` = "{0} inputs of {1} selected") ),
            uiOutput("inpUI")
          ),
          mainPanel(
            dataTableOutput("table01")
          )
        )
      )
      #-----------------generateArguments4invoke_map---------------------------. 
      server <- function(input, output, session) {
        #------------------renderAsManyInputUisAsPicked------------
        output$inpUI <- renderUI({
          wdgtArgs <- inpWidgArgs[match(input$pkInp01, inputWidget)]
          invoke_map(input$pkInp01, wdgtArgs)
        })
      }
    }
    #-----------------------------------------------------
    shinyApp(ui, server)