【发布时间】:2021-01-27 06:18:44
【问题描述】:
以下应用会根据所选变量的数量生成动态 UI。 A problem is that when the number of variables selected is odd, the app generates an extra UI that is not tied to any of the variables previously selected.我尝试在fluidRow 创建语句中包含if 语句,主要是检查是否有余数,如果有,我试图告诉应用程序插入一个空格,但这并不能解决问题。有人对如何解决此问题有任何建议吗?
## libraries
library(tidyverse)
library(shiny)
ui <- fluidPage(
selectInput(inputId = "var",
label = "vars:",
choices = colnames(mtcars),
multiple = TRUE),
uiOutput("dynUI")
)
server <- function(input, output, session) {
output$dynUI <- renderUI({
row_idx <- length(input$var) %>% seq_len
row_idx <- row_idx[row_idx %% 2 == 1]
row_idx %>%
map(~fluidRow(column(width = 2,
selectizeInput(inputId = paste0("var", .x),
label = paste(input$var[.x], "var:"),
choices = c("this", "that"),
multiple = FALSE)),
column(width = 2,
selectizeInput(inputId = paste0("var", .x + 1),
label = paste(input$var[.x + 1], "var:"),
choices = c("this", "that"),
multiple = FALSE))))
})
}
shinyApp(ui, server)
【问题讨论】: