简体   繁体   English

在R Shiny中基于SelectInput添加交互式菜单

[英]Adding an Interactive Menu based on SelectInput in R Shiny

the given script creates the attached snapshot.It is a table created using DT package in R. I want to make the menu above the table such that by selecting an input "A" in the first SelectInput, I get the second selectInput with two sliders, while selecting "B" in the first SelectInput, I should get only the second SelectInput and no sliders. 给定的脚本将创建附加的快照。这是一个使用R中的DT包创建的表。我想在表上方创建菜单,以便通过在第一个SelectInput中选择输入“ A”来获得带有两个滑块的第二个selectInput ,在第一个SelectInput中选择“ B”时,我应该只获得第二个SelectInput而没有滑块。 There is no change needed on the table iris. 表格光圈无需更改。 Please help and Thanks. 请帮助,谢谢。

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)

#Declaring the UI
ui <- fluidPage(
titlePanel("Interactive Menu"),

# Create a new Row in the UI for selectInputs
fluidRow(
column(3,
     selectInput("names",
                 "Customer:",
                 c("A","B"))
)),
fluidRow(

column(4,
       selectInput("names",
                   "Customer:",
                   c(as.character(iris$Species)))
),
column(4,
       sliderInput("slide", "Select the slider one",
                   min = 75, max = 100,
                   value = 75, step = 5)

),
column(4,

       sliderInput("city", "Select the slider two",
                   min = 60, max = 100,
                   value = 60, step = 10)
)),

# Create a new row for the table.
fluidRow(
DT::dataTableOutput("table1")
)
)
#Declaring the Server
server <- function(input, output) {

# Filter data based on selections
output$table1 <- renderDataTable({

datatable(iris, options = list(
  searching = FALSE,
  pageLength = 5,
  lengthMenu = c(5, 10, 15, 20)
))
})
}
shinyApp(ui, server)

iris_capture

You can add multiple widgets like so: 您可以添加多个小部件,如下所示:

rm(list = ls())
library(shiny)
runApp(list(
  ui = bootstrapPage(
    selectInput('data', 'Data', c('mtcars', 'iris')),
    uiOutput('columns')
  ),
  server = function(input, output){
    output$columns <- renderUI({
      mydata <- get(input$data)
      tagList(
      selectInput('columns2', 'Columns', names(mydata)),
      selectInput('columns3', 'Columns 2', names(mydata)))
    })
  }
))

Here is your working example with the code you provided and the output you asked : 这是您的工作示例,其中包含您提供的代码和您要求的输出:

## app.R ##
library(shiny)
library(shinydashboard)
library(DT)

#Declaring the UI
ui <- fluidPage(
  titlePanel("Interactive Menu"),

  # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4,
      selectInput("names",
                  "Customer:",
                  c("A","B")))
      )
    ,
  fluidRow(
    conditionalPanel(
      condition = "input.names == 'A'",
      column(4,
             selectInput("names",
                         "Customer:",
                         c(as.character(iris$Species)))
             ),
      column(4,
             sliderInput("slide", "Select the slider one",
                         min = 75, max = 100,
                         value = 75, step = 5)),
      column(4,
             sliderInput("city", "Select the slider two",
                         min = 60, max = 100,
                         value = 60, step = 10)
      )
      ),
    conditionalPanel(
      condition = "input.names == 'B'",
    column(4,
           selectInput("names",
                       "Customer:",
                       c(as.character(iris$Species)))
           ))
    ),

  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table1")
  )
)
#Declaring the Server
server <- function(input, output) {

  # Filter data based on selections
  output$table1 <- renderDataTable({

    datatable(iris, options = list(
      searching = FALSE,
      pageLength = 5,
      lengthMenu = c(5, 10, 15, 20)
    ))
  })
}
shinyApp(ui, server)

在此处输入图片说明

在此处输入图片说明

As stated in the comments, the trick is to use conditionalPanel . 如评论中所述,诀窍是使用conditionalPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM