- Create an - .xlsxfile with three sheets named: "Test 1", "S&P500 TR" and "SP500 TR". Put some random content in each sheet and save it as "Book1.xlsx".
- Run: - > a <- getSheetNames("Book1.xlsx") > a [1] "Test 1" "S&P500 TR" "SP500 TR"
- Now try: - > read.xlsx("Book1.xlsx", a[2]) Error in read.xlsx.default("Book1.xlsx", a[2]) : Cannot find sheet named "S&P500 TR"
- 
                    I'm guessing you're out of luck. Your choices are likely to be: (1) rename your sheets; (2) hack the internals of `openxlsx::read.xlsx` to fix the problem; (3) look around for another XLS-reader that works the way you want it to (for example, see alternatives [here](https://www.datacamp.com/community/tutorials/r-tutorial-read-excel-into-r) – Ben Bolker Mar 30 '17 at 16:49
- 
                    Please read this [post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) to know how to provide a reproducible code. I would like to know your OS and R version. – M-- Mar 30 '17 at 17:29
- 
                    1Hi @Masoud it happens on both my win 7 and on my Fedora 25 machines. R versions 3.3.2 and 3.3.3 respectively – Gio Mar 30 '17 at 20:04
3 Answers
First check if you actually type the name S&P500 TR instead of using a[2] that would change anything.
Alternatively, you can use readxl package for importing;
  library(readxl)
  X1 <- read_excel("C:/1.xls", sheet = "S&P500 TR")
This is a spreadsheet that I had and it is the result after it is imported;
 head(X1)
  # A tibble: 6 × 4
  #   Year Month       Community                      ` Average Daily`
  #  <dbl> <chr>         <chr>                                   <dbl>
  # 1  2016   Jan       Arlington                              5.35
  # 2  2016   Jan       Ashland                               1.26
  # 3  2016   Jan       Bedford                                2.62
  # 4  2016   Jan       Belmont                               3.03
  # 5  2016   Jan       Boston                                 84.89
  # 6  2016   Jan       Braintree                              8.16
 
    
    - 25,431
- 8
- 61
- 93
- 
                    1Thank you @Masoud. I am actually trying to find an alternative to read_excel and XLConnect, hence openxlsx. Also trying with the explicit name makes no difference. – Gio Mar 30 '17 at 19:55
I ran into the same problem, but found a workaround. First load in the workbook using read.xlsx(). Then rename the problematic sheet to avoid the ampersand. To fix the code in your example:
wb = read.xlsx("Book1.xlsx")
renameWorksheet(wb, "S&P500 TR", "NEW NAME")
output = read.xlsx(wb, "NEW NAME")
Hope this helps!
 
    
    - 1
First load the workbook, then use the which and grepl function to return the sheet number containing the sheet name (which can include the '&' character when done in this way). This seems to work quite well in an application I am currently working on.
An (incomplete) example is given below that should be easily modified to your context. In my case 'i' is a file name (looping over many files). The "toy" code is here:
wb <- loadWorkbook(file = i)
which( grepl("CAPEX & Depreciation", names(wb)) )
 
    
    - 4,646
- 3
- 22
- 43
 
    
    - 1
