在 R 中如何在绘图的两个内部函数之间传递 dataframe/tibble 然后保存绘图?

In R how to pass dataframe/tibble between two inner functions for a plot and then save the plot?

我已经为 运行 3 个内部函数创建了一个外部函数用于数据预处理,然后绘图并保存它。

在与下一个内部函数共享 1 个内部函数的数据帧输出时面临问题

library(tidyverse)
library(glue)

gapminder <- read.csv("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder-FiveYearData.csv")

外函数

fn_run_all <-function(bench_country = India, year_start = 1952, year_end = 2007){
  
  
  year_start = year_start
  year_end = year_end
  
  # Function1 for PreProcess Benchmarked Country
  fn_benchmark_country({{bench_country}}) 

  # Function2 to PreProcess dates & pass df to function3 to plot  
  Plot_final <- fn_year_filter(gapminder_joined, year_start, year_end) %>% 
  
                # function 3 to plot  
                fn_create_plot(., year_start, year_end, {{bench_country}})
  
  
  # Saving plot
  jpeg(file="gdp_benchmarked_FinalPlot.jpeg", width = 1400, height = 1800)
  Plot_final
  dev.off()
  
  # Printing Plot
  Plot_final
  }

fn_run_all(India, 1952, 2002)

第 1 期

如果我不输出数据帧,即第一个函数 fn_benchmark_country()gapminder_joinedglobal,那么我在函数 2 fn_year_filter() 中出现错误。

但是当我将 function1 的输出即 gapminder_joined 推送到 Global 时,它就可以工作了。

Is it necessary to make it global even when I am returning it within the outer function or there is some other alternative way?

Function1 参考代码

fn_benchmark_country  <- function(bench_country = India){
  
  bench_country = enquo(bench_country)

  gapminder_benchmarked_wider <- gapminder %>% 
                                  select(country, year, gdpPercap) %>% 
                                  pivot_wider(names_from = country, values_from = gdpPercap) %>% 
                                  arrange(year) %>% 
                                  # map_dbl( ~{.x - India })
                                  mutate(across(-1, ~ . - !!bench_country))
                                  
  # Reshaping back to Longer
  gapminder_benchmarked_longer <- gapminder_benchmarked_wider %>% 
                                  pivot_longer(cols = !year, names_to = "country", values_to = "benchmarked") 
 
  # Joining tables
  gapminder_joined <- left_join(x = gapminder, y = gapminder_benchmarked_longer, by = c("year","country"))

  # converting to factor
  gapminder_joined$country <- as.factor(gapminder_joined$country)
  
  # gapminder_joined <<- gapminder_joined
  return(gapminder_joined)
}

在上面的代码中,如果我取消对最后一行的注释,那么它就可以工作了。

Function2 参考代码

fn_year_filter  <- function(gapminder_joined, year_start, year_end){

                              gapminder_joined %>% 
                              filter(year %in% c(year_start,year_end)) %>% 
                              
                              arrange(country, year) %>% 
                              
                              group_by(country) %>% 
                              
                              mutate(benchmark_diff = benchmarked[2] - benchmarked[1],
                                     max_pop = max(pop)) %>% 
                              
                              ungroup() %>% 
                              
                              arrange(benchmark_diff) %>% 
                              
                              filter(max_pop > 30000000) %>% 
                              
                              mutate(country = droplevels(country)) %>% 
                              select(country, year, continent, benchmarked, benchmark_diff) %>% 
                                
                              # --- 
                              mutate(country = fct_inorder(country)) %>% 
                              
                              group_by(country) %>% 
                                
                              mutate(benchmarked_end = benchmarked[2],
                                     benchmarked_start = benchmarked[1]  ) %>% 
                              
                              ungroup() 

  }

不要从函数内部更新变量。您采用的方法是正确的,因此无需使用 <<-,保持 fn_benchmark_country 不变。尝试在函数中保存返回的数据帧。

fn_run_all <-function(bench_country = India, year_start = 1952, year_end = 2007){
  
  # Function1 for PreProcess Benchmarked Country
  gapminder_joined <- fn_benchmark_country({{bench_country}}) 
  # Function2 to PreProcess dates & pass df to function3 to plot  
  Plot_final <- fn_year_filter(gapminder_joined, year_start, year_end) %>% 
    # function 3 to plot  
   fn_create_plot(., year_start, year_end, {{bench_country}})
   #...rest of the code
   #...
   #...
  
}