解决闪亮的多个输入

Cen*_* 15 3 regex grep r shiny

我正在构建一个相对复杂的应用程序,我的动态输入数量标题为:d1,d2 .. dn.有一次,我想尝试同时处理多个输入:

input[[grep(pattern="d+[[:digit:]]",input)]]
Run Code Online (Sandbox Code Playgroud)

这当然导致错误:

Must use single string to index into reactivevalues
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有人知道这样做的优雅方式?

Vic*_*orp 7

您可以在输入上使用名称:

grep(pattern = "d+[[:digit:]]", x = names(input), value = TRUE)
Run Code Online (Sandbox Code Playgroud)

一个工作的例子:

library("shiny")
ui <- fluidPage(
  fluidRow(
    column(
      width = 6,
      lapply(
        X = 1:6,
        FUN = function(i) {
          sliderInput(inputId = paste0("d", i), label = i, min = 0, max = 10, value = i)
        }
      )
    ),
    column(
      width = 6,
      verbatimTextOutput(outputId = "test")
    )
  )
)
server <- function(input, output){
  output$test <- renderPrint({
    sapply(grep(pattern = "d+[[:digit:]]", x = names(input), value = TRUE), function(x) input[[x]])
  })
}
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用:`observeEvent(lapply(grep(pattern =“ d + [[:digit:]]]”,x =名称(输入),值= TRUE),函数(x)输入[[x]]),{} )` (2认同)