2017-10-04 14 views
0

在我的Shiny App中,我包含了一個以開始和結束日期爲輸入的dateRangeInput。 我也有另一個selectInput UI元素,其採用輸入(從該交除外)使用dateRangeInput過濾數據框不會產生所需的結果

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側的提取物,我期望基於一個selectInput

1)過濾一個數據幀 2)轉換和濾波後的數據集存儲到一組不同的變量 3):日期輸入,爲正確的值

這顯示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

錯誤任何想法如何可以工作將不勝感激!

+0

爲什麼你叫'''內本身kpidf_pg1_totalqol'''? – amrrs

+0

我建議你查看https://www.programiz.com/r-programming/if-else-statement以獲取正確的「if else-if else」語句。 –

+0

@amrrs我編輯了它。但它仍然給我一個錯誤(你試圖做的事情只能從一個被動的表達式或觀察者內部完成) – Varun

回答

1

所有這些函數表達式都必須包含在單獨或組合的reactive中。

獨立,

例子:

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

所以,你可以調用這些函數totalqol_enr()

+0

再次感謝您的幫助,我將它們分別包含在內,並且它們都可以工作。我明白我做錯了什麼。當你說包含這些函數表達式時,你會怎麼做? – Varun

相關問題