Skip to content

Commit

Permalink
Coerce dataframes to tibbles (#9)
Browse files Browse the repository at this point in the history
* list_any() now outputs tibbles.
  • Loading branch information
maurolepore committed Jan 8, 2019
1 parent a8e7df7 commit 1f65d46
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 107 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ URL: https://github.com/maurolepore/tor
BugReports: https://github.com/maurolepore/tor/issues
Imports:
fs,
rlang
rlang,
tibble
Suggests:
covr,
spelling,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Minor

* Each dataframe output is now converted to tibble.
* Users no longer need to call `tibble::as_tibble()`.

* New `load_*()` variants load each file in a directory into an environment.

## Patch
Expand Down
21 changes: 14 additions & 7 deletions R/list_any.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$",
Expand Down Expand Up @@ -52,5 +52,12 @@ list_any <- function(path = ".",
}

file_names <- fs::path_ext_remove(fs::path_file(files))
set_names(lapply(files, rlang::as_function(.f), ...), file_names)
result <- set_names(lapply(files, rlang::as_function(.f), ...), file_names)
dataframe_to_tibble(result)
}

dataframe_to_tibble <- function(.x) {
sel <- vapply(.x, is.data.frame, logical(1))
.x[sel] <- lapply(.x[sel], tibble::as_tibble)
.x
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ library(fs)
library(tor)
```

All functions list whatever they read, and default to reading from the working directory.
All functions list whatever they read, and default to reading from the working directory. Dataframes are coerced to tibbles.

### `list_*()`: Import multiple files from a directory into a list

Expand Down
58 changes: 37 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ library(tor)
```

All functions list whatever they read, and default to reading from the
working directory.
working directory. Dataframes are coerced to tibbles.

### `list_*()`: Import multiple files from a directory into a list

Expand All @@ -62,13 +62,17 @@ dir()

list_csv()
#> $csv1
#> x
#> 1 1
#> 2 2
#> # A tibble: 2 x 1
#> x
#> <int>
#> 1 1
#> 2 2
#>
#> $csv2
#> y
#> 1 a
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b
```

Expand Down Expand Up @@ -154,13 +158,17 @@ dir(path_csv)

list_any(path_csv, read.csv)
#> $csv1
#> x
#> 1 1
#> 2 2
#> # A tibble: 2 x 1
#> x
#> <int>
#> 1 1
#> 2 2
#>
#> $csv2
#> y
#> 1 a
#> # A tibble: 2 x 1
#> y
#> <fct>
#> 1 a
#> 2 b
```

Expand Down Expand Up @@ -319,12 +327,16 @@ load_csv()

# Each file is now available as a dataframe in the global environment
csv1
#> x
#> 1 1
#> 2 2
#> # A tibble: 2 x 1
#> x
#> <int>
#> 1 1
#> 2 2
csv2
#> y
#> 1 a
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b
```

Expand Down Expand Up @@ -385,13 +397,17 @@ map_chr(dfms, ~ format_path(names(.), "csv", ".", "this-"))

(dfs <- list_csv())
#> $csv1
#> x
#> 1 1
#> 2 2
#> # A tibble: 2 x 1
#> x
#> <int>
#> 1 1
#> 2 2
#>
#> $csv2
#> y
#> 1 a
#> # A tibble: 2 x 1
#> y
#> <chr>
#> 1 a
#> 2 b

paths <- dfs %>%
Expand Down
96 changes: 56 additions & 40 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f65d46

Please sign in to comment.