1

我有一个带有许多输入的 R Shiny 应用程序,在它运行输出之前,我想避免它显示输出,直到它具有所有必需的输入。但是,有很多输出,而不是全部输入,我想通过他们的 div 标签(输入)使用 req() 调用。

这是一个简单的应用程序:

library(shiny)

ui <- fluidRow(
    column(12,
           div(id = "inputs",
        selectInput(inputId = "reasons",
                    label = "Select Your Reasons",
                    choices = c("Everything", "Your Hair", "Your Eyes", "Your Smile"),
                    multiple = TRUE),
        selectInput(inputId = "verb",
                    label = "Select Your Verb",
                    choices = c("love", "hate"),
                    multiple = TRUE)),
        textOutput("message")
    )
)

server <- function(input, output) {
    
    output$message <- renderText({
        paste("I", input$verb, input$reasons)
    })

}

shinyApp(ui = ui, server = server)

我尝试shiny::req(input$inputs)renderTextandpaste调用之间添加,但是即使我为 2 个下拉菜单选择了值,该代码也没有显示任何内容。

4

1 回答 1

0

正如我所提到的,您可能会发现模块非常适合。但是,就我个人而言,我会选择一个中间反应来要求。

library(shiny)

ui <- fluidRow(
  column(12,
         div(id = "inputs",
             selectInput(inputId = "reasons",
                         label = "Select Your Reasons",
                         choices = c("Everything", "Your Hair", "Your Eyes", "Your Smile"),
                         multiple = TRUE),
             selectInput(inputId = "verb",
                         label = "Select Your Verb",
                         choices = c("love", "hate"),
                         multiple = TRUE)),
         textOutput("message")
  )
)

server <- function(input, output) {
  
  ## returns NULL if any of the listed inputs are null
  inputs <- reactive({
    input_list<- lapply(c('reasons','verb'), function(x) input[[x]])
    if(list(NULL) %in% input_list) NULL else input_list
  })
  
  
  output$message <- renderText({
    req(inputs())
    paste("I", input$verb, input$reasons)
  })
  
}

shinyApp(ui = ui, server = server)
于 2021-06-29T12:56:09.960 回答