如何访问 R 函数外部的变量或函数参数?
How to access variable or argument of function outside the function in R?
我正在创建一个绘图,为了准备数据,我创建了一个需要年份开始值和结束值作为函数参数的函数:
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()
)
下面是我构建的静态图表的图像,并试图使其标题动态化
很遗憾,您的代码不可重现。也许下次使用 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-09-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)
}
否则上面的代码可以保持不变。
我正在创建一个绘图,为了准备数据,我创建了一个需要年份开始值和结束值作为函数参数的函数:
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()
)
下面是我构建的静态图表的图像,并试图使其标题动态化
很遗憾,您的代码不可重现。也许下次使用 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-09-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)
}
否则上面的代码可以保持不变。