read_excel() ignore case of worksheet name?
Hi Ravi,
The simplest way seems to be the excel_sheets function in the readxl
package. If you know that the sheet name will be some form of "Table",
something like this may do it:
getSheetCase<-function(filepath,sheetname) {
localnames<-c(sheetname,
paste0(toupper(substr(sheetname,1,1)),substr(sheetname,2,nchar(sheetname))),
toupper(sheetname),tolower(sheetname))
xlnames<-readxl::excel_sheets(filepath)
namepos<-0
for(pos in 1:length(localnames)) {
if(length(grep(localnames[pos],xlnames))) namepos<-pos
cat(localnames[pos],namepos,"\n")
}
if(is.null(namepos)) return(NULL)
else return(read_excel(filepath,
sheet=localnames[namepos]))
}
getSheetCase("GS_SS2.xlsx","intent")
This example works on an excel spreadsheet I have as in the last line.
Just as I was about to send this, three messages came in. One was
Ista's excellent solution that blew mine away. Maybe next time.
Jim
On Wed, May 27, 2020 at 1:05 PM Ravi Jeyaraman <ravi76 at gmail.com> wrote:
I?ve already tried that and doesn?t work From: Erin Hodgess [mailto:erinm.hodgess at gmail.com] Sent: Tuesday, May 26, 2020 10:55 PM To: Ravi Jeyaraman <ravi76 at gmail.com> Cc: r-help at r-project.org Subject: Re: [R] read_excel() ignore case of worksheet name? Here?s a thought, please. Could you use the tolower function and make them all lower case? Thanks, Erin On Tue, May 26, 2020 at 8:21 PM Ravi Jeyaraman <ravi76 at gmail.com <mailto:ravi76 at gmail.com> > wrote: Hello All, Is there any parameter to make read_excel() ignore the case-sensitiveness of the worksheet? I'm using the below to ready in multiple spreadsheets and it works perfectly fine if the worksheet is named 'Tables', but fails when it's named ' TABLES'. Any thoughts? lapply(1:nrow(SIS), function(x) readxl::read_excel(SIS$FULL_FILEPATH[x], sheet='Tables', .name_repair = fixColNames)) Thanks in advance for your response. Cheers Ravi -- This email has been checked for viruses by AVG. https://www.avg.com
______________________________________________ R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Erin Hodgess, PhD mailto: erinm.hodgess at gmail.com <mailto:erinm.hodgess at gmail.com> [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.