问题
在一个函数中引用一个参数的正确方法是什么,这个参数将被用来创建一个新的变量,这个变量将被传递给另一个函数?
背景
最终的目标是在 Dataframe 中为具有2级层次结构的树图创建标签,我正在尝试创建一个可重用的函数。
示例
library(scales)
library(tidyverse)
# Create dataframe
region = rep(c("North", "South"), 3)
district <- sprintf("Dist-%d", 1:6)
sales <- seq(2000, 1500000000, length.out = 6)
df <- tibble(region, district, sales)
df
# A tibble: 6 × 3
region district sales
<chr> <chr> <dbl>
1 North Dist-1 2000
2 South Dist-2 300001600
3 North Dist-3 600001200
4 South Dist-4 900000800
5 North Dist-5 1200000400
6 South Dist-6 1500000000
我创建了这个helper函数来格式化货币,它将在main函数中使用,我的问题与从main函数向这个helper函数传递一个新的变量名有关:
# First function for formatting currency
mydollars <- scales::label_dollar(prefix = "$",
largest_with_cents = 5000,
scale_cut = c(0, " K" = 1e3, " M" = 1e6, " B" = 1e9, " T" = 1e12)
)
# Example function output
mydollars(df$sales)
[1] "$2 K" "$300 M" "$600 M" "$900 M" "$1.2 B" "$1.5 B"
这是使用上述帮助器的主要函数。我将 Dataframe 传递给函数,创建第2级". index"标签,然后对数字列进行分组和聚合,我将其添加"2"后缀,以便知道它是第二个数字。我的问题是从mydollars("{{agg_number}}2")
内部产生的。如果我将该代码替换为"Test String"
,我让功能发挥作用了。
treemap_index1 <- function(df, category1, category2, agg_number){
df_out <- df %>%
mutate("{{category2}}.index" := paste({{category2}}, mydollars({{agg_number}}), sep = "\n")) %>%
group_by({{category1}}) %>%
mutate("{{agg_number}}2" := sum({{agg_number}}),
"{{category1}}.index" := paste({{category1}},
mydollars("{{agg_number}}2"), # Code breaks on this line
sep = "\n")) %>%
print()
return(df_out)
}
treemap_index1(df, region, district, sales)
rlang::last_error()
<error/dplyr:::mutate_error>
Error in `mutate()`:
! Problem while computing `region.index = paste(region, mydollars("{{agg_number}}2"), sep = "\n")`.
ℹ The error occurred in group 1: region = "North".
Caused by error in `x * scale`:
! non-numeric argument to binary operator
---
Backtrace:
1. global treemap_index1(df, region, district, sales)
10. scales (local) mydollars("{{agg_number}}2")
11. scales::dollar(...)
12. scales::number(...)
13. scales:::scale_cut(...)
14. base::cut(...)
Run `rlang::last_trace()` to see the full context.
如果我如下所示替换了有问题的代码,那么函数就可以正常工作:
treemap_index2 <- function(df, category1, category2, agg_number){
df_out <- df %>%
mutate("{{category2}}.index" := paste({{category2}}, mydollars({{agg_number}}), sep = "\n")) %>%
group_by({{category1}}) %>%
mutate("{{agg_number}}2" := sum({{agg_number}}),
"{{category1}}.index" := paste({{category1}},
"Test String", # Temporarily replaced code
sep = "\n")) %>%
print()
return(df_out)
}
treemap_index2(df, region, district, sales)
# A tibble: 6 × 6
# Groups: region [2]
region district sales district.index sales2 region.index
<chr> <chr> <dbl> <chr> <dbl> <chr>
1 North Dist-1 2000 "Dist-1\n$2 K" 1800003600 "North\nTest String"
2 South Dist-2 300001600 "Dist-2\n$300 M" 2700002400 "South\nTest String"
3 North Dist-3 600001200 "Dist-3\n$600 M" 1800003600 "North\nTest String"
4 South Dist-4 900000800 "Dist-4\n$900 M" 2700002400 "South\nTest String"
5 North Dist-5 1200000400 "Dist-5\n$1.2 B" 1800003600 "North\nTest String"
6 South Dist-6 1500000000 "Dist-6\n$1.5 B" 2700002400 "South\nTest String"
谢谢你的帮助
我希望得到关于如何正确地将新变量名传递给helper函数的指导,并且由于我是数据屏蔽、引用、非标准评估的新手,因此任何其他关于如何做得更好的评论都是非常感谢的。
1条答案
按热度按时间mbzjlibv1#
改编莱昂内尔·亨利的the answer(@LionelHenry),一种选择是使用
rlang::englue
和.data
代词,如下所示: