简体   繁体   中英

R Shiny rendering UI widgets interactively

I am trying to build an App that allows the user to select one or many functions (modules) from a pre-supplied list, and then based on their selection, select values for various arguments to that function.

Basically, I'm trying to reconstruct the renderUI call tree so that when the reactive part of shiny happens, and the server function gets called, if input$abcd.in is not NULL the renderUI function is run with a different argument depending on what the value of input$abcd.in is.

The error seems to be that the input argument to the set_output_automatically function is of length 0, but I have no idea what to do next.

Here is a simplified example

library(shiny)

arg.info <-  list(
  list(choices = c("Yes" = T, "No" = F),
                  prompt = quote(paste("Remove Missing Values for analysis?")),
                  type = "radioButtons"),
  list(choices = c("not" = 0, "some" = 1, "very" = 3),
       prompt = quote(paste("how cool are you?")),
       type = "checkboxGroupInput"))


set_output_automatically <- function(input, arg.info){

  if(input[1] > 3){
    arg.info <- arg.info[[1]]
  } else {
    arg.info <- arg.info[[2]]
  }

  renderUI({

    call(arg.info$type, paste0(input$abcd, ".in"), label = arg.info$prompt,
         choices = arg.info$choices)
  })


}

ui <- fluidPage(

  uiOutput('abcd'),

  textOutput('value'),

  uiOutput('result')


)

server <- function(input, output){

  output$abcd <- renderUI({

    checkboxGroupInput('abcd.in', 'sample',
                       choices = c('wowe'= 1,
                                   'such' = 2,
                                   'choice' = 3,
                                   'very' = 4,
                                   'programe' = 5))

  })

  output$value <- renderPrint({input$abcd.in})

  output$result <- reactive(set_output_automatically(input$abcd.in, arg.info))

}

shinyApp(ui, server)

I replaced call with do.call (having an argument list) and the reactive with a renderUI , this works.

library(shiny)

arg.info <-  list(
  list(choices = c("Yes" = T, "No" = F),
       prompt = quote(paste("Remove Missing Values for analysis?")),
       type = "radioButtons"),
  list(choices = c("not" = 0, "some" = 1, "very" = 3),
       prompt = quote(paste("how cool are you?")),
       type = "checkboxGroupInput"))


set_output_automatically <- function(input, arg.info){
  if(input[1] > 3){
    arg.info <- arg.info[[1]]
  } else {
    arg.info <- arg.info[[2]]
  }
  do.call(arg.info$type, args=list(inputId=paste0("in", input), label = arg.info$prompt,
          choices = arg.info$choices))

}

ui <- fluidPage(
  uiOutput('abcd'),
  textOutput('value'),
  uiOutput('result')
)

server <- function(input, output){

  output$abcd <- renderUI({
    checkboxGroupInput('abcd.in', 'sample',
                       choices = c('wowe'= 1,
                                   'such' = 2,
                                   'choice' = 3,
                                   'very' = 4,
                                   'programe' = 5))
    })

  output$value <- renderPrint({input$abcd.in})

  output$result <- renderUI({
    set_output_automatically(input$abcd.in, arg.info)
  })

}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM