From 4f118e94316047afc59dc7789de9041253c759be Mon Sep 17 00:00:00 2001 From: Mauro Lepore Date: Tue, 8 Jan 2019 18:41:11 -0500 Subject: [PATCH] Use readr (#10) Use readr. This adds a heavy dependency but substantially increases safety. --- DESCRIPTION | 3 +- NEWS.md | 3 + R/format_path.R | 6 +- R/list_any.R | 16 ++-- R/list_csv.R | 58 +++----------- R/load_csv.R | 47 ++---------- R/tor_example.R | 4 +- README.md | 32 +++++++- docs/index.html | 108 ++++++++++++++++----------- docs/news/index.html | 6 ++ docs/reference/format_path.html | 12 ++- docs/reference/list_csv.html | 82 +++++--------------- docs/reference/load_csv.html | 78 +++---------------- inst/extdata/tsv/tsv1.tsv | 2 +- inst/extdata/tsv/tsv2.tsv | 2 +- man/list_csv.Rd | 47 +----------- man/load_csv.Rd | 49 +----------- tests/testthat/{csv.csv => csv1.csv} | 0 tests/testthat/csv2.csv | 3 + tests/testthat/file1.csv | 3 - tests/testthat/file2.csv | 3 - tests/testthat/test-list_csv.R | 3 +- tests/testthat/test-load_csv.R | 14 +--- 23 files changed, 189 insertions(+), 392 deletions(-) rename tests/testthat/{csv.csv => csv1.csv} (100%) create mode 100644 tests/testthat/csv2.csv delete mode 100644 tests/testthat/file1.csv delete mode 100644 tests/testthat/file2.csv diff --git a/DESCRIPTION b/DESCRIPTION index 8a0583c..867f8a2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,8 @@ BugReports: https://github.com/maurolepore/tor/issues Imports: fs, rlang, - tibble + tibble, + readr Suggests: covr, spelling, diff --git a/NEWS.md b/NEWS.md index 2aa444e..b8dd997 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,9 @@ ## Minor +* `list_any()`, `list_csv()`, `list_tsv()`, `load_csv()`, and `load_tsv()` now use __readr__. + * This makes reading data faster and safer. + * Each dataframe output is now converted to tibble. * Users no longer need to call `tibble::as_tibble()`. diff --git a/R/format_path.R b/R/format_path.R index b6f9817..48eb374 100644 --- a/R/format_path.R +++ b/R/format_path.R @@ -10,11 +10,11 @@ #' #' @examples #' format_path(c("file1", "file2"), "csv") -#' +#' #' (dfs <- list_csv(tor_example("csv"))) -#' +#' #' format_path(names(dfs), "csv") -#' +#' #' format_path(names(dfs), "csv", base = "home", prefix = "this-") #' @family helpers #' @export diff --git a/R/list_any.R b/R/list_any.R index b6c43af..d3eea1a 100644 --- a/R/list_any.R +++ b/R/list_any.R @@ -8,23 +8,23 @@ #' #' @examples #' tor_example() -#' +#' #' path <- tor_example("csv") #' dir(path) -#' +#' #' list_any(path, read.csv) -#' +#' #' list_any(path, ~ read.csv(.x, stringsAsFactors = FALSE)) -#' +#' #' (path_mixed <- tor_example("mixed")) #' dir(path_mixed) -#' +#' #' list_any( #' path_mixed, ~ get(load(.x)), #' regexp = "[.]csv$", #' invert = TRUE #' ) -#' +#' #' list_any( #' path_mixed, ~ get(load(.x)), #' "[.]Rdata$", @@ -46,9 +46,7 @@ list_any <- function(path = ".", ) if (length(files) == 0) { - abort( - sprintf("Can't find files matching '%s' in:\n '%s'", regexp, path) - ) + abort(sprintf("Can't find files matching '%s' in:\n '%s'", regexp, path)) } file_names <- fs::path_ext_remove(fs::path_file(files)) diff --git a/R/list_csv.R b/R/list_csv.R index f10c12d..7ff5f56 100644 --- a/R/list_csv.R +++ b/R/list_csv.R @@ -3,29 +3,29 @@ #' These functions wrap the most common special cases of [list_any()]. #' #' @inheritParams list_any -#' @inheritParams utils::read.table -#' @param ... Arguments passed to [utils::read.table()]. +#' @inheritParams readr::read_delim +#' @param ... Arguments passed to `readr::read_csv()` or `readr::read_tsv()`. #' #' @return A list. #' #' @examples #' (rds <- tor_example("rds")) #' dir(rds) -#' +#' #' list_rds(rds) -#' +#' #' (tsv <- tor_example("tsv")) #' dir(tsv) -#' +#' #' list_tsv(tsv) -#' +#' #' (mixed <- tor_example("mixed")) #' dir(mixed) -#' +#' #' list_rdata(mixed) -#' +#' #' list_csv(mixed) -#' +#' #' list_rdata(mixed, regexp = "[.]RData", ignore.case = FALSE) #' @family general functions to import data #' @export @@ -33,28 +33,10 @@ list_csv <- function(path = ".", regexp = "[.]csv$", ignore.case = TRUE, invert = FALSE, - header = TRUE, - sep = ",", - quote = "\"", - dec = ".", - fill = TRUE, - comment.char = "", - stringsAsFactors = FALSE, - na.strings = c("", "NA"), ...) { list_any( path, - function(x) utils::read.csv( - file = x, - header = header, - sep = sep, - quote = quote, - dec = dec, - fill = fill, - comment.char = comment.char, - stringsAsFactors = stringsAsFactors, - na.strings = na.strings - ), + readr::read_csv, regexp = regexp, ignore.case = ignore.case, invert = invert, @@ -68,28 +50,10 @@ list_tsv <- function(path = ".", regexp = "[.]tsv$", ignore.case = TRUE, invert = FALSE, - header = TRUE, - sep = "\t", - quote = "\"", - dec = ".", - fill = TRUE, - comment.char = "", - stringsAsFactors = FALSE, - na.strings = c("", "NA"), ...) { list_any( path, - function(x) utils::read.csv( - file = x, - header = header, - sep = sep, - quote = quote, - dec = dec, - fill = fill, - comment.char = comment.char, - stringsAsFactors = stringsAsFactors, - na.strings = na.strings - ), + .f = readr::read_tsv, regexp = regexp, ignore.case = ignore.case, invert = invert, diff --git a/R/load_csv.R b/R/load_csv.R index af9a2c7..9866913 100644 --- a/R/load_csv.R +++ b/R/load_csv.R @@ -8,15 +8,15 @@ #' @examples #' (path_csv <- tor_example("csv")) #' dir(path_csv) -#' +#' #' load_csv(path_csv) #' # Each dataframe is now available in the global environment #' csv1 #' csv2 -#' +#' #' (path_mixed <- tor_example("mixed")) #' dir(path_mixed) -#' +#' #' load_rdata(path_mixed) #' # Each dataframe is now available in the global environment #' lower_rdata @@ -27,30 +27,11 @@ load_csv <- function(path = ".", regexp = "[.]csv$", ignore.case = TRUE, invert = FALSE, - header = TRUE, - sep = ",", - quote = "\"", - dec = ".", - fill = TRUE, - comment.char = "", - stringsAsFactors = FALSE, - na.strings = c("", "NA"), envir = .GlobalEnv, ...) { lst <- list_any( path, - # TODO: Use function(x) to more obviously show what's going on - function(x) utils::read.csv( - file = x, - header = header, - sep = sep, - quote = quote, - dec = dec, - fill = fill, - comment.char = comment.char, - stringsAsFactors = stringsAsFactors, - na.strings = na.strings - ), + readr::read_csv, regexp = regexp, ignore.case = ignore.case, invert = invert, @@ -67,29 +48,11 @@ load_tsv <- function(path = ".", regexp = "[.]tsv$", ignore.case = TRUE, invert = FALSE, - header = TRUE, - sep = "\t", - quote = "\"", - dec = ".", - fill = TRUE, - comment.char = "", - stringsAsFactors = FALSE, - na.strings = c("", "NA"), envir = .GlobalEnv, ...) { lst <- list_any( path, - function(x) utils::read.csv( - file = x, - header = header, - sep = sep, - quote = quote, - dec = dec, - fill = fill, - comment.char = comment.char, - stringsAsFactors = stringsAsFactors, - na.strings = na.strings - ), + readr::read_tsv, regexp = regexp, ignore.case = ignore.case, invert = invert, diff --git a/R/tor_example.R b/R/tor_example.R index c942bb9..fe12c53 100644 --- a/R/tor_example.R +++ b/R/tor_example.R @@ -9,9 +9,9 @@ #' #' @examples #' tor_example() -#' +#' #' tor_example("csv") -#' +#' #' dir(tor_example("csv")) #' @family helpers #' @export diff --git a/README.md b/README.md index 4495223..1964352 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,12 @@ devtools::install_github("maurolepore/tor") ``` r library(tidyverse) -#> -- Attaching packages --------------------------------------------- tidyverse 1.2.1 -- +#> -- Attaching packages -------------------------------------------- tidyverse 1.2.1 -- #> v ggplot2 3.1.0 v purrr 0.2.5 #> v tibble 2.0.0 v dplyr 0.7.8 #> v tidyr 0.8.2 v stringr 1.3.1 #> v readr 1.3.1 v forcats 0.3.0 -#> -- Conflicts ------------------------------------------------ tidyverse_conflicts() -- +#> -- Conflicts ----------------------------------------------- tidyverse_conflicts() -- #> x dplyr::filter() masks stats::filter() #> x dplyr::lag() masks stats::lag() library(fs) @@ -61,10 +61,18 @@ dir() #> [19] "tor.Rproj" list_csv() +#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) #> $csv1 #> # A tibble: 2 x 1 #> x -#> +#> #> 1 1 #> 2 2 #> @@ -375,6 +383,14 @@ dir(pattern = "[.]csv$") #> [1] "csv1.csv" "csv2.csv" dfms <- list_csv() +#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) format_path(names(dfms), "csv") #> [1] "./csv1.csv" "./csv2.csv" @@ -396,10 +412,18 @@ map_chr(dfms, ~ format_path(names(.), "csv", ".", "this-")) #> "./this-x.csv" "./this-y.csv" (dfs <- list_csv()) +#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) #> $csv1 #> # A tibble: 2 x 1 #> x -#> +#> #> 1 1 #> 2 2 #> diff --git a/docs/index.html b/docs/index.html index 40881ec..a0a20f7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -92,12 +92,12 @@

Example

+#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) +#> $csv1 +#> # A tibble: 2 x 1 +#> x +#> <dbl> +#> 1 1 +#> 2 2 +#> +#> $csv2 +#> # A tibble: 2 x 1 +#> y +#> <chr> +#> 1 a +#> 2 b

Often you will specify a path to read from.

+#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) + +format_path(names(dfms), "csv") +#> [1] "./csv1.csv" "./csv2.csv" + +format_path(names(dfms), "csv", base = "home", prefix = "this-") +#> [1] "home/this-csv1.csv" "home/this-csv2.csv"

Combine it with purrr.

+#> Parsed with column specification: +#> cols( +#> x = col_double() +#> ) +#> Parsed with column specification: +#> cols( +#> y = col_character() +#> ) +#> $csv1 +#> # A tibble: 2 x 1 +#> x +#> <dbl> +#> 1 1 +#> 2 2 +#> +#> $csv2 +#> # A tibble: 2 x 1 +#> y +#> <chr> +#> 1 a +#> 2 b + +paths <- dfs %>% + imap_chr(~ format_path(.y, "csv", base = ".", prefix = "this-")) + +walk2(dfs, paths, readr::write_csv) + +dir_ls(".", regexp = "this-") +#> this-csv1.csv this-csv2.csv diff --git a/docs/news/index.html b/docs/news/index.html index f91d1a6..de68880 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -110,6 +110,12 @@

Minor

    +
  • +list_any(), list_csv(), list_tsv(), load_csv(), and load_tsv() now use readr. +
      +
    • This makes reading data faster and safer.
    • +
    +
  • Each dataframe output is now converted to tibble.
    • Users no longer need to call tibble::as_tibble().
    • diff --git a/docs/reference/format_path.html b/docs/reference/format_path.html index 56b384c..4a5da3b 100644 --- a/docs/reference/format_path.html +++ b/docs/reference/format_path.html @@ -138,10 +138,16 @@

      See a

      Examples

      format_path(c("file1", "file2"), "csv")
      #> [1] "./file1.csv" "./file2.csv"
      -(dfs <- list_csv(tor_example("csv")))
      #> $csv1 -#> # A tibble: 2 x 1 +(dfs <- list_csv(tor_example("csv")))
      #> Parsed with column specification: +#> cols( +#> x = col_double() +#> )
      #> Parsed with column specification: +#> cols( +#> y = col_character() +#> )
      #> $csv1 +#> # A tibble: 2 x 1 #> x -#> <int> +#> <dbl> #> 1 1 #> 2 2 #> diff --git a/docs/reference/list_csv.html b/docs/reference/list_csv.html index 333b8c7..c9336fc 100644 --- a/docs/reference/list_csv.html +++ b/docs/reference/list_csv.html @@ -114,14 +114,10 @@

      Read multiple files from a directory into a list.

      list_csv(path = ".", regexp = "[.]csv$", ignore.case = TRUE,
      -  invert = FALSE, header = TRUE, sep = ",", quote = "\"",
      -  dec = ".", fill = TRUE, comment.char = "",
      -  stringsAsFactors = FALSE, na.strings = c("", "NA"), ...)
      +  invert = FALSE, ...)
       
       list_tsv(path = ".", regexp = "[.]tsv$", ignore.case = TRUE,
      -  invert = FALSE, header = TRUE, sep = "\t", quote = "\"",
      -  dec = ".", fill = TRUE, comment.char = "",
      -  stringsAsFactors = FALSE, na.strings = c("", "NA"), ...)
      +  invert = FALSE, ...)
       
       list_rds(path = ".", regexp = "[.]rds$", ignore.case = TRUE,
         invert = FALSE)
      @@ -149,62 +145,9 @@ 

      Arg invert

      If TRUE return files which do not match

      - - header -

      a logical value indicating whether the file contains the - names of the variables as its first line. If missing, the value is - determined from the file format: header is set to TRUE - if and only if the first row contains one fewer field than the - number of columns.

      - - - sep -

      the field separator character. Values on each line of the - file are separated by this character. If sep = "" (the - default for read.table) the separator is ‘white space’, - that is one or more spaces, tabs, newlines or carriage returns.

      - - - quote -

      the set of quoting characters. To disable quoting - altogether, use quote = "". See scan for the - behaviour on quotes embedded in quotes. Quoting is only considered - for columns read as character, which is all of them unless - colClasses is specified.

      - - - dec -

      the character used in the file for decimal points.

      - - - fill -

      logical. If TRUE then in case the rows have unequal - length, blank fields are implicitly added. See ‘Details’.

      - - - comment.char -

      character: a character vector of length one - containing a single character or an empty string. Use "" to - turn off the interpretation of comments altogether.

      - - - stringsAsFactors -

      logical: should character vectors be converted - to factors? Note that this is overridden by as.is and - colClasses, both of which allow finer control.

      - - - na.strings -

      a character vector of strings which are to be - interpreted as NA values. Blank fields are also - considered to be missing values in logical, integer, numeric and - complex fields. Note that the test happens after - white space is stripped from the input, so na.strings - values may need their own white space stripped in advance.

      - ... -

      Arguments passed to utils::read.table().

      +

      Arguments passed to readr::read_csv() or readr::read_tsv().

      @@ -235,10 +178,18 @@

      Examp #> 2 b #>
      (tsv <- tor_example("tsv"))
      #> [1] "C:/Users/LeporeM/Documents/Dropbox/git/ml/tor/inst/extdata/tsv"
      dir(tsv)
      #> [1] "tsv1.tsv" "tsv2.tsv"
      -list_tsv(tsv)
      #> $tsv1 +list_tsv(tsv)
      #> Parsed with column specification: +#> cols( +#> x = col_double(), +#> y = col_character() +#> )
      #> Parsed with column specification: +#> cols( +#> x = col_double(), +#> y = col_character() +#> )
      #> $tsv1 #> # A tibble: 3 x 2 #> x y -#> <int> <chr> +#> <dbl> <chr> #> 1 1 a #> 2 2 NA #> 3 3 NA @@ -246,7 +197,7 @@

      Examp #> $tsv2 #> # A tibble: 3 x 2 #> x y -#> <int> <chr> +#> <dbl> <chr> #> 1 1 a #> 2 2 NA #> 3 3 b @@ -274,7 +225,10 @@

      Examp #> 1 a #> 2 b #>

      -list_csv(mixed)
      #> $csv +list_csv(mixed)
      #> Parsed with column specification: +#> cols( +#> y = col_character() +#> )
      #> $csv #> # A tibble: 2 x 1 #> y #> <chr> diff --git a/docs/reference/load_csv.html b/docs/reference/load_csv.html index 3439092..224349c 100644 --- a/docs/reference/load_csv.html +++ b/docs/reference/load_csv.html @@ -114,16 +114,10 @@

      Load each element of a list into an environment.

      load_csv(path = ".", regexp = "[.]csv$", ignore.case = TRUE,
      -  invert = FALSE, header = TRUE, sep = ",", quote = "\"",
      -  dec = ".", fill = TRUE, comment.char = "",
      -  stringsAsFactors = FALSE, na.strings = c("", "NA"),
      -  envir = .GlobalEnv, ...)
      +  invert = FALSE, envir = .GlobalEnv, ...)
       
       load_tsv(path = ".", regexp = "[.]tsv$", ignore.case = TRUE,
      -  invert = FALSE, header = TRUE, sep = "\t", quote = "\"",
      -  dec = ".", fill = TRUE, comment.char = "",
      -  stringsAsFactors = FALSE, na.strings = c("", "NA"),
      -  envir = .GlobalEnv, ...)
      +  invert = FALSE, envir = .GlobalEnv, ...)
       
       load_rds(path = ".", regexp = "[.]rds$", ignore.case = TRUE,
         invert = FALSE, envir = .GlobalEnv)
      @@ -151,66 +145,13 @@ 

      Arg invert

      If TRUE return files which do not match

      - - header -

      a logical value indicating whether the file contains the - names of the variables as its first line. If missing, the value is - determined from the file format: header is set to TRUE - if and only if the first row contains one fewer field than the - number of columns.

      - - - sep -

      the field separator character. Values on each line of the - file are separated by this character. If sep = "" (the - default for read.table) the separator is ‘white space’, - that is one or more spaces, tabs, newlines or carriage returns.

      - - - quote -

      the set of quoting characters. To disable quoting - altogether, use quote = "". See scan for the - behaviour on quotes embedded in quotes. Quoting is only considered - for columns read as character, which is all of them unless - colClasses is specified.

      - - - dec -

      the character used in the file for decimal points.

      - - - fill -

      logical. If TRUE then in case the rows have unequal - length, blank fields are implicitly added. See ‘Details’.

      - - - comment.char -

      character: a character vector of length one - containing a single character or an empty string. Use "" to - turn off the interpretation of comments altogether.

      - - - stringsAsFactors -

      logical: should character vectors be converted - to factors? Note that this is overridden by as.is and - colClasses, both of which allow finer control.

      - - - na.strings -

      a character vector of strings which are to be - interpreted as NA values. Blank fields are also - considered to be missing values in logical, integer, numeric and - complex fields. Note that the test happens after - white space is stripped from the input, so na.strings - values may need their own white space stripped in advance.

      - envir

      an environment or NULL.

      ... -

      Arguments passed to utils::read.table().

      +

      Arguments passed to readr::read_csv() or readr::read_tsv().

      @@ -226,11 +167,16 @@

      See a

      Examples

      (path_csv <- tor_example("csv"))
      #> [1] "C:/Users/LeporeM/Documents/Dropbox/git/ml/tor/inst/extdata/csv"
      dir(path_csv)
      #> [1] "csv1.csv" "csv2.csv"
      -load_csv(path_csv) -# Each dataframe is now available in the global environment -csv1
      #> # A tibble: 2 x 1 +load_csv(path_csv)
      #> Parsed with column specification: +#> cols( +#> x = col_double() +#> )
      #> Parsed with column specification: +#> cols( +#> y = col_character() +#> )
      # Each dataframe is now available in the global environment +csv1
      #> # A tibble: 2 x 1 #> x -#> <int> +#> <dbl> #> 1 1 #> 2 2
      csv2
      #> # A tibble: 2 x 1 #> y diff --git a/inst/extdata/tsv/tsv1.tsv b/inst/extdata/tsv/tsv1.tsv index 05f8965..606ad51 100644 --- a/inst/extdata/tsv/tsv1.tsv +++ b/inst/extdata/tsv/tsv1.tsv @@ -1,4 +1,4 @@ x y 1 a -2 +2 NA 3 NA diff --git a/inst/extdata/tsv/tsv2.tsv b/inst/extdata/tsv/tsv2.tsv index 1673831..f64144d 100644 --- a/inst/extdata/tsv/tsv2.tsv +++ b/inst/extdata/tsv/tsv2.tsv @@ -1,4 +1,4 @@ x y 1 a -2 +2 NA 3 b diff --git a/man/list_csv.Rd b/man/list_csv.Rd index 7ef7183..2868972 100644 --- a/man/list_csv.Rd +++ b/man/list_csv.Rd @@ -8,14 +8,10 @@ \title{Read multiple files from a directory into a list.} \usage{ list_csv(path = ".", regexp = "[.]csv$", ignore.case = TRUE, - invert = FALSE, header = TRUE, sep = ",", quote = "\\"", - dec = ".", fill = TRUE, comment.char = "", - stringsAsFactors = FALSE, na.strings = c("", "NA"), ...) + invert = FALSE, ...) list_tsv(path = ".", regexp = "[.]tsv$", ignore.case = TRUE, - invert = FALSE, header = TRUE, sep = "\\t", quote = "\\"", - dec = ".", fill = TRUE, comment.char = "", - stringsAsFactors = FALSE, na.strings = c("", "NA"), ...) + invert = FALSE, ...) list_rds(path = ".", regexp = "[.]rds$", ignore.case = TRUE, invert = FALSE) @@ -33,44 +29,7 @@ list_rdata(path = ".", regexp = "[.]rdata$|[.]rda$", \item{invert}{If \code{TRUE} return files which do \emph{not} match} -\item{header}{a logical value indicating whether the file contains the - names of the variables as its first line. If missing, the value is - determined from the file format: \code{header} is set to \code{TRUE} - if and only if the first row contains one fewer field than the - number of columns.} - -\item{sep}{the field separator character. Values on each line of the - file are separated by this character. If \code{sep = ""} (the - default for \code{read.table}) the separator is \sQuote{white space}, - that is one or more spaces, tabs, newlines or carriage returns.} - -\item{quote}{the set of quoting characters. To disable quoting - altogether, use \code{quote = ""}. See \code{\link{scan}} for the - behaviour on quotes embedded in quotes. Quoting is only considered - for columns read as character, which is all of them unless - \code{colClasses} is specified.} - -\item{dec}{the character used in the file for decimal points.} - -\item{fill}{logical. If \code{TRUE} then in case the rows have unequal - length, blank fields are implicitly added. See \sQuote{Details}.} - -\item{comment.char}{character: a character vector of length one - containing a single character or an empty string. Use \code{""} to - turn off the interpretation of comments altogether.} - -\item{stringsAsFactors}{logical: should character vectors be converted - to factors? Note that this is overridden by \code{as.is} and - \code{colClasses}, both of which allow finer control.} - -\item{na.strings}{a character vector of strings which are to be - interpreted as \code{\link{NA}} values. Blank fields are also - considered to be missing values in logical, integer, numeric and - complex fields. Note that the test happens \emph{after} - white space is stripped from the input, so \code{na.strings} - values may need their own white space stripped in advance.} - -\item{...}{Arguments passed to \code{\link[utils:read.table]{utils::read.table()}}.} +\item{...}{Arguments passed to \code{readr::read_csv()} or \code{readr::read_tsv()}.} } \value{ A list. diff --git a/man/load_csv.Rd b/man/load_csv.Rd index 14cb63a..53aefa1 100644 --- a/man/load_csv.Rd +++ b/man/load_csv.Rd @@ -8,16 +8,10 @@ \title{Load each element of a list into an environment.} \usage{ load_csv(path = ".", regexp = "[.]csv$", ignore.case = TRUE, - invert = FALSE, header = TRUE, sep = ",", quote = "\\"", - dec = ".", fill = TRUE, comment.char = "", - stringsAsFactors = FALSE, na.strings = c("", "NA"), - envir = .GlobalEnv, ...) + invert = FALSE, envir = .GlobalEnv, ...) load_tsv(path = ".", regexp = "[.]tsv$", ignore.case = TRUE, - invert = FALSE, header = TRUE, sep = "\\t", quote = "\\"", - dec = ".", fill = TRUE, comment.char = "", - stringsAsFactors = FALSE, na.strings = c("", "NA"), - envir = .GlobalEnv, ...) + invert = FALSE, envir = .GlobalEnv, ...) load_rds(path = ".", regexp = "[.]rds$", ignore.case = TRUE, invert = FALSE, envir = .GlobalEnv) @@ -35,46 +29,9 @@ load_rdata(path = ".", regexp = "[.]rdata$|[.]rda$", \item{invert}{If \code{TRUE} return files which do \emph{not} match} -\item{header}{a logical value indicating whether the file contains the - names of the variables as its first line. If missing, the value is - determined from the file format: \code{header} is set to \code{TRUE} - if and only if the first row contains one fewer field than the - number of columns.} - -\item{sep}{the field separator character. Values on each line of the - file are separated by this character. If \code{sep = ""} (the - default for \code{read.table}) the separator is \sQuote{white space}, - that is one or more spaces, tabs, newlines or carriage returns.} - -\item{quote}{the set of quoting characters. To disable quoting - altogether, use \code{quote = ""}. See \code{\link{scan}} for the - behaviour on quotes embedded in quotes. Quoting is only considered - for columns read as character, which is all of them unless - \code{colClasses} is specified.} - -\item{dec}{the character used in the file for decimal points.} - -\item{fill}{logical. If \code{TRUE} then in case the rows have unequal - length, blank fields are implicitly added. See \sQuote{Details}.} - -\item{comment.char}{character: a character vector of length one - containing a single character or an empty string. Use \code{""} to - turn off the interpretation of comments altogether.} - -\item{stringsAsFactors}{logical: should character vectors be converted - to factors? Note that this is overridden by \code{as.is} and - \code{colClasses}, both of which allow finer control.} - -\item{na.strings}{a character vector of strings which are to be - interpreted as \code{\link{NA}} values. Blank fields are also - considered to be missing values in logical, integer, numeric and - complex fields. Note that the test happens \emph{after} - white space is stripped from the input, so \code{na.strings} - values may need their own white space stripped in advance.} - \item{envir}{an \code{\link{environment}} or \code{NULL}.} -\item{...}{Arguments passed to \code{\link[utils:read.table]{utils::read.table()}}.} +\item{...}{Arguments passed to \code{readr::read_csv()} or \code{readr::read_tsv()}.} } \value{ \code{invisible(path)}. diff --git a/tests/testthat/csv.csv b/tests/testthat/csv1.csv similarity index 100% rename from tests/testthat/csv.csv rename to tests/testthat/csv1.csv diff --git a/tests/testthat/csv2.csv b/tests/testthat/csv2.csv new file mode 100644 index 0000000..840975b --- /dev/null +++ b/tests/testthat/csv2.csv @@ -0,0 +1,3 @@ +y +a +b diff --git a/tests/testthat/file1.csv b/tests/testthat/file1.csv deleted file mode 100644 index 0d78e42..0000000 --- a/tests/testthat/file1.csv +++ /dev/null @@ -1,3 +0,0 @@ -"","x" -"1",1 -"2",2 diff --git a/tests/testthat/file2.csv b/tests/testthat/file2.csv deleted file mode 100644 index f0533b6..0000000 --- a/tests/testthat/file2.csv +++ /dev/null @@ -1,3 +0,0 @@ -"","y" -"1","a" -"2","b" diff --git a/tests/testthat/test-list_csv.R b/tests/testthat/test-list_csv.R index f4376d0..e6ca917 100644 --- a/tests/testthat/test-list_csv.R +++ b/tests/testthat/test-list_csv.R @@ -82,7 +82,8 @@ test_that("list_csv defaults to `stringsAsFactors = FALSE`", { }) test_that("list_csv defaults to read from working directory", { - expect_true(any("csv" %in% names(list_csv()))) + # WARNING: Not for interactive use. + expect_true(all(c("csv1", "csv2") %in% names(list_csv()))) }) test_that("list_csv is sensitive to `regexp`, `invert, and `ignore.case`", { diff --git a/tests/testthat/test-load_csv.R b/tests/testthat/test-load_csv.R index bd69194..d9bcdcd 100644 --- a/tests/testthat/test-load_csv.R +++ b/tests/testthat/test-load_csv.R @@ -1,18 +1,16 @@ -context("load_rdata") +context("load_rds") -test_that("load_rdata loads multiple .rdata files in a new environment", { +test_that("load_rds loads multiple .rds files in a new environment", { e <- new.env() - load_rdata(tor_example("rdata"), envir = e) + load_rds(tor_example("rds"), envir = e) expect_equal( ls(e), - c("rdata1", "rdata2") + c("rds1", "rds2") ) rm(list = ls()) }) - - context("load_rdata") test_that("load_rdata loads multiple .rdata files in a new environment", { @@ -26,8 +24,6 @@ test_that("load_rdata loads multiple .rdata files in a new environment", { rm(list = ls()) }) - - context("load_csv") test_that("load_csv loads multiple .csv files in a new environment", { @@ -41,8 +37,6 @@ test_that("load_csv loads multiple .csv files in a new environment", { rm(list = ls()) }) - - context("load_tsv") test_that("load_tsv loads multiple .tsv files in a new environment", {