forked from business-science/free_r_tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
046_furrr_parallel_processing/046_furrr_parallel_processing.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# R TIPS ---- | ||
# TIP 046| furrr: Parallel Processing ---- | ||
# | ||
# 👉 For Weekly R-Tips, Sign Up Here: | ||
# https://mailchi.mp/business-science/r-tips-newsletter | ||
|
||
# furrr package: https://furrr.futureverse.org/ | ||
|
||
# LIBRARIES ---- | ||
|
||
library(tidyverse) | ||
library(timetk) | ||
library(lubridate) | ||
library(furrr) | ||
library(tictoc) | ||
|
||
# DATA ---- | ||
|
||
sales_data_tbl <- walmart_sales_weekly %>% | ||
select(id, Date, Weekly_Sales) %>% | ||
set_names(c("id", "date", "value")) | ||
|
||
# 1.0 PURRR ---- | ||
|
||
tic() | ||
sales_data_tbl %>% | ||
nest(data = c(date, value)) %>% | ||
mutate(model = map(data, function(df) { | ||
Sys.sleep(1) | ||
lm(value ~ month(date) + as.numeric(date), data = df) | ||
})) | ||
toc() | ||
|
||
# 2.0 FURRR (Parallel Processing) ---- | ||
plan(multisession, workers = 6) | ||
|
||
tic() | ||
sales_data_tbl %>% | ||
nest(data = c(date, value)) %>% | ||
mutate(model = future_map(data, function(df) { | ||
Sys.sleep(1) | ||
lm(value ~ month(date) + as.numeric(date), data = df) | ||
})) | ||
toc() | ||
|
||
# LEARNING MORE ---- | ||
|
||
# 5-COURSE R-TRACK | ||
# - Beginner to Expert in 6-months | ||
# https://university.business-science.io/p/5-course-bundle-machine-learning-web-apps-time-series/ | ||
|