MyNixOS website logo
Description

A Collection of Helper and Wrapper Functions.

Helper functions to easily add functionality to functions. The package can assign functions to have an lazy evaluation allowing you to save and update the arguments before and after each function call. You can set a temporary working directory within functions and wrap console messages around other functions.

wrappr

R-CMD-check Codecov testcoverage

wrappr is a collection of wrapper and helper functions that can add extra functionality to other functions and support cleaner dry code.

Here is a brief description of some functionality the wrappr package has to offer:

  • Set up functions that can be called later keeping a copy of the arguments. Flexibility to update or add arguments and change the function to call.

  • Set a temporary working directory to work with a function that deals with I/O to maintain the current working directory.

  • Get an existing variable from the environment or create a new variable if it does not exist. Great for loading large files once when re-running development code.

  • Print a messages to the console before and after a function call.

  • Combine functions into a single function.

Installation

You can install the development version of wrappr from GitHub with:

# install.packages("devtools")
devtools::install_github("John-Piper/wrappr")

Usage

The examples below show how each function from wrappr could be used to add functionality to other functions.

library(wrappr)


# Set up a function closure with a function and arguments set up to use later in the code.

csv_file_loader <- wrappr::lazy_eval(
                                     file = "some/example/path/file_name_one.csv",
                                     sep = "|,
                                     .f = read.csv
                                     )


# lots of code here.

# even more code here.

# a little extra code here.


# call the function when it is needed in the code.

df_one <- csv_file_loader()

# use the same function closure to load a different file with the same arguments used previously.

df_two <- csv_file_loader(file = "some/example/path/file_name_two.csv")


#-----------------------------------------------------------------------------------------------------


# Assign to a variable using a value from an existing variable from the environment or create a new value.

# This example code will be helpful to use when developing code
# and you require loading a big data file into the environment.

# The read.csv function in the example below will not load if the variable in the param `var` is still in the environment scope.

df_big_file <- wrappr::get_cache_or_create(
                                           var = "df_big_file",
                                           func = read.csv,
                                           file = "some/example/path/file_name.csv"
                                          )


#-----------------------------------------------------------------------------------------------------


# load and save a file using a temporary working directory to keep the existing working directory.

df_example <- wrappr::set_temp_wd(
                    temp_cwd = "temp_wd/load/folder_one/",
                    func = read.csv,
                    file = "example_file.csv"
                    )


# code here to change the df.

# saving the file.

wrappr::set_temp_wd(
                    temp_cwd = "temp_wd/save/folder_one/",
                    func = write.csv,
                    df_example,
                    file = "df_example.csv"
                    )
                    

#-----------------------------------------------------------------------------------------------------


# write a message to the console before and after a function call saving the output to a variable.

output_df <- wrappr::msg_wrap(
                              func = read.csv,
                              file = "path/to/example_workbook.csv",
                              sep = "|",
                              before_func_msg = "Loading the data.",
                              after_func_msg = "The data has loaded..",
                              )
                              

#-----------------------------------------------------------------------------------------------------


# How to use the package to decorate a function using the main functions from wrappr.


load_data_func <- wrappr::lazy_eval(
                                    file = "data_file.csv",
                                    sep = "|",
                                    .f = read.csv
                                   )

load_data_func_temp_wd <- wrappr::lazy_eval(
  temp_cwd = "temp/working/dir",
  func = load_data_func,
  .f = wrappr::set_temp_wd
)

load_func_with_msg <- wrappr::lazy_eval(
  func = load_data_func_temp_wd,
  before_func_msg = "Loading the data.",
  after_func_msg = "The data has loaded..",
  .f = wrappr::msg_wrap
)

df <- load_func_with_msg()


# using the pipe symbol from the package magrittr to creare the same function.
# `.` symbol used to place the return value from the previous function into the new function.

library(magrittr)

load_data_func_pipe <- wrappr::lazy_eval(
                                    file = "data_file.csv",
                                    sep = "|",
                                    .f = read.csv
                                        ) %>%
                      wrappr::lazy_eval(
                                    temp_cwd = "temp/working/dir",
                                    func = .,
                                    .f = wrappr::set_temp_wd
                                       ) %>%
                      wrappr::lazy_eval(
                                    func = .,
                                    before_func_msg = "Loading the data.",
                                    after_func_msg = "The data has loaded..",
                                    .f = wrappr::msg_wrap
                                     )

df_with_pipe <- load_data_func_pipe()


#-----------------------------------------------------------------------------------------------------
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows