如何在R中的函数之外访问函数的变量或参数?
Posted
技术标签:
【中文标题】如何在R中的函数之外访问函数的变量或参数?【英文标题】:How to access variable or argument of function outside the function in R? 【发布时间】:2020-09-27 08:53:50 【问题描述】:我正在创建一个绘图,并为它的数据准备创建了一个函数,该函数需要年份开始值和结束值作为函数的参数:
fn_gapminder_benchmark_diff <- function(year_start = 1952, year_end = 2007)
year_start = year_start
year_end = year_end
gapminder_joined %>%
filter(year %in% c(year_start,year_end)) %>%
arrange(country, year) %>%
group_by(country) %>%
mutate(benchmark_diff = benchmarked_india[2] - benchmarked_india[1],
max_pop = max(pop)) %>%
ungroup() %>%
arrange(benchmark_diff) %>%
filter(max_pop > 30000000) %>%
mutate(country = droplevels(country)) %>%
select(country, year, continent, benchmarked_india, benchmark_diff)
fn_gapminder_benchmark_diff(1987, 2007)
上面的函数在下面的代码中被调用
问题是我无法在图表的title
中使用上述函数arguments or variable values
来保持图表标题中的years
动态
# data function
fn_gapminder_benchmark_diff(1987, 2007) %>%
# data prep
mutate(country = fct_inorder(country)) %>%
group_by(country) %>%
mutate(benchmarked_end = benchmarked_india[2],
benchmarked_start = benchmarked_india[1] ) %>%
ungroup() %>%
# plotting
ggplot() +
geom_vline(xintercept = 0, col = "blue", alpha = 0.5) +
geom_label( label="India - As Benchamrking line", x=0, y="United States",
label.padding = unit(0.55, "lines"), # Rectangle size around label
label.size = 0.35, color = "black") +
geom_segment(aes(x = benchmarked_start, xend = benchmarked_end,
y = country, yend = country,
col = continent), alpha = 0.5, size = 7) +
geom_point(aes(x = benchmarked_india, y = country, col = continent), size = 9, alpha = .6) +
scale_color_brewer(palette = "Pastel2") +
labs(title = sprintf("GdpPerCapita Differenece with India (Starting point at %i and Ending at %i)",year_start, year_end),
subtitle = "Benchmarked India in blue line \nFor Countries with pop > 30000000 \n(Chart created by ViSa)",
col = "Continent", x = "GdpPerCap Difference at 1952 & 2007") +
# background & theme settings
theme_classic() +
theme(legend.position = "top",
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()
)
下面是我构建并尝试使其标题动态化的静态图表图像
【问题讨论】:
【参考方案1】:很遗憾,您的代码不可重现。也许下次使用reprex 包来获得更适合您需求的答案。
以iris
数据为例,我为您提供了一个遵循相同逻辑但在我看来相当脏的快速修复:
library(tidyverse)
data_prep <- function(data, species, min_sepal_w)
species <<- species # use superassignment operator to assign to global environment
min_sepal_w <<- min_sepal_w
data %>%
filter(species == species,
Sepal.Width >= min_sepal_w)
iris %>%
data_prep(species = "setosa", min_sepal_w = 3.0) %>%
ggplot(aes(Sepal.Width, Sepal.Length)) +
geom_point() +
labs(title = sprintf("Scatterplot of species %s with minimum sepal with of %i)", species, min_sepal_w))
由reprex package (v0.3.0) 于 2020 年 9 月 27 日创建
更简洁的解决方案是将信息存储在数据中:
data_prep <- function(data, species, min_sepal_w)
species <<- species
min_sepal_w <<- min_sepal_w
data %>%
filter(species == species,
Sepal.Width >= min_sepal_w) %>%
mutate(species = species,
min_sepal_w = min_sepal_w)
否则上面的代码可以保持不变。
【讨论】:
是的,这是一种更清洁的方式,感谢您帮助我解决这个问题并让我了解 reprex 包。将来肯定会使用reprex !!以上是关于如何在R中的函数之外访问函数的变量或参数?的主要内容,如果未能解决你的问题,请参考以下文章