簡體   English   中英

R Shiny:帶有renderUI的條件面板不起作用

[英]R Shiny: conditionalPanel with renderUI not working

我正在嘗試在某些條件下顯示一些 UI。 I would like the way to choose input to vary according to the type of data: when the type selected is Type X , I want to display a selectizeInput and when Type Y is selected, I want to display a sliderInput . 為此,我在conditionalPanel中使用uiOutput

這是我到目前為止所做的,遵循這個問題

library(shiny)
library(shinyWidgets)
library(WDI)
library(DT)
library(dplyr)

foo <- data.frame(foo_name = c("A", "A", "B", "B", "C", "C"))
data <- cbind(head(mtcars), foo)

ui <- navbarPage(position = "static-top",

                 tabPanel(title = "Base 1",
                          fluidRow(
                            dropdownButton(

                              selectInput(inputId = "choice",
                                          label = "Type of data",
                                          choices = c("Type X",
                                                      "Type Y"),
                                          selected = NULL,
                                          multiple = FALSE),
                              conditionalPanel(
                                condition = "input$choice == 'Type X'",
                                uiOutput("machin")
                                ),
                              conditionalPanel(
                                condition = "input$choice == 'Type Y'",
                                uiOutput("machin2")
                                ),

                              circle = TRUE, status = "primary", 
                              icon = icon("gear"), 
                              width = "300px",
                              tooltip = tooltipOptions(title = "Outils")
                            ),
                            column(width = 12,
                                   dataTableOutput("data"))
                          ))

)


server <- function(input, output) {

  output$machin <- renderUI({
        selectizeInput(inputId = "test",
                       label = "test",
                       choices = unique(data$foo_name),
                       selected = NULL, 
                       multiple = T)
  })

output$machin2 <- renderUI({
      sliderInput("test2", 
                    "Test2", 
                    min = min(data$hp), 
                    max = max(data$hp), 
                    value = c(min(data$hp), 
                              max(data$hp))
                    )
        })


  output$data <- renderDataTable({
    if(input$choice == "Type X"){
    data2 <- data %>%
      filter(foo_name %in% input$test)
    }
    else if(input$choice == "Type Y"){
      data3 <- data %>%
        filter(hp %in% input$test2)
    }
  })

}


shinyApp(ui = ui, server = server)

這里有幾個問題:

  • 無論選擇何種數據類型,都會顯示兩種類型的輸入
  • when Type X is selected, the dataframe is empty (since the selectizeInput is NULL) but when Type Y is selected, some values of the dataframe are displayed

我怎樣才能解決這個問題?

下次首先查看 R ( ?conditionalPanel ) 中的幫助頁面或在線查找 function。 條件是 JavaScript 表達式,因此您的 $ 符號不起作用。 它應該是一個。 (點)。

conditionalPanel(
  condition = "input.choice == 'Type X'",
  uiOutput("machin")
),
conditionalPanel(
  condition = "input.choice == 'Type Y'",
  uiOutput("machin2")
)

暫無
暫無

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

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