'Open cell range in read_excel R
Is there a way to not specify a cell range in the read_excel function in R?
So, for example, if the data entries start at cell C3 and end at cell D300, could I not be forced to specify D300, but instead tell R to read until there are no more cells with data entries in them?
Something like:
A <- read_excel("excell/book.xlsx", sheet="A", range="C3:")
Solution 1:[1]
If you read the docs for readxl::read_excel, it includes
range: A cell range to read from, as described in
cell-specification. Includes typical Excel ranges like
"B3:D87", possibly including the sheet name like
"Budget!B2:G14", and more. Interpreted strictly, even if the
range forces the inclusion of leading or trailing empty rows
or columns. Takes precedence over 'skip', 'n_max' and
'sheet'.
Though it is easy to assume it must be a string, if you dig into ?readxl::cell-specification, it suggests you look into cellranger::cell_limits(). In that, it says that
cell_limits(ul = c(NA_integer_, NA_integer_), lr = c(NA_integer_,
NA_integer_), sheet = NA_character_)
...
A value of 'NA' in 'ul' or 'lr' means the corresponding limit is left unspecified.
This says that you can use NA to leave things open.
Try:
A <- read_excel("excell/book.xlsx", sheet="A", range=cellranger::cell_limits(c(3,3)))
where an unspecified lr leaves the lower-right corner open.
Solution 2:[2]
Try
A <- read_exel("excell/book.xlsx", sheet = "A", range = cell_limits(c(3, 3), c(NA, NA))
see here for more information: https://readxl.tidyverse.org/reference/cell-specification.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | r2evans |
| Solution 2 | martinim |
