R语言 仅当变量存在时才使用过滤器

yzckvree  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(162)

我正在努力创建一个闪亮的应用程序与互动的选择。如果一个区域已被选中,那么我想过滤我的数据的基础上。但是,如果没有区域已被选中,那么我不想过滤。
我读了一些相关的文章,但似乎没有一个回答我的问题。
下面是我的一些尝试。所有的结果都是一个错误消息

top<-test %>%
   filter(date > start_date & date < end_date ) %>%
   filter(if (exists(selected_Area))Area==selected_Area else TRUE) %>% 
   mutate(Quarter=quarter(date, type = "date_first", fiscal_start = 11)) %>%
   mutate(Year=year(date))%>%
   # getting the top products sold over that timeframe
   group_by(Area,Main_Category,Sub_Category,Sample,Analyte,Isolation_Method) %>%
   summarise(sum_products_grouped=sum(Products_sold)) %>%
   unite(group_name,Sub_Category,Sample,Analyte,Isolation_Method) %>%
    top_n(top_numb,sum_products_grouped)

字符串
我也在想这样的事情,但是语法不正确。

if (exists(selected_Area)) {
         filter(Area==selected_Area) %>% }


数据如下:

> dput(test)
structure(list(PID = c(233L, 233L, 233L, 233L, 233L, 233L, 233L, 
233L, 233L, 233L, 233L, 6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 
6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 
6880L, 6880L, 6880L, 6880L, 6880L, 6880L, 6507L, 6507L, 6507L, 
6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 
6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 
6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L, 6507L
), Name = c("Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", 
"Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", 
"Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", 
"Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", "Genomic DNA Isolation Kit", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Slurry Format)", 
"FBS Exosome Depletion Kit I (Slurry Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)", "FBS Exosome Depletion Kit I (Column Format)", 
"FBS Exosome Depletion Kit I (Column Format)"), Category = c("DNA Kits", 
"DNA Kits", "DNA Kits", "DNA Kits", "DNA Kits", "DNA Kits", "DNA Kits", 
"DNA Kits", "DNA Kits", "DNA Kits", "DNA Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits", "Multi-Analyte Kits", 
"Multi-Analyte Kits", "Multi-Analyte Kits"), Area = c("Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification", "Isolation_and_Purification", "Isolation_and_Purification", 
"Isolation_and_Purification"), Main_Category = c("DNA kits", 
"DNA kits", "DNA kits", "DNA kits", "DNA kits", "DNA kits", "DNA kits", 
"DNA kits", "DNA kits", "DNA kits", "DNA kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits", "exosome kits", "exosome kits", 
"exosome kits", "exosome kits"), Sub_Category = c("DNA Isolation", 
"DNA Isolation", "DNA Isolation", "DNA Isolation", "DNA Isolation", 
"DNA Isolation", "DNA Isolation", "DNA Isolation", "DNA Isolation", 
"DNA Isolation", "DNA Isolation", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up", "Exosome clean-up", "Exosome clean-up", "Exosome clean-up", 
"Exosome clean-up"), Sample_Category = c("multi-sample", "multi-sample", 
"multi-sample", "multi-sample", "multi-sample", "multi-sample", 
"multi-sample", "multi-sample", "multi-sample", "multi-sample", 
"multi-sample", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", ""), Sample = c("multi-sample", "multi-sample", 
"multi-sample", "multi-sample", "multi-sample", "multi-sample", 
"multi-sample", "multi-sample", "multi-sample", "multi-sample", 
"multi-sample", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", 
"FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", 
"FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", 
"FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", 
"FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", 
"FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS", "FBS"), Analyte = c("totDNA", 
"totDNA", "totDNA", "totDNA", "totDNA", "totDNA", "totDNA", "totDNA", 
"totDNA", "totDNA", "totDNA", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", ""), Sample_Volume = c("micro", 
"micro", "micro", "micro", "micro", "micro", "micro", "micro", 
"micro", "micro", "micro", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", "maxi", 
"maxi", "maxi", "maxi", "maxi", "maxi", "maxi"), Isolation_Method = c("spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"slurry", "slurry", "slurry", "slurry", "slurry", "slurry", "slurry", 
"slurry", "slurry", "slurry", "slurry", "slurry", "slurry", "slurry", 
"slurry", "slurry", "slurry", "slurry", "slurry", "slurry", "slurry", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column", 
"spin column", "spin column", "spin column", "spin column", "spin column"
), Product_Size = c(100L, 100L, 100L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 100L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), date = structure(c(18440, 
18470, 18501, 18532, 18562, 18593, 18623, 18654, 18685, 18713, 
18744, 19050, 19078, 19109, 19139, 19170, 19200, 19231, 19262, 
19292, 19323, 19353, 19384, 19415, 19443, 19474, 19504, 19535, 
19565, 19596, 19627, 19657, 17862, 17892, 17923, 17954, 17982, 
18013, 18043, 18074, 18104, 18135, 18166, 18196, 18227, 18257, 
18288, 18319, 18348, 18379, 18409, 18440, 18470, 18501, 18532, 
18562, 18593, 18623, 18654, 18685, 18713, 18744), class = "Date"), 
    Products_sold = c(3L, 5L, 1L, 2L, 3L, 5L, 2L, 2L, 0L, 0L, 
    0L, 0L, 2L, 0L, 3L, 0L, 2L, 0L, 0L, 3L, 0L, 1L, 3L, 1L, 1L, 
    0L, 2L, 0L, 1L, 0L, 0L, 5L, 2L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 
    1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 2L, 0L)), row.names = c(NA, -62L), class = c("tbl_df", 
"tbl", "data.frame"))

kiayqfof

kiayqfof1#

考虑到管道,使用下面的命令:

... %>% 
 { if (exists('selected_Area')) filter(., Area == selected_Area) 
   else .} %>%
...

字符串
例如:比较以下内容

iris %>%
   { if (exists('Species', .)) head(.)
     else NA
   }
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

 iris %>%
   { if (exists('Species2', .)) head(.)
     else NA
   }
 [1] NA

r8uurelv

r8uurelv2#

一种选择是创建一个小的辅助函数filter_if_selected

filter_if_selected <- function(data, selected){
  if(is.null(selected)) return(data) # or, if needed. replace is.null() with exists()
  dplyr::filter(data, Area == selected)
}

top<-test %>%
  filter(date > start_date & date < end_date ) %>%
  filter_if_selected(selected_Area) %>%
  mutate(Quarter=quarter(date, type = "date_first", fiscal_start = 11)) %>%
  mutate(Year=year(date))%>%
  # getting the top products sold over that timeframe
  group_by(Area,Main_Category,Sub_Category,Sample,Analyte,Isolation_Method) %>%
  summarise(sum_products_grouped=sum(Products_sold)) %>%
  unite(group_name,Sub_Category,Sample,Analyte,Isolation_Method) %>%
  top_n(top_numb,sum_products_grouped)```

字符串

相关问题