【问题标题】:How to use Shiny inputs in a string for reactive function and as a condition for observe event如何在字符串中使用 Shiny 输入作为反应函数并作为观察事件的条件
【发布时间】:2020-03-14 09:41:20
【问题描述】:

我正在尝试在反应元素或观察事件中使用自动生成的 selectInput ID。当我明确写出input$dfSelect1,input$dfSelect2,input$dfSelect3 之类的输入ID 时,它会按我的意愿工作。

由于我事先不知道会有多少个 ID(数据将是用户输入),所以我需要创建与自动化相同的输入 ID 字符串,但它不会将其识别为观察事件中的触发器或反应元件中的输入数据。

这是我的问题的最小可重现示例。如果您注释掉第 1 行 req(input$dfSelect1,input$dfSelect2,input$dfSelect3) 和第 2 行 dfx <- data.frame(carb = c(input$dfSelect1,input$dfSelect2,input$dfSelect3),stringsAsFactors = F) 并从以下行中删除注释,这将是我正在尝试做的情况。

知道如何传递这些值吗?

library(dplyr)
library(DT)

exdata <- head(mtcars, 3) 
exdata$ROWs <- row.names(exdata)

ui <- fluidPage(
  headerPanel("Example"),
  mainPanel(
    uiOutput("selectionUI"),
    uiOutput("tableOutput")
  )
)

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

  ### reqString result <- input$dfSelect1,input$dfSelect2,input$dfSelect3
  reqString <- noquote(paste0(unlist(lapply(1:length(sort(unique(row.names(exdata)))),function(i) {paste0("input$dfSelect",i,"")})),collapse = ","))

  values <- reactiveValues(
    upload_state = NULL
  )

  observe({
    ### 1-USE the line below with reqString instead -doesn't work ##
    req(input$dfSelect1,input$dfSelect2,input$dfSelect3)
    # req(reqString)
    values$upload_state <- 'uploaded'
  })

  output$selectionUI <- renderUI({
    df <- sort(unique(row.names(exdata)))
    wellPanel(
      lapply(1:length(df), function(i) {selectizeInput(paste0("dfSelect",i,""),df[i],choices=c("", unique(exdata$carb)))})
    )
  })

  completeTable <- reactive({
    browser()
    if (is.null(values$upload_state)) {
      return(exdata)
    }else if (values$upload_state == 'uploaded') {
      ### 2-USE the line below with  reqString instead -doesn't work##
      dfx <- data.frame(carb = c(input$dfSelect1,input$dfSelect2,input$dfSelect3),stringsAsFactors = F)
      # dfx <- data.frame(carb = c(reqString),stringsAsFactors = F)
      dfx <- data.frame(carb =as.numeric(unlist(dfx)))
      dataJoin <- exdata %>% left_join(dfx,by=("carb"))
    }
  })

  output$tableOutput <- renderUI({
    DT::dataTableOutput("dataTableServer")
  })

  output$dataTableServer <- DT::renderDataTable({
    DT::datatable(completeTable())
  })

}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用[[ 而不是$ 来索引input

    sapply(1:length(sort(unique(row.names(exdata)))), 
           FUN=function(x) req(input[[paste0("dfSelect", x)]]))
    

    l <- sapply(1:length(sort(unique(row.names(exdata)))), 
                FUN=function(x) input[[paste0("dfSelect", x)]])
    dfx <- data.frame(carb = l,stringsAsFactors = F)
    

    【讨论】:

    • 谢谢,这解决了问题的第二部分。但是如何将其应用于req 函数以触发观察事件?替换此代码req(input$dfSelect1,input$dfSelect2,input$dfSelect3)
    • sapply(1:length(sort(unique(row.names(exdata)))), FUN=function(x) req(input[[paste0("dfSelect", x)]])) 替换req(input$dfSelect1,input$dfSelect2,input$dfSelect3) 应该可以解决问题,因为req(input$dfSelect1,input$dfSelect2,input$dfSelect3) 就像req(input$dfSelect1):req(input$dfSelect2):req(input$dfSelect3)
    猜你喜欢
    • 2017-04-24
    • 2018-11-28
    • 2021-08-16
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多