2016-01-14 50 views
1

我有一個不同列的數據框,我想繪製,但是我在Shiny中創建了一個複選框來指示我想要繪製哪些。如何在我的ggvis代碼中使用條件?

UI.R

tabPanel('Data Insights', 
      mainPanel(
         ggvisOutput("plot4"), 
         fluidRow(
           checkboxGroupInput('investcheck', 'Investment', 
               choices = c('OOH', 'PRINT', 'TV', 'DIGITAL'), 
               selected=c('TV')))    
      ) 
) 

SERVER.R

reactive({ 
    if(is.element('PRINT', input$investcheck)) {oprint=1} else {oprint=0} 
    if(is.element('OOH', input$investcheck)) {oooh=1} else {oooh=0} 
    if(is.element('TV', input$investcheck)) {otv=1} else {otv=0} 
    if(is.element('DIGITAL', input$investcheck)) {odigital=1} else {odigital=0} 

    plt <- df_sales %>% ggvis(~sales) %>% 
    if(oprint==1) { 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~PRINT_Investment, fill:='3498DB') 
    } else{ 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~PRINT_Investment, fill:='3498DB') 
    } 
    plt 

    }) %>% bind_shiny("plot4") 

但我始終有一個錯誤:

Warning in if (.) oprint == 1 else { : 
    the condition has length > 1 and only the first element will be used 
Error in if (.) oprint == 1 else { : 
    argument is not interpretable as logical 

回答

1

我終於糾正了!這是一個額外的 '%>%',我改變了我的代碼:

SERVER.R

reactive({ 


    plt <- df_sales %>% ggvis(~sales) 
    if('PRINT' %in% input$investcheck) { 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~PRINT_Investment, fill:='3498DB') 
    } 
    if('TV' %in% input$investcheck) { 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~TV_Investment, fill:='1ABC9C') 
    } 
    if('OOH' %in% input$investcheck) { 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~OOH_Investment, fill:='F39C12') 
    } 
    if('DIGITAL' %in% input$investcheck) { 
     plt <- plt %>% 
     layer_points(data = df_sales, x = ~sales, y = ~DIGITAL_Investment, fill:='E74C3C') 
    } 

    plt 

    }) %>% bind_shiny("plot4") 
相關問題