2016-01-21 95 views
5

我有一些數據在下面,我正用它來創建一個R閃亮的圓環圖,其中date是一個字符。我希望能夠選擇要查看其分數的電子郵件,但在第二個下拉選擇中,只能查看該電子郵件具有活動的日期。R依賴於另一個selectInput的閃亮selectInput

例如,如果我在第一個下拉列表中選擇email = xxxx,我希望在日期選擇字段中只看到'no activity'。而對於電子郵件= 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")) 
+ ) 
+)) 

但是,這仍然顯示了所有可能的日期選擇

DATA

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) 

回答

8

您無法訪問應用程序的ui.R部分中的輸入,因此您需要使用renderUi/uiOutput動態生成selectInput。

在你ui.R您可以添加:

uiOutput("secondSelection") 

,並在您server.R

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

也做到了!出於某種原因,我必須選擇= as.character(dat5 [dat5 $ email == input $ Select,「date」])'不帶日期的引號。否則工作很好! – Hillary

+0

我很困惑的回答聲明「你不能在應用程序的ui.R部分訪問輸入」。當你使用條件面板時,你正在根據input.panel定義條件。這不取決於用戶界面的輸入嗎? –