R Shiny selectInput依赖于另一个selectInput

Hil*_*ary 21 r shiny shinydashboard

我在下面有一些数据,我用它来制作R闪亮的圆环图,其中date是一个角色.我希望能够选择我想要查看其分数的电子邮件,但是在第二个下拉选项中只能看到该电子邮件具有活动的日期.

例如,如果我在第一个下拉列表中选择email = xxxx,我希望在日期选择字段中只看到"无活动".而对于email = yyyy,我希望只看到6/17/14,6/18/14,16/19/14作为选择.

我在ui中尝试过一种嵌套的子集.例:

> ui <- shinyUI(fluidPage(
+   sidebarLayout(
+     sidebarPanel(
+       selectInput('Select', 'Customer:', choices = unique(as.character(dat5$email))),
+       selectInput("User", "Date:", choices = dat5[dat5$email==input$Select,date])
+     ),
+     mainPanel(plotOutput("distPlot"))
+   )
+ ))
Run Code Online (Sandbox Code Playgroud)

但这仍然显示所有可能的日期选择

数据

email   date        variable    value   ymin    ymax
xxxx    no activity e_score         0   0       0
xxxx    no activity diff            1   0       1
yyyy    6/17/14     e_score    0.7472   0       0.7472
yyyy    6/17/14     diff       0.2528   0.7472  1
yyyy    6/18/14     e_score    0.373    0       0.373
yyyy    6/18/14     diff       0.627    0.373   1
yyyy    6/19/14     e_score    0.533    0       0.533
yyyy    6/19/14     diff       0.467    0.533   1
Run Code Online (Sandbox Code Playgroud)

我的代码到目前为止:

app.R

library(shiny)
library(shinydashboard)

ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('Select', 'Customer:', choices = unique(as.character(dat5$email))),
      selectInput("User", "Date:", choices = unique(dat5$date) )
    ),
    mainPanel(plotOutput("distPlot"))
  )
))


server <- function(input, output) {
  output$distPlot <- renderPlot({
    ggplot(data = subset(dat5, (email %in% input$Select & date %in% input$User)), aes(fill=variable, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
      geom_rect(colour = "grey30", show_guide = F) +
      coord_polar(theta = "y") +
      geom_text(aes(x = 0, y = 0,label = round(value[1]*100))) +
      xlim(c(0, 4)) +
      theme_bw() +
      theme(panel.grid=element_blank()) +
      theme(axis.text=element_blank()) +
      theme(axis.ticks=element_blank()) +
      xlab("") +
      ylab("") +
      scale_fill_manual(values=c('#33FF00','#CCCCCC')) 

  })
}
  shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

Nic*_*icE 27

您无法访问应用程序的ui.R部分中的输入,因此您需要使用renderUi/uiOutput来动态生成您的selectInput.

在你ui.R可以添加:

uiOutput("secondSelection")
Run Code Online (Sandbox Code Playgroud)

在你的server.R:

 output$secondSelection <- renderUI({
                selectInput("User", "Date:", choices = as.character(dat5[dat5$email==input$Select,"date"]))
        })
Run Code Online (Sandbox Code Playgroud)

  • 我对答案声明“您无法访问应用程序的 ui.R 部分中的输入”感到困惑。当您使用条件面板时,您是根据 input.panel 定义条件。这不是取决于UI的输入吗? (2认同)

Jan*_*Jan 5

您也可以在不更改的情况下执行此操作ui.R。至少该解决方案适用于我所做的应用程序。将此添加到server.R应该具有相同的效果。

observe({
      updateSelectInput(session, "User", choices = as.character(dat5[dat5$email==input$Select, date]))
})
Run Code Online (Sandbox Code Playgroud)

尽管它也很有用且功能强大,但我发现没有renderUI. 它将 UI 保留在 UI 中,将服务器保留在服务器中。但这只是品味问题,我想。

  • 随着应用程序变得越来越大、越来越复杂,我多次被警告不要使用 renderUI,因为它往往会减慢速度并可能导致其他问题。我会使用简的解决方案。 (2认同)