0

I am still new to R and I am not sure if I am missing something simple. I can open the excel file in R and it displays only the first tab/sheet. I know the path exists because I have the file currently on R and I copied the path right from the "copy path" option on the document. The file is also in my set working directory. Image of rstudio below. Thank you.

Picture of screen

r2evans
  • 141,215
  • 6
  • 77
  • 149
LogeDoge
  • 1
  • 1
  • 1
    Take the quotes off the R object name. excelsheets was apparently trying to find a path rather than a connection. You should explain why you wanted to use `excel_sheets` when you alreay have an R data object with the information. – IRTFM Jul 11 '22 at 21:27
  • Thanks for the response. Without the quotes It produces: Error: `path` must be a string – LogeDoge Jul 11 '22 at 21:30
  • It would also be easier if you could transform your excel table into a csv one. Then you could use read.csv or read.csv2. I also recommand you to create project and put your data in this project file. You can then access it using the path "./yourfilename.csv". – Léo Henry Jul 11 '22 at 21:31
  • 1
    `excel_sheets`'s argument is also a `path`, so it needs to be the `"C:\\Users\\spice\\..."`, same as the previous call. In a general sense, I would expect to look at the sheet names _before_ loading from the workbook, not after ... though that's just my "exploratory process" preference. The `read_excel` function can take `sheet=` as an argument, which can be the name of the sheet or an integer (position of the sheet). If you have `shnms <- excel_sheets("C:\\.....")`, you could do something like `allsheets <- lapply(shnms, function(nm) read_excel("C:\\......", sheet=nm))` (for a list of frames) – r2evans Jul 11 '22 at 21:33

1 Answers1

1

I suggest this workflow instead:

path <- "C:\\Users\\spice\\......." # use your real path here
shnms <- sheet_names(path)
alldata <- lapply(shnms, function(nm) read_excel(path, sheet=nm))

and you'll get a named list, each element is a worksheet in the original workbook.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thanks for the answer. I'm not actually interested in getting the sheet names displayed on r since i can look at them easily on excel. What I am trying to do is isolate each sheet from the excel file and assign object names to them so I can get summaries/stats for each sheet independently then proceed to get stats/summaries for all sheets combined. Each sheet is an experiment and I am trying to get summaries/stats for them essentially. – LogeDoge Jul 11 '22 at 21:45
  • @LogeDoge : Each of the "leaves" in the `alldata` structure is a full-fledged dataframe. You can use any descriptive function, eg that from Hmisc, with this call`lapply(alldata, Hmisc:describe)` – IRTFM Jul 11 '22 at 22:10
  • Technically LogeDoge, none of the names are displayed in R. The point of this was to do exactly what you said you wanted: make each sheet available as a frame. The fact that it's a list of frames might be a new thing, but it works very well in some situations (such as when they are all identical or similar structure and/or you will be doing the same thing(s) to each, see https://stackoverflow.com/a/24376207/3358227). – r2evans Jul 12 '22 at 00:50