我正在尝试将 NSE 与dplyr
and一起使用tidyr
。
我有一个产品列表,每个地区每个月的数量。产品和区域具有层次结构(ProductLevel1 粗于 ProductLevel2 粗于 Product Level3 ...)。在制作图表之前,我想使用tidyr::nesting
和添加没有音量的月份tidyr::complete
。
这在功能层面上tidyr::nesting
似乎失败了。
f_test <- function(IS1, finerProductLevel, finerRegionLevel) {
qfinerProductLevel <- enquo(finerProductLevel)
qfinerRegionLevel <- enquo(finerRegionLevel)
IS2 <- IS1 %>%
group_by(calDate, !! qfinerRegionLevel, !! qfinerProductLevel) %>%
summarize(Vol = sum(Vol, na.rm = TRUE)) %>%
ungroup()
IS3 <- IS2 %>%
complete(nesting(!! qfinerProductLevel, !! qfinerRegionLevel),
calDate, fill = list(Vol=0))
}
f_test(IS1, ProductLevel4, RegionLevel4_18)
我收到的错误是:
Error in eval_tidy(xs[[i]], unique_output) : object 'ProductLevel4' not found
如果我删除!!
嵌套函数中的 s ,那么错误是
Error: Columns `qfinerProductLevel`, `qfinerRegionLevel` must be 1d atomic vectors or lists
过去有一个类似的问题没有答案:dplyr programming: unquote-splicing cause overscope error with complete() and nesting()
数据集样本:
IS1 <- structure(list(COPASalesOffice = c("DACH", "DACH", "EMO", "EMO",
"France", "French Africa", "Israel", "Italy", "Middle East",
"Nordic", "South Hub", "South Hub", "Spain Portugal", "Spain Portugal",
"Turkey", "Turkey", "Turkey", "UK Ireland", "UK Ireland"), calDate = structure(c(16861,
17440, 16983, 17410, 16436, 16709, 16161, 16222, 17136, 16526,
16861, 16922, 16587, 17136, 16495, 16283, 16983, 16222, 16740
), class = "Date"), Vol = c(75.17, 121.731, 0, 23, 0, 15, 30,
173.36, 0, 9.217, -1, 0, 0.849, 12.154, 0, 0, 25, 1, 20.313),
RegionLevel0 = c("EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR",
"EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR",
"EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR", "EMEAR"
), RegionLevel1 = c("Europe", "Europe", "MEAR", "MEAR", "Europe",
"MEAR", "MEAR", "Europe", "MEAR", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe"), RegionLevel2 = c("EU5-NB", "EU5-NB", "CIS", "CIS",
"EU5-NB", "MEA", "MEA", "EU5-NB", "MEA", "EU5-NB", "CEE",
"CEE", "EU5-NB", "EU5-NB", "CEE", "CEE", "CEE", "EU5-NB",
"EU5-NB"), RegionLevel4_18 = c("R01", "R01", "R20", "R20",
"R03", "R14", "R17", "R04", "R12", "R06", "R08", "R08", "R05",
"R05", "R09", "R09", "R09", "R02", "R02"), ProductLevel1 = c("aqueous",
"aqueous", "aqueous", "aqueous", "aqueous", "aqueous", "aqueous",
"aqueous", "aqueous", "aqueous", "aqueous", "aqueous", "aqueous",
"aqueous", "aqueous", "aqueous", "aqueous", "aqueous", "aqueous"
), ProductLevel2 = c("aq8", "aq8", "aq8", "aq8", "aq8", "aq4",
"aq4", "aq4", "aq8", "aq8", "aq8", "aq8", "aq8", "aq8", "aq8",
"aq8", "aq4", "aq8", "aq8"), ProductLevel3 = c("8.24-44",
"8.24-44", "8.24-44", "8.24-44", "8.24-44", "4.24-44", "4.24-44",
"4.24-44", "8.A2", "8.24-44", "64in", "8.A2", "8.24-44",
"64in", "8.24-44", "8.24-44", "4.24-44", "8.24-44", "8.24-44"
), ProductLevel4 = c("T636-596-642, H&H", "T804, SC-P6/7/8/9000",
"T636-596-642, H&H", "T636-596-642, H&H", "T40X, 9000", "T692-693-694-698 Opr",
"T611-612, 74x0-94x0", "T611-612, 74x0-94x0", "T850, SCP800",
"T543-544, 40-76-9600", "T591, 11880", "T605-606, 48x0",
"T543-544, 40-76-9600", "T591, 11880", "T46X, 7000", "T602-603, 78x0-98x0",
"T692-693-694-698 Opr", "T47X, 9500", "T543-544, 40-76-9600"
)), row.names = c(NA, -19L), class = c("tbl_df", "tbl", "data.frame"
), .Names = c("COPASalesOffice", "calDate", "Vol", "RegionLevel0",
"RegionLevel1", "RegionLevel2", "RegionLevel4_18", "ProductLevel1",
"ProductLevel2", "ProductLevel3", "ProductLevel4"))
和会话信息:
Rstudio 1.1.442
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.3.0 stringr_1.3.0 dplyr_0.7.4 purrr_0.2.4 readr_1.1.1 tidyr_0.8.0 tibble_1.4.2 ggplot2_2.2.1 tidyverse_1.2.1