簡體   English   中英

使用帶有條件語句的fluidRow

[英]Using fluidRow with a conditional statement

以下應用程序根據所選變量的數量生成動態 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)

您可以使用is.na(input$var[.x + 1])檢測奇數變量,然后將其跨越 4 列,如下所示:

row_idx %>%
  map( ~ {
    if (!is.na(input$var[.x + 1]))
      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
        )
      ))
    else
      fluidRow(column(
        width = 4,
        selectizeInput(
          inputId = paste0("var", .x),
          label = paste(input$var[.x], "var:"),
          choices = c("this", "that"),
          multiple = FALSE
        )
      ))
  })

結果

像下面這樣的東西怎么樣?

library(tidyverse)
library(shiny)

column2 = function(x, input) {
  column(
    width = 2,
    selectizeInput(
      inputId = paste0("var", x), 
      label = paste(input$var[x], "var:"),
      choices = c("this", "that"),
      multiple = FALSE
    )
  )
}

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 <- split(row_idx, (seq(row_idx) - 1) %/% 2)
    map(row_idx, function(x, input) fluidRow(map(x, column2, input = input)), input = input)
  })
}

shinyApp(ui, server)

編輯:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM