Many of the functions in the workloopR package are built to facilitate batch processing of workloop and related data files. This vignette will start with an overview of how the functions were intended to be used for batch processing and then provide specific examples.

Conceptual overview

We generally expect a single file to store data from a single experimental trial, whereas directories hold data from all the trials of a single experiment. Accordingly, the muscle_stim objects created and used by most of the workloopR functions are intended to hold data from a single trial of a workloop or related experiment. Lists are then used to package together trials from a single experiment. This also lends itself to using recursion to transform and analyze all data from a single experiment.

In broad strokes, there are three ways that batch processing has been worked into workloopR functions. First, some functions like the *_dir() family of import functions and summarize_wl_trials() specifically generate or require lists of muscle_stim objects. Second, the first argument of all other functions are the objects being manipulated, which can help clean up recursion using the purrr::map() family of functions. Finally, some functions return summarized data as single rows of a data.frame that can easily be bound together to generate a summary table.

Load packages and data

This vignette will rely heavily on the purrr::map() family of functions for recursion, though it should be mentioned that the base::apply() family of functions would work as well.

Necessarily-multi-trial functions

*_dir() functions

Both read_ddf() and read_analyze_wl() have alternatives suffixed by _dir() to read in multiple files from a directory. Both take a path to the directory and an optional regular expression to filter files by and return a list of muscle_stim objects or analyzed_workloop objects, respectively.

The sort_by argument can be used to rearrange this list by any attribute of the read-in objects. By default, the objects are sorted by their modification time. Other arguments of read_ddf() and read_analyze_wl() can also be passed to their *_dir() alternatives as named arguments.

Summarizing workloop trials

In a series of workloop trials, it can useful to see how mean power and work change as you vary different experimental parameters. To facilitate this, summarize_wl_trials() specifically takes a list of analyzed_workloop objects and returns a data.frame of this information. We will explore ways of generating lists of analyzed workloops without using read_analyze_wl_dir() in the following section.

Manual recursion examples

Batch import for non-ddf data

One of the more realistic use cases for manual recursion is for importing data from multiple related trials that are not stored in ddf format. As with importing individual non-ddf data sources, we start by reading the data into a data.frame, only now we want a list of data.frames. In this example, we will read in csv files and stitch them into a list using purrr::map()

Data transformation and analysis

Applying a constant transformation to a list of muscle_stim objects is fairly straightforward using purrr::map().

non_ddf_list<-
  non_ddf_list %>%
  map(~{
    attr(.x,"stimulus_width")<-0.2
    attr(.x,"stimulus_offset")<-0.1
    return(.x)
  }) %>%
  map(fix_GR,2)

Applying a non-constant transformation like setting a unique file ID can be done using purrr::map2().

file_ids<-paste0("0",1:4,"-",2:5,"mA-twitch.csv")

non_ddf_list<-
  non_ddf_list %>%
  map2(file_ids, ~{
    attr(.x,"file_id")<-.y
    return(.x)
  })

non_ddf_list
#> [[1]]
#> # Twitch Data: 3 channels recorded over 0.4001s
#> File ID: 01-2mA-twitch.csv
#> 
#>    Time  Position   Force Stim
#> 1 1e-04 -3.002651 474.262    0
#> 2 2e-04 -3.001682 471.682    0
#> 3 3e-04 -3.001360 472.650    0
#> 4 4e-04 -3.000554 471.037    0
#> 5 5e-04 -3.001199 472.004    0
#> 6 6e-04 -3.001360 472.327    0
#> # … with 3995 more rows
#> 
#> [[2]]
#> # Twitch Data: 3 channels recorded over 0.4001s
#> File ID: 02-3mA-twitch.csv
#> 
#>    Time  Position   Force Stim
#> 1 1e-04 -3.002489 476.520    0
#> 2 2e-04 -3.001199 475.553    0
#> 3 3e-04 -3.000876 474.585    0
#> 4 4e-04 -3.001199 473.940    0
#> 5 5e-04 -3.001199 474.262    0
#> 6 6e-04 -3.001360 474.262    0
#> # … with 3995 more rows
#> 
#> [[3]]
#> # Twitch Data: 3 channels recorded over 0.4001s
#> File ID: 03-4mA-twitch.csv
#> 
#>    Time  Position   Force Stim
#> 1 1e-04 -3.002651 451.037    0
#> 2 2e-04 -3.001360 449.747    0
#> 3 3e-04 -3.000715 449.747    0
#> 4 4e-04 -3.001037 449.424    0
#> 5 5e-04 -3.000876 449.424    0
#> 6 6e-04 -3.001521 450.069    0
#> # … with 3995 more rows
#> 
#> [[4]]
#> # Twitch Data: 3 channels recorded over 0.4001s
#> File ID: 04-5mA-twitch.csv
#> 
#>    Time  Position   Force Stim
#> 1 1e-04 -3.002327 446.521    0
#> 2 2e-04 -3.001521 445.876    0
#> 3 3e-04 -3.001199 445.876    0
#> 4 4e-04 -3.001199 445.876    0
#> 5 5e-04 -3.001360 445.553    0
#> 6 6e-04 -3.001037 446.199    0
#> # … with 3995 more rows

Analysis can similarly be run recursively. isometric_timing() in particular returns a single row of a data.frame with timings and forces for key points in an isometric dataset. Here we can use purrr::map_dfr() to bind the rows together for neatness.