lapply and readxl Error - r

I am using readxl and lapply to import multiple .xlsx files into my environment. The following worked perfectly before but now when I try to re-run it, it gives me the following error:
Error in read_fun(path = path, sheet = sheet, limits = limits, shim = shim, :
Evaluation error: zip file 'data.xlsx' cannot be opened.
Code:
setwd("./Data Folder") #set path in order to avoid lapply error (This is what solved it last time I got errors)
Load all "Data Folder" datasets
library(readxl)
file.list <- list.files(path = "./Data Folder", pattern = '*.xlsx')
df.list <- lapply(file.list, read_excel)
I have checked if the path I entered is still correct and I didn't alter it by mistake. I have also tried to open the documents in the folder using excel and there is no problem with the files. Any ideas?

I have figured out the problem. I had two different tabs opened in RStudio, one was a R markdown and the other an R Script. I was trying to run the code in R markdown without realising and so I got the lapply error as the setwd was not saved in R's system.
If anyone has this problem at any point:
check if you are in an R Script
set the folder you are taking the data out from as your home folder
run the entire chunk in markdown at once

Related

Error in file(file, "rt") : invalid 'description' argument when reading csv files

I have seen posts about this issue before, but none exactly like the issue that I am having. This code has been working for me with previous versions of R. I recently updated my R and R Studio to versions R 4.2.1 and RStudio Desktop 2022.07.1+554, and now I am getting the subject error when I try to read in my data files. The data files all have the same filenames. I point to the top level directory and then the code goes down through the folder structure and pulls out all of the data files to be used by the rest of the program.
Also want to mention that I am not that well versed in R, so I may not be doing everything in the best manner. Any suggestions that anyone can provide would be most appreciated.
Here is my code to select the top level folder, search through those folders and then read the files which is generating the error.
wd <<- choose.dir(caption = "Select top level folder where your data is located")
setwd(wd)
#List the full path and filename of all files in the working directory and sub-directories that starts
#with "DINum" and ends with ".csv"
out_files <- list.files(pattern = "^DINum(.*)csv$", recursive = TRUE)
# initialise list to store csv files
list.data <- NULL
# create a loop to read in data
for (i in 1:length(out_files))
{
list.data[[i]]<-read.csv(out_files[i], check.names = TRUE)
}
I found a solution. I have been using read.csv from base R. I installed the readr package and tried read_csv and it is working fine. Not sure why read.csv no longer works, but from what I read online, read_csv is a better choice anyway for large data files. Thanks to everyone that tried to help me. I appreciate your time!

excel_sheets produces: Error: `path` does not exist: ‘AE800’ but rstudio has the file saved in my environment and I can open file

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.
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.

Unable to read SPSS file from working directory

I have created a folder that has my dataset and set it as my working directory in Rstudio.
The dataset is an SPSS file which I named "Stats in R", I downloaded the packages foreign and Hmisc and tried to run to the following command to get the dataset:
data = read.csv("Stats in R.csv", header = TRUE)
…but the console showed the following message:
Error in file.choose() : file choice cancelled.
The problem is, my dataset is in the working directory but whenever I try to open it, R shows me an empty folder. I run the dir() command and instead of getting the directory content in the console, I get the following message:
character(0).
I really don't know what's the problem, the SPSS dataset is saved as a SAV file. I tried other extensions as well like CSV but nothing worked. Any suggestions, please? I really need to sort out that issue soon. Thank you!
It seems like you have some issue with your working directory - it's either not where you think it is, or you don't have full permissions and something is blocking the R Studio interface from accessing it.
Since you want to get this resolved quickly, the approach I would try is importing the file using the entire file path instead of the working directory, and then you can troubleshoot your WD when you have time.
This should work:
df <- read.csv("C://Users//Mina//Folder//Subfolder//Stats in R.csv", header = TRUE)

importing excel files in r

i am trying to load an excel file in r studio but each time i run it
Error in read_excel("R/win-library/3.6/IMDB_data.xlsx", sheet = "IMDB_data",
:
could not find function "read_excel" this is displayed.
i have tried changing directory
saving the data to load, in the same as working directory
none of the articles resolve my issues concerned yet
tried changing directory
saving the file in the same place as my working directory
importing through choose directory
setwd("~/R/win-library/3.6")
library(readxl)
IMDB_data <- read_excel("R/win-library/3.6/IMDB_data.xlsx",
sheet = "IMDB_data", skip = 2)
Write R code using data “IMDB_data” to
Load CSV in R by skipping second row.
enter image description here
It seems like your readxl library is not loaded.
Do you get any errors when you run library(readxl)?
Your working folder shouldn't matter and you should probably avoid working in the R's library.
The read_excel command should read the file based on the path provided, but your error is not complaining about the missing file. It's complaining about the missing function.
Lastly, if you set the working directory to ~/R/win-library/3.6, then it would be enough to run the following code (provided your readxl library loaded correctly):
IMDB_data <- read_excel("IMDB_data.xlsx", sheet = "IMDB_data", skip = 2)

How to add external data file into developing R package?

I am building my R packages in Rstudio, I ran into some unexpected problem when I tired to create package' vignette. when I hit build/load panel in Rstudio, I got vignette error, while package's documentation was created. To possibly solve vignette error I got, I have to add external data to my packages, use this data to compile package vignette accordingly. I used devtools::install() command to install my packages, but inst/ directory is not created. extdata must be located in inst directory. I also used devtools::use_data() to add my data from my PC, but I can't able to add my external data. How can I load external data for my packages ? I think I should not manually create extdata and put external data over there. Why inst/ was not created when I used devtools::install() ? How to add set of csv files as external data into my packages ?
This is the toy helper function I am going to use in my vignette to read external data :
myFunc <- function(myDir, ...) {
files <- list.files(myDir, full.names = TRUE, "\\.csv$")
readMe <- lapply(files, read.csv)
return(readMe)
}
This is the first time I build R packages, getting some common error. My apology if my questions is not well stated.
to find files in inst/, I need to use system.file(), but I don't have this directory, plus myFunc accept file directory to to grab the files and read them as .csv, this is toy code chunk could be executed in vignette file :
```{r}
library(myPkg)
file.1 <- system.file("extdata", "xxx.csv", "myPkg")
file.2 <- system.file("extdata", "yyy.csv", "myPkg")
myFunc(list(file.1, file.2))
```
How can I load external data to my packages in order to compile package vignette by using this data ? Why inst/ not created when I hit devtools::install() ? Can anyone help me how to do this ?Thanks in advance :)
You should manually create inst/extdata/file.csv in the base directory for your project (where DESCRIPTION is). You can put all the files you want to access in that directory.
Then to get the files in function examples or your vignette:
files <- lapply(list.files(system.file('extdata', package = 'my_package'), full.names = TRUE), read.csv)
system.file() returns the path to the extdata folder, then list.files() will create a vector of all the files in extdata. Finally, running lapply() with read.csv() should read the contents of all the files into a single list for you.

Resources