使用dateRangeInput过滤数据帧不会产生所需的结果

时间:2017-10-04 12:17:54

标签: r shiny

在我的Shiny App中,我添加了一个dateRangeInput,它以开始和结束日期作为输入。 我还有另一个selectInput ui元素,它接受一个输入(从这篇文章中排除)

ui.R

的摘录
column(wellPanel(
tags$style('.input-sm {font-size: 20px; } label {font-weight: 500; margin-bottom: 30px; }'),
dateRangeInput("inp_pg1daterange", 
               label = paste('Date Range Selection'),
               start = min(results_combined$Date),
               end = max(results_combined$Date), 
               separator = " to ", 
               weekstart = 1

)

),width=3)

server.R 方面,我希望日期输入为:

1)过滤数据帧 2)将过滤后的数据集转换并存储到一组不同的变量中 3)根据selectInput,显示具有正确值的valueBox

这就是服务器代码的样子

server <- function(input, output,session) {

步骤1)使用日期范围输入过滤数据框

kpidf_pg1_totalqol= reactive({

results_combined %>%
filter(SVM_LABEL_QOL=='QoL' & Date >=input$inp_pg1daterange[[1]] & Date <=input$inp_pg1daterange[[2]])  %>%
select(`Global Segment`=globalsegment,Classified=SVM_LABEL_QOL) %>%
group_by(`Global Segment`) %>%
summarise(n=n()) %>%
select(`Global Segment`,Count=n) %>%
ungroup()

}) #close reactive function

步骤2)将过滤后的数据集转换并存储到一组不同的变量中(仍然在同一个反应函数内)

totalqol_enr <- unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "ENR", select = Count))
totalqol_def <- unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "DEF", select = Count))
totalqol_snr <- unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "SNR", select = Count))
totalqol_jus <- unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "JUS", select = Count))
totalqol_gov <- unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "GOV", select = Count))
totalqol_hc <-  unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "HC", select = Count))
totalqol_spl<-  unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "SPL", select = Count))

步骤3)基于selectInput,应用会显示valueBox,其中包含上一步中的正确值

output$KPItotalqol <-renderValueBox({
if(input$inp_pg1segment=="ENR")
{
  valueBox(
    value = totalqol_enr()
    ,"Total number of QoL tweets identified"
    ,icon = icon("twitter-square")
    ,color = "green")
}
else
  if(input$inp_pg1segment=="DEF")
  {
    valueBox(
      value = totalqol_def()
      ,"Total number of QoL tweets identified"
      ,icon = icon("twitter-square")
      ,color = "green")
  }
else
  if(input$inp_pg1segment=="SNR")
  {
    valueBox(
      value = totalqol_snr()
      ,"Total number of QoL tweets identified"
      ,icon = icon("twitter-square")
      ,color = "green")
  }
else
  if(input$inp_pg1segment=="JUS")
  {
    valueBox(
      value = totalqol_jus()
      ,"Total number of QoL tweets identified"
      ,icon = icon("twitter-square")
      ,color = "green")
  }
else
  if(input$inp_pg1segment=="GOV")
  {
    valueBox(
      value = totalqol_gov()
      ,"Total number of QoL tweets identified"
      ,icon = icon("twitter-square")
      ,color = "green")
  }
else
  if(input$inp_pg1segment=="HC")
  {
    valueBox(
      value = totalqol_hc()
      ,"Total number of QoL tweets identified"
      ,icon = icon("twitter-square")
      ,color = "green")
  }
else
{
  valueBox(
    value = totalqol_spl()
    ,"Total number of QoL tweets identified"
    ,icon = icon("twitter-square")
    ,color = "green")
}

}) 

然而,这会产生一个错误,表示函数Could not find function totalqol_def

任何关于如何运作的想法都将不胜感激!

1 个答案:

答案 0 :(得分:1)

所有这些函数表达式必须单独或组合地包含在reactive中。

单独地,

示例:

totalqol_enr <- reactive({ 
unlist(subset(kpidf_pg1_totalqol(), `Global Segment` == "ENR", select = Count))
})

因此,您可以调用这些函数totalqol_enr()