Home » excel » excel – Open cell range in read_excel R

excel – Open cell range in read_excel R

Posted by: admin May 14, 2020 Leave a comment

Questions:

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:")
How to&Answers:

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.