Skip to contents

This is a wrapper function for extracting one or more variables and objects declared from within the multiverse in a tidy format.

Usage

extract_variables(x, ..., .results = .results)

# S3 method for class 'multiverse'
extract_variables(x, ..., .results = .results)

# S3 method for class 'data.frame'
extract_variables(x, ..., .results = .results)

Arguments

x

either a multiverse object or a dataframe (created using expand()) from a multiverse object. See usage.

...

one or more variable (or object) names to be extracted from the multiverse object. The names can be quoted or unquoted.

.results

(Optional) if the .results column which stores the environments for each unique analysis has been changed, specify the new name of the column. Defaults to .results

Details

In a typical analysis, the user will declare variables and objects inside the multiverse object. However, it might be difficult to access the variables and objects, hence we provide convenient wrappers in the form of extract_variables().

If the user wants to extract one or more literals (strings, integers, doubles or logicals of length 1) then each variables is separated out into its own column. If the user wants to extract one or more vectors (or lists) then each such variable will be extracted in its own list column. If the user wants to extract one or more dataframe then they a column of type data frame (or tibble) would be created (which is a special instance of a list column).

Examples

# \donttest{
library(dplyr)

M <- multiverse()

inside(M, {
  data <- rnorm(100, 50, 20)

  x.mean <- mean(data, trim = branch(
    trim_values,
    "trim_none" ~ 0,
    "trim_1pc" ~ 0.05,
    "trim_5pc" ~ 0.025,
    "trim_10pc" ~ 0.05
  ))
  
  y <- sd(data)
})

# Extracts the relevant variable from the multiverse
M %>%
  extract_variables(x.mean)
#> # A tibble: 4 × 7
#>   .universe trim_values .parameter_assignment .code            .results .errors
#>       <int> <chr>       <list>                <list>           <list>   <list> 
#> 1         1 trim_none   <named list [1]>      <named list [1]> <env>    <lgl>  
#> 2         2 trim_1pc    <named list [1]>      <named list [1]> <env>    <lgl>  
#> 3         3 trim_5pc    <named list [1]>      <named list [1]> <env>    <lgl>  
#> 4         4 trim_10pc   <named list [1]>      <named list [1]> <env>    <lgl>  
#> # ℹ 1 more variable: x.mean <dbl>

# if you want to filter the multiverse before extracting variables from it
# you ca first create the table and manipulate it before extracting variables
expand(M) %>%
  extract_variables(x.mean)
#> Error in UseMethod("expand"): no applicable method for 'expand' applied to an object of class "multiverse"

# you can extract more than one variable from the multiverse simultaneously
# these variables will be new columns in the dataset
expand(M) %>%
  extract_variables(x.mean, y)
#> Error in UseMethod("expand"): no applicable method for 'expand' applied to an object of class "multiverse"
# }