Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Commit

Permalink
Replaced apply functions with purrr::map functions, see #41
Browse files Browse the repository at this point in the history
I still need to replace some occurrences of mapply, but I don't know how to deal with nested purrr::map (whereas it works with mapply).
  • Loading branch information
Guillawme committed Mar 14, 2018
1 parent 24e6ba2 commit 9f871ac
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 37 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports: broom (>= 0.4.2),
jsonlite (>= 1.5),
magrittr (>= 1.5),
minpack.lm (>= 1.2-1),
purrr (>= 0.2.4),
readr (>= 1.1.1),
rlang (>= 0.1.4),
stringr (>= 1.2.0),
Expand Down
7 changes: 3 additions & 4 deletions R/fp_format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ fp_format_data <- function(input = NULL,

# Format data and return a single large dataframe
raw_data %>%
mapply(fp_format_one_dataset,
.,
names(raw_data),
SIMPLIFY = FALSE) %>%
purrr::map2(.x = .,
.y = names(raw_data),
.f = fp_format_one_dataset) %>%
dplyr::bind_rows() %>%
tidyr::drop_na()
}
Expand Down
16 changes: 7 additions & 9 deletions R/fp_inspect_raw_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ fp_inspect_raw_data <- function(raw_data,
# Split input data by Experiment and make plots for each dataset
results <- raw_data %>%
split(raw_data$Experiment) %>%
mapply(fp_inspect_one_dataset,
.,
MoreArgs = list(highest_signal = highest_signal),
SIMPLIFY = FALSE)
purrr::map(fp_inspect_one_dataset,
highest_signal = highest_signal)

# Optionally, write plots to files in the specified directory
if (!is.null(output_directory)) {
Expand All @@ -44,11 +42,11 @@ fp_inspect_raw_data <- function(raw_data,
message("Creating directory: ", output_directory)
dir.create(output_directory)
}
mapply(fp_save_inspection_plot,
results,
names(results),
MoreArgs = list(output_directory = output_directory,
plot_format = plot_format))
purrr::walk2(.x = results,
.y = names(results),
.f = fp_save_inspection_plots,
output_directory = output_directory,
plot_format = plot_format)
}

# Always return results
Expand Down
7 changes: 3 additions & 4 deletions R/fret_format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ fret_format_data <- function(input = NULL,

# Format data and return a single large dataframe
raw_data %>%
mapply(fret_format_one_dataset,
.,
names(raw_data),
SIMPLIFY = FALSE) %>%
purrr::map2(.x = .,
.y = names(raw_data),
.f = fret_format_one_dataset) %>%
dplyr::bind_rows()%>%
tidyr::drop_na()
}
Expand Down
16 changes: 7 additions & 9 deletions R/fret_inspect_raw_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ fret_inspect_raw_data <- function(raw_data,
# Split input data by Experiment and make plots for each dataset
results <- raw_data %>%
split(raw_data$Experiment) %>%
mapply(fret_inspect_one_dataset,
.,
MoreArgs = list(highest_signal = highest_signal),
SIMPLIFY = FALSE)
purrr::map(fret_inspect_one_dataset,
highest_signal = highest_signal)

# Optionally, write plots to files in the specified directory
if (!is.null(output_directory)) {
Expand All @@ -44,11 +42,11 @@ fret_inspect_raw_data <- function(raw_data,
message("Creating directory: ", output_directory)
dir.create(output_directory)
}
mapply(fret_save_inspection_plots,
results,
names(results),
MoreArgs = list(output_directory = output_directory,
plot_format = plot_format))
purrr::walk2(.x = results,
.y = names(results),
.f = fret_save_inspection_plots,
output_directory = output_directory,
plot_format = plot_format)
}

# Always return results
Expand Down
6 changes: 4 additions & 2 deletions R/load_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load_data <- function(input = NULL, skip_lines = NULL) {
# If no input is provided, ask for one
stop("You must specify an input: a directory name, a vector of file names, a dataframe, or a list of dataframes.")
} else if (is.list(input) && is.data.frame(input)) {
# If a single dataframe is provided, we put it in a named list so mapply
# If a single dataframe is provided, we put it in a named list so map
# can process it
raw_data <- list(input)
names(raw_data) <- deparse(substitute(input))
Expand Down Expand Up @@ -76,7 +76,9 @@ load_data <- function(input = NULL, skip_lines = NULL) {
#' }

read_files <- function(input = NULL, skip_lines = NULL) {
loaded_files <- lapply(input, readr::read_csv, skip = skip_lines)
loaded_files <- purrr::map(.x = input,
.f = readr::read_csv,
skip = skip_lines)
if(is.null(names(input))) {
# If the vector of files has no names attribute, use each corresponding
# file name without the .csv extension to name each element of the vector
Expand Down
13 changes: 4 additions & 9 deletions R/make_figure.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ make_figure <- function(fits,
output_directory = NULL,
plot_format = "png") {
# Get binding models used to obtain the input fits
binding_models <- vapply(fits,
detect_binding_model,
character(length = 1))
binding_models <- purrr::map_chr(fits, detect_binding_model)

# Generate plot for each input model
figures <- mapply(make_one_figure,
fit = fits,
experiment_name = names(fits),
binding_model = binding_models,
MoreArgs = list(probe_conc = probe_concentration),
SIMPLIFY = FALSE)
figures <- purrr::pmap(.l = list(fits, names(fits), binding_models),
.f = make_one_figure,
probe_conc = probe_concentration)

# Optionally, write plots to files in the specified directory
if (!is.null(output_directory)) {
Expand Down

0 comments on commit 9f871ac

Please sign in to comment.