This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#install.packages(“officer”) library(ggplot2) library(officer) library(furrr) library(purrr) library(future)
generate_report <- function(i) { sub_df <- date_df[i, ]
# マップを作成 report_plot <- ggplot(data = sub_df) + geom_point(aes(x = lon, y = lat), size = 3, color = “blue”) + labs(title = paste(“Report for”, sub_df$date)) + theme_minimal()
# Wordドキュメントを作成 doc <- read_docx() %>% body_add_gg(value = report_plot, style = “Normal”) %>% body_add_par(value = as.character(sub_df$date), style = “heading 1”)
# ドキュメントを保存 file_name <- paste0(“report_”, format(sub_df\(date, "%Y-%m-%d"), ".docx") print(doc, target = file_name) paste(format(sub_df\)date, “%Y-%m-%d”), “.docx”) %>% print() Sys.sleep(0.1) # 疑似的な負荷 }
generate_report_batch <- function(indexes) { for (i in indexes) { generate_report(i) } }
dates <- seq(as.Date(“2023-01-01”), as.Date(“2023-12-31”), by=“day”)
date_df <- data.frame( date = dates, lon = runif(length(dates), min = -180, max = 180), lat = runif(length(dates), min = -90, max = 90) )
start_time_seq <- Sys.time() sequential_results <- map(seq_along(dates), generate_report) end_time_seq <- Sys.time() sequential_duration_seconds <- round(as.numeric(difftime(end_time_seq, start_time_seq, units = “secs”)), 1) print(paste(“Sequential processing duration:”, sequential_duration_seconds, “seconds”))
plan(multisession)
index_batches <- split(seq_along(dates), ceiling(seq_along(dates) / 10)) # 分割
start_time_par <- Sys.time() parallel_results <- future_map(index_batches, generate_report_batch) end_time_par <- Sys.time()
parallel_duration_seconds <- round(as.numeric(difftime(end_time_par, start_time_par, units = “secs”)), 1) print(paste(“Parallel processing duration:”, parallel_duration_seconds, “seconds”))