【发布时间】: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”丢失,没有默认值)。不知道出了什么问题。任何帮助将不胜感激。