带有多个输入的闪亮renderUI

nzg*_*ynn 2 r shiny

我的闪亮应用程序具有多个输入,具体取决于所使用的变量数量。下面是一个简化的版本,尽管不起作用。我使用一个用来制作uiOutput的名为Make.UI的函数来使UI根据numericInput进行更新,但是将输入返回服务器已经超出了我的Shiny技能范围!任何建议将不胜感激。

格温

library(shiny)
D = matrix(runif(400), nrow = 20)
colnames(D) = labs = sapply(1:20, function(i) {paste0("col",i)})

# Define UI for application that summarises data
ui <- fluidPage(

  # Application title
  titlePanel("Summaries"),

  # Select columns to get fed into summary 
  tabsetPanel(
    tabPanel("Matching Variables Info",
             sidebarPanel(

               numericInput("NoVars","No. of variables to summarize", 
                            value = 3, min = 2, max = dim(D)[2]),

               uiOutput("VarsInput")
             ),

             # Show summaries of columns choosen above
             mainPanel(
               verbatimTextOutput("dataInfo")
             )
    )
  )
)


# Define the server code
server <- function(input, output){

  Make.UI <- function(NoV){
    C = sapply(1:NoV, function(i){paste0("cols",i)})
    L = sapply(1:NoV, function(i){paste0("label",i)})

    output = tagList()

    for(i in seq_along(1:NoV)){
      output[[i]] = tagList()
      output[[i]][[1]] = selectInput(C[i], "Variable to summarize:", labs)
      output[[i]][[2]] = textInput(L[i], label = "Label for variable:", 
                                   value = "Label for variable Here")
    } ## for loop

    output
  } # closes Make.UI function

  K <- reactive({
    input$NoVars
  })

  output$VarsInput <- renderUI({
    Make.UI(K())
  })

  output$dataInfo <- renderPrint({
    C <- sapply(1:K(), function(i) {input[[paste0("cols",i)]]})
    ## the code in the line above doesn't work

    summary(D[, C()])
  })

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

Big*_*ist 5

就像我在第一条评论中所写的那样,我不确定该Make.UI()功能。如果您真的想将其保留为单独的功能,则应使其具有反应性。或者像下面的代码一样使用它。而且,由于output$dataInfo <- renderPrint({C不是reactive()函数,因此您需要在其中删除括号。

library(shiny)
D = matrix(runif(400), nrow = 20)
colnames(D) = labs = sapply(1:20, function(i) {paste0("col",i)})

# Define UI for application that summarises data
ui <- fluidPage(

  # Application title
  titlePanel("Summaries"),

  # Select columns to get fed into summary 
  tabsetPanel(
    tabPanel("Matching Variables Info",
             sidebarPanel(

               numericInput("NoVars","No. of variables to summarize", 
                            value = 3, min = 2, max = dim(D)[2]),

               uiOutput("VarsInput")
             ),

             # Show summaries of columns choosen above
             mainPanel(
               verbatimTextOutput("dataInfo")
             )
    )
  )
)


# Define the server code
server <- function(input, output){

  K <- reactive({
    input$NoVars
  })

  output$VarsInput <- renderUI({
    NoV = K()
    C = sapply(1:NoV, function(i){paste0("cols",i)})
    L = sapply(1:NoV, function(i){paste0("label",i)})

    output = tagList()

    for(i in seq_along(1:NoV)){
      output[[i]] = tagList()
      output[[i]][[1]] = selectInput(C[i], "Variable to summarize:", labs)
      output[[i]][[2]] = textInput(L[i], label = "Label for variable:", 
                                   value = "Label for variable Here")
    } ## for loop

    output
  })

  output$dataInfo <- renderPrint({
    C <- sapply(1:K(), function(i) {input[[paste0("cols",i)]]})
    ## the code in the line above doesn't work

    summary(D[, C])
  })

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)