如何使用 tidyevaluate 将对象名称作为字符串获取?
How to get object name as a string using tidyevaluate?
我需要将列名传递给绘图函数。问题是我的列名在参数中,我不知道如何将它们提取为文本。
这是有效的代码,但我需要从参数中动态获取字符串。
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)) %>%
ggsummarystats(., x = "COUNTRY", y="VALUE", # Need to get these from the arguments
palette = custom_pal,
ggfunc = ggboxplot,
color = "YEAR", #Same here
fill = "YEAR", #And here
summaries = c("n", "mean"))
}
create_plot(sales, YEAR, COUNTRY, VALUE)
使用 mtcars 的示例:
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)) %>%
ggsummarystats(., x = "carb", y="mpg",
palette = custom_pal,
ggfunc = ggboxplot,
fill = "gear",
color = "gear",
summaries = c("n", "mean"))
}
create_plot(mtcars, gear, carb, mpg)
您可以使用 rlang::as_name()
:
library(tidyverse)
library(rlang)
library(ggpubr)
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(
!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)
) %>%
ggsummarystats(
x = as_name(var_group),
y = as_name(var_y),
fill = as_name(var_subgroup),
ggfunc = ggboxplot,
summaries = c("n", "mean"))
}
create_plot(mtcars, gear, carb, mpg)
由 reprex package (v1.0.0)
于 2021 年 6 月 14 日创建
我需要将列名传递给绘图函数。问题是我的列名在参数中,我不知道如何将它们提取为文本。
这是有效的代码,但我需要从参数中动态获取字符串。
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)) %>%
ggsummarystats(., x = "COUNTRY", y="VALUE", # Need to get these from the arguments
palette = custom_pal,
ggfunc = ggboxplot,
color = "YEAR", #Same here
fill = "YEAR", #And here
summaries = c("n", "mean"))
}
create_plot(sales, YEAR, COUNTRY, VALUE)
使用 mtcars 的示例:
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)) %>%
ggsummarystats(., x = "carb", y="mpg",
palette = custom_pal,
ggfunc = ggboxplot,
fill = "gear",
color = "gear",
summaries = c("n", "mean"))
}
create_plot(mtcars, gear, carb, mpg)
您可以使用 rlang::as_name()
:
library(tidyverse)
library(rlang)
library(ggpubr)
create_plot <- function(df, group, subgroup, y){
var_group <- enquo(group)
var_subgroup <- enquo(subgroup)
var_y <- enquo(y)
df %>%
select(!!var_group, !!var_subgroup, !!var_y) %>%
mutate(
!!var_group := as.factor(!!var_group),
!!var_subgroup := as.factor(!!var_subgroup)
) %>%
ggsummarystats(
x = as_name(var_group),
y = as_name(var_y),
fill = as_name(var_subgroup),
ggfunc = ggboxplot,
summaries = c("n", "mean"))
}
create_plot(mtcars, gear, carb, mpg)
由 reprex package (v1.0.0)
于 2021 年 6 月 14 日创建