Skip to content

Commit

Permalink
Merge pull request #114 from pbrohan/as_tibble
Browse files Browse the repository at this point in the history
As tibble
  • Loading branch information
chainsawriot authored Jul 24, 2023
2 parents c3b4e25 + b35df95 commit 0b2b682
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 755 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
inst/doc
doc
Meta
/doc/
/Meta/
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Imports:
readr (>= 1.2.1),
stringi,
utils,
zip
zip,
tibble
LinkingTo:
cpp11
Suggests:
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ These have been deprecated for several years.
* Changed behaviour when only one column is read. Previously gave an error. If row names are requested, gives a warning that this would cause the output to be empty, and does not assign names.
* Sheets are now accepted as part of the `range` argument, e.g. `Range = "Sheet2!A2:B7"`. If this and the `sheets` argument are given, this is preferred.
* Merged cells now have their value places in the top-left cell. All other cells that would be covered by the merge are filled with `NA`.
* Added `as_tibble` and `.name_repair` as arugments. If `as_tibble` is true, outputs as a tibble using `tibble::as_tibble()` passing on `.name_repair` (default being `"check_unique"`).

## read_fods

Expand Down
48 changes: 44 additions & 4 deletions R/read_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE) {
verbose = FALSE,
as_tibble = FALSE,
.name_repair = "check_unique") {
if (missing(path) || !is.character(path)) {
stop("No file path was provided for the 'path' argument. Please provide a path to a file to import.", call. = FALSE)
}
Expand All @@ -96,6 +98,17 @@
if (!is.logical(verbose)) {
stop("verbose must be of type `boolean`", call. = FALSE)
}
if (!is.logical(as_tibble)) {
stop("as_tibble must be of type `boolean", call. = FALSE)
}
if (as_tibble &&
(!(.name_repair %in% c("minimal",
"unique",
"check_unique",
"universal")) ||
is.function(.name_repair))) {
stop(".name_repair must either be one of \"minimal\", \"unique\", \"check_unique\", \"univseral\" or a function", call. = FALSE)
}
}

.read_ods <- function(path,
Expand All @@ -110,6 +123,8 @@
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE,
as_tibble = FALSE,
.name_repair = "check_unique",
flat = FALSE) {
.check_read_args(path,
sheet,
Expand All @@ -122,7 +137,9 @@
row_names,
strings_as_factors,
check_names,
verbose)
verbose,
as_tibble,
.name_repair)
# Get cell range info
limits <- .standardise_limits(range, skip)
# Get sheet number.
Expand Down Expand Up @@ -202,6 +219,10 @@
res <- .convert_strings_to_factors(res)
}

if (as_tibble){
res <- tibble::as_tibble(res, .name_repair = .name_repair)
}

return(res)

}
Expand All @@ -224,6 +245,16 @@
#' @param strings_as_factors logical, if character columns to be converted to factors. Default is FALSE.
#' @param check_names logical, passed down to base::data.frame(). Default is FALSE.
#' @param verbose logical, if messages should be displayed. Default is FALSE.
#' @param as_tibble logical, if the output should be a tibble (as opposed to a data.frame). Default is FALSE.
#' @param .name_repair A string or function passed on as `.name_repair` to [tibble::as_tibble()]
#' - `"minimal"`: No name repair
#' - `"unique"` : Make sure names are unique and not empty
#' - `"check_unique"`: Check names are unique, but do not repair
#' - `"universal"` : Checks names are unique and valid R variables names in scope
#' - A function to apply custom name repair.
#'
#' Default is `"check_unique"`.
#'
#' @return A data frame (\code{data.frame}) containing a representation of data in the (f)ods file.
#' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
#' @examples
Expand Down Expand Up @@ -253,7 +284,9 @@ read_ods <- function(path,
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE
verbose = FALSE,
as_tibble = FALSE,
.name_repair = "check_unique"

) {
## Should use match.call but there's a weird bug if one of the variable names is 'file'
Expand All @@ -269,6 +302,8 @@ read_ods <- function(path,
strings_as_factors,
check_names,
verbose,
as_tibble,
.name_repair,
flat = FALSE)
}

Expand All @@ -285,7 +320,10 @@ read_fods <- function(path,
row_names = FALSE,
strings_as_factors = FALSE,
check_names = FALSE,
verbose = FALSE
verbose = FALSE,
as_tibble = FALSE,
.name_repair = "check_unique"

) {
## Should use match.call but there's a weird bug if one of the variable names is 'file'
.read_ods(path,
Expand All @@ -300,5 +338,7 @@ read_fods <- function(path,
strings_as_factors,
check_names,
verbose,
as_tibble,
.name_repair,
flat = TRUE)
}
5 changes: 5 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Reading from a specific range
read_ods("starwars.ods", sheet = 2, range = "A1:C11")
```

Reading as a tibble
```{r}
read_ods("starwars.ods", range="Sheet1!A2:C11", as_tibble = TRUE)
```

#### Writing

```{r}
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ read_ods("starwars.ods", sheet = 2, range = "A1:C11")
#> 10 Obi-Wan Kenobi 180 80
```

Reading as a tibble

``` r
read_ods("starwars.ods", range="Sheet1!A2:C11", as_tibble = TRUE)
#> # A tibble: 9 × 3
#> `Luke Skywalker` Tatooine Human
#> <chr> <chr> <chr>
#> 1 C-3PO Tatooine Human
#> 2 R2-D2 Alderaan Human
#> 3 Darth Vader Tatooine Human
#> 4 Leia Organa Tatooine Human
#> 5 Owen Lars Tatooine Human
#> 6 Beru Whitesun lars Stewjon Human
#> 7 R5-D4 Tatooine Human
#> 8 Biggs Darklighter Kashyyyk Wookiee
#> 9 Obi-Wan Kenobi Corellia Human
```

#### Writing

``` r
Expand Down
21 changes: 19 additions & 2 deletions man/read_ods.Rd

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

174 changes: 0 additions & 174 deletions src/rapidxml/rapidxml_iterators.hpp

This file was deleted.

Loading

0 comments on commit 0b2b682

Please sign in to comment.