R 如何在ggvis代码中使用条件?

R 如何在ggvis代码中使用条件?,r,shiny,conditional-statements,ggvis,R,Shiny,Conditional Statements,Ggvis,我有一个数据框,其中包含我想要打印的不同列,但我已经在Shiny中创建了一个复选框来指示我想要打印的列 用户界面 服务器.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$inve

我有一个数据框,其中包含我想要打印的不同列,但我已经在Shiny中创建了一个复选框来指示我想要打印的列

用户界面

服务器.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")

我终于改正了!这是一个额外的“>%”,我更改了代码:

服务器.R
无功({
plt%ggvis(~销售额)
如果('在%input$investcheck中打印“%1”){
plt%
图层点数(数据=df\U销售额,x=~销售额,y=~打印投资,填充:='3498DB')
}
if('TV'%in%input$investcheck){
plt%
图层点数(数据=df\U销售额,x=~销售额,y=~电视投资,填充:='1ABC9C')
}
if('OOH'%in%input$investcheck){
plt%
图层点数(数据=df\U销售额,x=~销售额,y=~OOH\U投资,填充:='F39C12')
}
if((%input$investcheck中的数字“%1”){
plt%
图层点数(数据=df\U销售额,x=~销售额,y=~数字投资,填充:='E74C3C')
}
plt
})%%>%bind_(图4)
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
  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")