R - dplyr - ifelse 和过滤器 [英] R - dplyr - ifelse and filter

查看:20
本文介绍了R - dplyr - ifelse 和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Shiny 上构建一个小部件,我希望使用全部"选项来选择所有可用数据,并且不执行过滤.

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.

基本上,我想要以下代码(使用 dplyr):

Basically, I would like to have the following code (using dplyr):

filt<-sample(c("All", unique(mtcars$carb)),1)

data1<- mtcars %>% 
                  ifelse (filt=="All", select(), filter(carb==filt))

它将根据 filt 的值过滤 mtcars.

It will filter mtcars based on the value of filt.

如果 filt=="All" 那么它不会过滤并简单地返回 mtcars.

If filt=="All" then it does not filter and return simply mtcars.

任何优雅的解决方案?

推荐答案

这样的事情应该可以工作(通过适当的修改以在 filt 变量的这个反应中使用输入值):

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
})

这篇关于R - dplyr - ifelse 和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆