【问题标题】:Shiny renderUI with multiple inputs具有多个输入的闪亮 renderUI
【发布时间】:2017-06-29 09:24:06
【问题描述】:

My Shiny App 有多个输入,取决于使用的变量数量。下面是一个简化的版本,虽然不工作。我能够使用我用来制作 uiOutput 的名为 Make.UI 的函数根据 numericInput 更新 UI,但是将输入返回服务器超出了我的 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)

【问题讨论】:

  • 您确定可以在服务器函数之外定义Make.UI()。它没有可用的输出变量吗?您必须为此使用闪亮的模块。此外,如果你想从 renderUI 返回多个输入,你应该使用tagList()。关于如何使用 renderUI 生成的输入的最后一个问题,只是通过您在服务器中分配它们的 id。考虑到上述说明后,应该会立即起作用。
  • @BigDataScientist 我以前从未使用过 tagList()。你知道任何类似的例子吗?我之前在 Shiny 中使用过全局定义的函数,没有遇到任何问题,但也许这不同。
  • 啊,我明白了,你的output 不是闪亮的输出,而是你自己定义的,好像我有点累了。那么我会尝试改用output=tagList()。顺便说一句,代码不是完全可重现的,因为你没有提供l.colsis 的内容。因此,我以更笼统的方式回答。无论如何,希望它有所帮助。
  • @BigDataScientist OOOpps!!我现在已经更新了,l.cols = dim(D)[2]。我今天早上在想,您的建议似乎是将 Make.UI 放入服务器,然后使用 output = tagList() 而不是 output = list() 然后它会运行。我会在今天晚些时候尝试一下,手指交叉它有效!如果没有,我会告诉你的。
  • @BigDataScientist 在尝试运行编码时,我仍然收到此错误(参数“object”丢失,没有默认值)。不知道出了什么问题。任何帮助将不胜感激。

标签: r shiny


【解决方案1】:

就像我在第一条评论中写的那样,我不确定Make.UI()function。如果您真的想将其保留为单独的功能,则应使其具有反应性。或者就像我在下面的代码中那样使用它。 此外,在output$dataInfo &lt;- 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)

【讨论】:

  • 谢谢!效果很好,很抱歉我没有完全理解您最初的评论。
  • 我知道怎么做!抱歉,我不知道!
  • 我通常不对答案或解决方案发表评论,但对我来说,这非常令人印象深刻,我认为 @BigDataScientist 值得称赞。
  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多