Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions vignettes/articles/readxl-workflows.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ Yes! Is the data written to CSV an exact copy of what we imported from Excel?

```{r}
mtcars_alt <- read_csv("mtcars-raw.csv")
## readr leaves a note-to-self in `spec` that records its column guessing,
## so we remove that attribute before the check
attr(mtcars_alt, "spec") <- NULL
identical(mtcars_xl, mtcars_alt)
## readr leaves some special attributes behind to record its
## column guessing, so we remove those with `[]`
identical(mtcars_xl, mtcars_alt[])
```

Yes! If we needed to restart or troubleshoot this fictional analysis, `mtcars-raw.csv` is available as a second, highly accessible alternative to `datasets.xlsx`.
Expand Down Expand Up @@ -144,7 +143,7 @@ path <- readxl_example("deaths.xlsx")
deaths <- path |>
excel_sheets() |>
set_names() |>
map(~ read_excel(path = path, sheet = .x, range = "A5:F15")) |>
map(\(x) read_excel(path = path, sheet = x, range = "A5:F15")) |>
list_rbind(names_to = "sheet")
print(deaths, n = Inf)
```
Expand Down Expand Up @@ -175,7 +174,7 @@ ranges <- list("A5:F15", cell_rows(5:15))
deaths <- map2(
sheets,
ranges,
~ read_excel(path, sheet = .x, range = .y)
\(x, y) read_excel(path, sheet = x, range = y)
) |>
list_rbind(names_to = "sheet") |>
write_csv("deaths.csv")
Expand All @@ -195,9 +194,9 @@ Rework examples from above but using base R only, other than readxl.
```{r eval = FALSE}
mtcars_xl <- read_excel(readxl_example("datasets.xlsx"), sheet = "mtcars")
write.csv(mtcars_xl, "mtcars-raw.csv", row.names = FALSE, quote = FALSE)
mtcars_alt <- read.csv("mtcars-raw.csv", stringsAsFactors = FALSE)
## coerce mtcars_xl back to a data.frame
identical(as.data.frame(mtcars_xl), mtcars_alt)
mtcars_alt <- read.csv("mtcars-raw.csv")
## coerce mtcars_xl to a data.frame, instead of a tibble
all.equal(as.data.frame(mtcars_xl), mtcars_alt)
```

### Iterate over multiple worksheets in a workbook
Expand All @@ -215,8 +214,12 @@ names(xl_list) <- sheets
read_then_csv <- function(sheet, path) {
pathbase <- tools::file_path_sans_ext(basename(path))
df <- read_excel(path = path, sheet = sheet)
write.csv(df, paste0(pathbase, "-", sheet, ".csv"),
quote = FALSE, row.names = FALSE)
write.csv(
df,
paste0(pathbase, "-", sheet, ".csv"),
quote = FALSE,
row.names = FALSE
)
df
}
path <- readxl_example("datasets.xlsx")
Expand Down Expand Up @@ -244,9 +247,14 @@ xl_list <- do.call(rbind, xl_list)
path <- readxl_example("deaths.xlsx")
sheets <- excel_sheets(path)
ranges <- list("A5:F15", cell_rows(5:15))
xl_list <- mapply(function(x, y) {
read_excel(path = path, sheet = x, range = y)
}, sheets, ranges, SIMPLIFY = FALSE)
xl_list <- mapply(
function(x, y) {
read_excel(path = path, sheet = x, range = y)
},
sheets,
ranges,
SIMPLIFY = FALSE
)
xl_list <- lapply(seq_along(sheets), function(i) {
data.frame(sheet = I(sheets[i]), xl_list[[i]])
})
Expand Down
Loading