R - dplyr - ifelse and filter

前端 未结 2 702
无人共我
无人共我 2021-01-17 04:07

I am building a widget on Shiny, and I would like to have the option \"all\" to select all of the data available, and don\'t perform a filtering.

Basically, I would

相关标签:
2条回答
  • 2021-01-17 04:50

    Something like this should work (with proper modifications to use the input value in this reactive for the filt variable):

    reactiveObject <- reactive({
      filt <- sample(c("All", unique(mtcars$carb)),1)
    
      if (filt == 'All') {
        data1 <- mtcars
      } else {
        data1 <- filter(mtcars, carb == filt)
      }
      data1
    })
    
    0 讨论(0)
  • Here is my try. If folt="All" then there is no filter, otherwhise just the Cars with carb==filt is returned.

       filt <-sample(c("All", unique(mtcars$carb)),1)
        filt
        if(filt=="All"){
          data1<- mtcars
        } else {
          data1<- filter(mtcars, mtcars$carb==filt)
        }
    

    should do the trick

    0 讨论(0)
提交回复
热议问题