【问题标题】:Adding an Interactive Menu based on SelectInput in R Shiny在 R Shiny 中添加基于 SelectInput 的交互式菜单
【发布时间】:2017-10-16 05:31:50
【问题描述】:

给定的脚本创建附加的快照。它是使用 R 中的 DT 包创建的表。我想在表上方制作菜单,以便通过在第一个 SelectInput 中选择输入“A”,我得到第二个 selectInput有两个滑块,在第一个 SelectInput 中选择“B”时,我应该只得到第二个 SelectInput 而没有滑块。桌面虹膜无需更改。请帮忙,谢谢。

## 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)

【问题讨论】:

  • 嗨哈迪克,谢谢,我已经研究过这个功能,我的问题是,我们可以像这个链接所说的标签集条件面板一样,用条件面板实现 selectInput,如果您有一个工作示例,请分享。
  • 您可以使用conditionalPaneluiOutputrenderUI
  • 我想使用 conditialPanel 添加多个 selectInputs 和滑块,你碰巧有一个工作示例的好链接吗?
  • 非常感谢 SBista 和 Hardik 的帮助。

标签: r shiny


【解决方案1】:

这是您提供的代码和您询问的输出的工作示例:

## 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)

如 cmets 所述,诀窍是使用 conditionalPanel

【讨论】:

【解决方案2】:

您可以像这样添加多个小部件:

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)))
    })
  }
))

【讨论】:

猜你喜欢
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
相关资源
最近更新 更多