首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >依赖于另一个selectInput的selectInput

依赖于另一个selectInput的selectInput
EN

Stack Overflow用户
提问于 2016-01-21 16:32:37
回答 2查看 25.4K关注 0票数 39

下面我有一些数据,我用它来创建一个R闪亮的甜甜圈图,其中date是一个字符。我希望能够选择我想查看其分数的电子邮件,但在第二个下拉选项中,只能看到该电子邮件具有活动的日期。

例如,如果我在第一个下拉列表中选择email = xxxx,我希望在日期选择字段中只看到‘无活动’。至于电子邮件= yyyy,我只想看到6/17/14,6/18/14,6/19/14作为选择。

我在ui中尝试了一种嵌套的子设置。示例:

代码语言:javascript
复制
> 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"))
+   )
+ ))

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

数据

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-21 18:00:01

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

在您的ui.R中可以添加:

代码语言:javascript
复制
uiOutput("secondSelection")

在你的server.R

代码语言:javascript
复制
 output$secondSelection <- renderUI({
                selectInput("User", "Date:", choices = as.character(dat5[dat5$email==input$Select,"date"]))
        })
票数 41
EN

Stack Overflow用户

发布于 2020-05-28 17:40:58

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

代码语言:javascript
复制
    observe({
          updateSelectInput(session, "User", choices = as.character(dat5[dat5$email==input$Select, date]))
    })

虽然它是一个有用和强大的工具,但我发现没有renderUI就能“更干净”地度过难关。它将UI保存在UI中,服务器保持在服务器中。但我想这只是品味的问题。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34929206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档