Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 03f4bd3

Browse files
committed
Merged TimBMK/furrr-support into TimBMK-furrr-support
2 parents 39575d7 + c333c81 commit 03f4bd3

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

R/bind_tweets.R

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#' By default, `bind_tweets` binds into a data frame containing tweets (from data_*id*.json files).
66
#'
77
#' If users is TRUE, it binds into a data frame containing user information (from users_*id*.json).
8+
#'
9+
#' For the "tidy" format, parallel processing with furrr is supported. In order to enable parallel processing, workers need to be set manually through [future::plan()]. See examples
810
#'
911
#' @param data_path string, file path to directory of stored tweets data saved as data_*id*.json and users_*id*.json
1012
#' @param user If `FALSE`, this function binds JSON files into a data frame containing tweets; data frame containing user information otherwise. Ignore if `output_format` is not NA
@@ -16,9 +18,6 @@
1618
#' \item{"tidy"}{Tidy format; all essential columns are available}
1719
#' }
1820
#'
19-
#' @param parallel_workers Number of threads used for parallel processing. Defaults to all detected threads. Only supported if the output_format is "tidy"
20-
#' @param auto_set_plan Should the parallelization plan be set automatically? If True, the function automatically sets up (and ends) a multisession with the specified amount of parallel workers. If False, a future::plan() session needs to be set up manually for parallelization
21-
#'
2221
#' @return a data.frame containing either tweets or user information
2322
#' @export
2423
#'
@@ -32,10 +31,19 @@
3231
#'
3332
#' # bind json files in the directory "data" into a "tidy" data frame / tibble
3433
#' bind_tweets(data_path = "data/", user = TRUE, output_format = "tidy")
34+
#'
35+
#' # bind json files in the directory "data" into a "tidy" data frame / tibble with parallel computing
36+
#' ## set up a multisession
37+
#' future::plan("multisession")
38+
#' ## run the function - note that no additional arguments are required
39+
#' bind_tweets(data_path = "data/", user = TRUE, output_format = "tidy")
40+
#' ## Shut down parallel workers
41+
#' future::plan("sequential")
3542
#' }
36-
bind_tweets <- function(data_path, user = FALSE, verbose = TRUE, output_format = NA, parallel_workers = parallel::detectCores(), auto_set_plan = TRUE) {
43+
#'
44+
bind_tweets <- function(data_path, user = FALSE, verbose = TRUE, output_format = NA) {
3745
if (!is.na(output_format)) {
38-
return(.flat(data_path, output_format = output_format, parallel_workers = parallel_workers, auto_set_plan = auto_set_plan))
46+
return(.flat(data_path, output_format = output_format))
3947
}
4048
if(user) {
4149
files <- ls_files(data_path, "^users_")
@@ -137,18 +145,14 @@ convert_json <- function(data_file, output_format = "tidy") {
137145
return(file.path(dirname(data_filename), paste0("users_", ids, ".json")))
138146
}
139147

140-
.flat <- function(data_path, output_format = "tidy", parallel_workers, auto_set_plan) {
148+
.flat <- function(data_path, output_format = "tidy") {
141149
if (!output_format %in% c("tidy", "raw")) {
142150
stop("Unknown format.", call. = FALSE)
143151
}
144152
data_files <- ls_files(data_path, "^data_")
145153
if (output_format == "raw") {
146154
return(convert_json(data_files, output_format = "raw"))
147155
}
148-
if (auto_set_plan == TRUE && parallel_workers > 1) {
149-
session_plan <- future::plan(future::multisession, workers = parallel_workers)
150-
on.exit(future::plan(session_plan))
151-
}
152156
return(furrr::future_map_dfr(data_files, convert_json, output_format = output_format))
153157
}
154158

man/bind_tweets.Rd

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)