r - 依赖于另一个 selectInput 的 selectInput

标签 r shiny shinydashboard

我在下面有一些数据,用于在 R Shiny 中创建圆环图,其中 date是一个字符。我希望能够选择我想要查看其分数的电子邮件,但是在第二个下拉选择中只能看到该电子邮件具有事件的日期。

例如,如果我在第一个下拉列表中选择 email = xxxx,我只想在日期选择字段中看到“无事件”。对于电子邮件 = yyyy,我只想看到 6/17/14、6/18/14、6/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"))
+   )
+ ))

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

数据
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

到目前为止我的代码:

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)

最佳答案

您也可以在不更改 ui.R 的情况下这样做.将此添加到 server.R有同样的效果。

    observe({
          updateSelectInput(session, "User", choices = as.character(dat5[dat5$email==input$Select, date]))
    })
虽然它是一个有用且功能强大的工具,但我发现没有 renderUI 的情况下它会“更干净”。 .它将 UI 保留在 UI 中,将服务器保留在服务器中。但这只是口味问题,我想。

关于r - 依赖于另一个 selectInput 的 selectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34929206/

相关文章:

r - R 中用户定义函数中的公式

css - 一些右对齐的 tabPanels Shiny

r - MenuItem 呈现为 subItem

r - 在 R 中 Shiny 的仪表板应用程序中显示 LaTeX 方程

r - 如何在R中缩写字符串

r - 按组变量排列grouped_df不起作用

r - 如何在 R Markdown 的 YAML 元数据中创建多级字典?

r - 如何更改 R Shiny 应用程序中图标的颜色?

R Shiny 的navbarPage右对齐标签

r - 当 multiple 为 true 时,如何限制用户可以使用 R Shiny 的 selectInput 选择的选项数量?