本文介绍了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))

它将根据 mtcars em> filt 。

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.

有什么优雅的解决方案吗?

Any elegant solution?

推荐答案

类似的东西应该可以工作(对使用此反应式中的输入值作为 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和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:24