gpt4 book ai didi

r - 具有多个输入的 Shiny 渲染UI

转载 作者:行者123 更新时间:2023-12-01 17:37:46 25 4
gpt4 key购买 nike

My Shiny App 有多个输入,这些输入取决于使用的变量数量。下面是一个简化版本,虽然不起作用。我能够使用名为 Make.UI 的函数根据 numericInput 更新 UI,我曾经使用该函数制作 uiOutput,但是将输入重新输入服务器超出了我的 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()功能。如果你真的想把它作为一个单独的函数,你应该让它成为响应式(Reactive)。或者像我在下面的代码中那样使用它。
此外,在 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)

关于r - 具有多个输入的 Shiny 渲染UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com