我闪亮的应用程序以 a 开头,checkboxGroupInput
其中包含三个公司的名称A
:B
和C
. 它还有 3 个hidden
数字输入,每个对应一个公司。潜在投资者可以选择他们希望投资的公司名称,并指定他们愿意投资的金额。检查公司名称时,会显示相应的数字输入。此外,当未选中公司名称时,数字输入消失。
被checkboxGroupInput
称为company
。这3个numericInput
字段分别称为amountA
,amountB
和amountC
都是在a内部生成的uiOutput
。它们以 的hidden
功能隐藏shinyjs
。
library(shiny)
library(shinyjs)
library(magrittr)
ui <- fluidPage(
useShinyjs(),
checkboxGroupInput(inputId = "company", label = "Select a company", choices = LETTERS[1:3]),
uiOutput(outputId = "amounts")
)
server <- function(input, output){
company_names <- LETTERS[1:3]
num_ids <- paste0("amount", LETTERS[1:3])
output$amounts <- renderUI({
num_inputs <- lapply(1:3, function(i){
numericInput(inputId = num_ids[i], label = paste0("Investment in ", company_names[i]), value = 0, min = 0, max = 5000)
}) %>% tagList
shinyjs::hidden(num_inputs)
})
observeEvent(eventExpr = input$company, handlerExpr = {
if(length(input$company) == 0){
for(i in num_ids){
shinyjs::hide(id = i)
}
} else {
for(i in input$company){
shinyjs::toggle(id = paste0("amount", i), condition = input$company)
}
}
})
}
shinyApp(ui = ui, server = server)
checkboxGroupInput
我的应用程序的问题是和numericInput
字段之间的预期动态没有按预期工作。例如,一旦numericInput
显示 a,就不能再通过取消选中复选框来隐藏它。我该如何处理?
上面粘贴的代码功能齐全。非常感谢。