knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(tor)
library(magrittr)

Introduction

I'm happy to announce that tor 1.1.1 in now on CRAN! tor (to-R) helps you to import multiple files at once into a list or an environment. It makes a small but frequent task less painful.

You can install tor with:

install.packages("tor")

List common files

list_csv() and friends import multiple common files from a directory into a list. They default to importing from the working directory.

dir()

list_csv()

You can specify a path to import from any directory. regexp, ignore.case, and invert allow you to pick specific files.

(path_mixed <- tor_example("mixed"))
dir(path_mixed)

list_rdata(
  path_mixed,
  regexp = "[.]RData",
  ignore.case = FALSE,
  invert = FALSE
)

Load common files

load_csv() and friends import files into an environment -- the global environment by default.

dir()

load_csv()

# Each dataframe is now available in the global environment
csv1
csv2

Beyond common files

list_any() imports files of any format into a list. You need to supply a function able to import a single file.

(path_csv <- tor_example("csv"))
dir(path_csv)

list_any(path_csv, readr::read_csv)

You may use anonymous functions using the formula syntax (powered by rlang).

(path_rdata <- tor_example("rdata"))
dir(path_rdata)

path_rdata %>%
  list_any(function(x) get(load(x)))

# Same
path_rdata %>%
  list_any(~ get(load(.x)))


maurolepore/tor documentation built on July 17, 2024, 12:03 a.m.