Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allow recycling of col_type #114

Closed
rickdonnelly opened this issue Jul 7, 2015 · 3 comments
Closed

Feature request: Allow recycling of col_type #114

rickdonnelly opened this issue Jul 7, 2015 · 3 comments

Comments

@rickdonnelly
Copy link

I tried to set a single value for col_type to text for an Excel sheet with multiple columns. I got this error message:

Error: Need one name and type for each column

Since there is no way to extract the number of columns before reading the file it would be great if col_type could recycle its arguments when the list is shorter than the number of columns, as read.table, read_table, and fread all do.

@ghost
Copy link

ghost commented Jul 12, 2015

@rickdonnelly There is actually a way to extract the number of columns in an Excel sheet before reading it in. While I agree that it would be nice to have a recycling feature, this is the workaround I've been using:

library(tools)
format <- file_ext(file) %>% tolower()
sheetNames <- excel_sheets(file)
sheetIndex <- which(sheetNames == sheet)
functionName <- paste("readxl", format, "col_names", sep="_")
n <- .Call(functionName, PACKAGE = 'readxl', file, sheetIndex, skip) %>% length()
colTypes <- rep("text", n)
dat <- read_excel(file, col_types=colTypes, sheet=sheet, skip=skip)

You can obviously replace "text" in the colTypes assignment with one of the other values that is accepted by read_excel.

Hope this helps!

@vergilcw
Copy link

readxl/R/RcppExports.R contains a number of helper functions that are not exported. Some of these could be useful for reading in excel sheets with unknown characteristics ahead of time (e.g. for linux users with no access to excel):
xls_col_names and xlsx_col_names as @theRanalyst suggests in his workaround
but there are also:
xls_col_types and xlsx_col_types which I assume predict the column types of a worksheet
xlsx_dim for getting the dimesions of the tabular data
countRows
and a few others. @hadley, is there a plan to export any of these helper functions in the future?

@jennybc
Copy link
Member

jennybc commented Jan 7, 2017

Covered by #198

@jennybc jennybc closed this as completed Jan 7, 2017
@lock lock bot locked and limited conversation to collaborators Oct 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants