purrr

repository·main·Indexed 23 days ago

https://github.com/tidyverse/purrr

A functional programming toolkit for R that provides a consistent set of tools for working with functions and vectors, primarily through the map() family of functions. It features pipe-friendly, type-stable functions that support anonymous functions, progress tracking, and parallelization via in_parallel().

Tokens
2.3K
Snippets
17
Records
18
Agent score
30%

What's inside purrr

  1. Use map() functions for functional programming

    main

    The map() family of functions allows you to replace for loops with succinct, readable code for working with functions and vectors.

    Key characteristics of purrr functions:

    • Pipe-friendly: The first argument is always the data, making them compatible with the native R pipe |>.
    • Type-stable: Functions like map() always return lists, while type-specific versions like map_dbl() always return double vectors (or throw an error if the type doesn't match).
    • Flexible inputs: Functions accept named functions, anonymous functions (lambdas), character vectors (for extraction by name), or numeric vectors (for extraction by position).
    • Progress tracking: All map() functions include a .progress argument to monitor long-running jobs.
    • Parallelization: All map() functions work with in_parallel() to distribute computation across multiple cores or machines.
    library(purrr)
    
    mtcars |> 
      split(mtcars$cyl) |>  # from base R
      map(\(df) lm(mpg ~ wt, data = df)) |> 
      map(summary) |> 
      map_dbl("r.squared")
    #>         4         6         8 
    #> 0.5086326 0.4645102 0.4229655
  2. Install purrr

    main

    You can install purrr as part of the full tidyverse suite, as a standalone package from CRAN, or use the development version from GitHub via pak.

    # The easiest way to get purrr is to install the whole tidyverse:
    install.packages("tidyverse")
    
    # Alternatively, install just purrr:
    install.packages("purrr")
    
    # Or the the development version from GitHub:
    # install.packages("pak")
    pak::pak("tidyverse/purrr")
  3. Troubleshoot vignette re-building errors in 'DataSimilarity'

    main

    The package DataSimilarity (version 0.3.0) fails installation because it cannot re-build its vignettes. The error specifically identifies that GettingStarted.Rnw and Details.Rnw failed to process. This typically indicates an issue with the LaTeX environment or the Rnw file content during the vignette build process.

    --- failed re-building ‘GettingStarted.Rnw’
    
    SUMMARY: processing the following files failed:
      ‘Details.Rnw’ ‘GettingStarted.Rnw’
    
    Error: Vignette re-building failed.
    Execution halted
  4. Troubleshoot missing dependency error in semidist

    main

    The semidist package (both Devel and CRAN versions) may fail during the dependency check phase if a required package is not available in the environment. Specifically, the error indicates that the energy package is required but cannot be found.

    Error Signature:

    * checking package dependencies ... ERROR
    Package required but not available: ‘energy'
  5. Troubleshoot dependency errors in 'cylcop'

    main

    The package cylcop (version 0.2.1) fails installation in both Devel and CRAN environments due to a missing dependency: copula. Ensure copula is installed in your R environment to fix this error.

    Package required but not available: ‘copula’
  6. Troubleshoot version and suggestion errors in 'deepSTRAPP'

    main

    The package deepSTRAPP (version 1.0.0) encounters two types of dependency issues during installation:

    1. Unsuitable Version: The required package methods is available but does not meet the version requirements specified in the package DESCRIPTION.
    2. Missing Suggestion: The suggested package BioGeoBEARS is not available for checking.

    To fix, update the methods package to a compatible version and ensure BioGeoBEARS is available if testing suggested dependencies.

    Package required and available but unsuitable version: ‘methods’
    
    Package suggested but not available for checking: ‘BioGeoBEARS’
  7. Troubleshoot vignette re-building failures in ffp

    main

    The package ffp fails during the installation/check process because it cannot re-build its vignette files. This prevents the package from passing the check.

    Error message: Error: Vignette re-building failed.

    --- failed re-building ‘views.Rmd’
    
    SUMMARY: processing the following file failed:
      ‘views.Rmd’
    
    Error: Vignette re-building failed.
    Execution halted
  8. Troubleshoot test failures in gratia

    main

    The package gratia fails installation due to test failures related to specific SVG files in the soap-films directory. This indicates that the expected output from certain tests does not match the actual output.

    Error message: ! Test failures.

    'soap-films/draw-smooth-estimates-so-soap-film-bndry.svg', and
    'soap-films/draw-smooth-estimates-so-soap-film.svg'
    Error:
    ! Test failures.
    Execution halted
  9. Troubleshoot missing dependencies for metrica

    main

    The metrica package (v2.1.1) fails installation in both Devel and CRAN environments due to a missing dependency. To resolve this, ensure the following package is installed:

    • energy
    Package required but not available: ‘energy’
  10. Troubleshoot missing dependencies for measureR

    main

    The measureR package (v0.0.2) fails installation in both Devel and CRAN environments because required dependencies are missing. To resolve this, ensure the following packages are installed:

    • semPlot
    • semptools
    Packages required but not available: 'semPlot', 'semptools'
  11. Troubleshoot dependency errors in ggsem and lcsm

    main

    The packages ggsem and lcsm fail installation due to a missing dependency. The error indicates that the package semPlot is required but not available in the environment during the check process.

    Error message: Package required but not available: ‘semPlot’

    Package required but not available: ‘semPlot’
  12. Troubleshoot dependency errors in 'apa'

    main

    The package apa (version 0.3.5) fails installation in both Devel and CRAN environments because the required dependency MBESS is not available. To resolve this, ensure that MBESS is installed and accessible in the R environment before attempting to install apa.

    Package required but not available: ‘MBESS’