"The system cannot find the file specified whenever I knit" - r

Whenever I knit to a PDF in RStudio, the error "The system cannot find the file specified."
Here is the code that I'm using:
##importing data
library(readr)
Quiz1data_2 <- read_csv("C:/Users/erinp/Downloads/Quiz1data-2.csv")
I have restarted RStudio multiple times and I have copied and pasted the exact link that my file is saved to and it's still not working.
What am I not seeing or what am I not thinking?

Some suggestions/questions:
Without knitting, when you run the line reading in the csv, does it work?
Also, are you sure that the error is referring to the data csv? Could it be referring to the (I'm assuming) markdown file you are writing your code in? Have you moved that file since you started working in it?
Are you able to knit other documents to pdf? You need MiKTeX on a windows machine. Does knitting to html work?
I've found R to be a little tricky reading in files. I usually use the base function like this:go to the environment tab>import dataset>from text(base)> (select the file you want, hit open)>(select settings so that the dataframe preview looks right>import. Code that does this will run in the console, and I copy it into my markdown file so that every time it knits, it replicates that successful process.

I figured it out. It's because I forgot to assign a value to an object so it wouldn't knit into a PDF. I made a comment earlier, but it's small so I thought I would add this to the answer section.

Related

Reading Excel: evaluation error with zip file cannot be opened [duplicate]

I am using R with readxl package. I try to import an Excel file with the following command:
library(readxl)
city_codes <- read_excel("./data/file.xlsx", sheet = "city_codes")
It says it is a zip file and cannot be opened:
Error in sheets_fun(path) :
Evaluation error: zip file './data/file.xlsx' cannot be opened.
Any ideas?
I had this error, but for me, it was just that I had the sheet open in Excel while trying to read it into R. I guess the package wrongly perceives it as a zip file when it's trying to read it while Excel has partial ownership of it (and this blocks the read).
The error message is readxl's funny way of saying "file not found". That exact line of code gives me the exact same error, and the file doesn't even exist for me.
Note: I'm on version 1.0.0 of readxl
For me the "Evaluation error: zip file" error message also appeared when the excel spreadsheet was still opened in MSExcel.
You can specify a path to a file, only if it is nested on the working directory.
For example: If your working directory is MyWD and there is a folder in it, named MyData and another folder within MyData named MyNestedData, and finally myExcelFile.xlsx
read_excel("MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will work
read_excel("MyWD/MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will not work
If your excel worksheet is password protected, read_excel won't be able to access it and will give you this error.
If it needs protection, I would suggest p/w protecting the folder it's in, and then unprotecting the worksheet.
You may also get this error if you are using the wrong read function.
For example, read_xlsx("file.xls" ...) will throw the error.
This can happen if you forget to do the following before you use the read_excel function
setwd("C:\\map\\map_in_map\\map_in_map_in_map_where_the_file_is")
Try to change file restrictions if you are working on company's computer. This worked for me to solve this issue.
You may try to specify the full path instead of ./path/to/file
Try to make a new file. click "save as" and make a new name. Run again your code. it worked well to me :)
Although the solution was already given, but for the sake of documentation I will leave my situation here:
I ran into the same problem and couldn't figure out why. It seems that my excel file had closed with an error or something similar. I had to open the excel file, and save it again. When I ran R, everything worked smoothly.
The issue was that there was hidden files inside my working directory. I copied them to outside and exclude all directories. Now it works when I run the code.
In my case the xlsx was created using Excel 2007, which caused this issue. Xlsx files from newer Excel versions work.
In my case the file was not visible in the directory although I had deleted it. I had to search for the file using a software called "Everyting" and I deleted it. After that it worked.
For me, I was downloading a file from Google Drive into a tempfile with extension .xlsx... Realised the file in Google Drive was .xls so I changed it to that and it worked. Kinda rare situation though.
I got the same error message.
In my case the line that worked properly specified the sheet by a string (copied from excel). Suddenly, it did not work anymore (with the mentioned error message). I tried several things explained here. Ended up specifying the sheet by its number in the excel file - worked!
read_xlsx("table.xlsx", sheet="name_of_sheet") # suddently corrupt
read_xlsx("table.xlsx", sheet=4) # works
Why? No update of the package or something. I dislike to have to use a less specific line.
UPDATE: Restarting the R session made the initial line work again (although, I HAD checked the wd before...)

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)

write.csv executes with no errors but is not creating the csv

I am attempting to write a simple CSV of some data, but no CSV is being created. There are no error messages when I run the code, though. This code was working fine on Friday, but was not working when I came back today. I have tried using different working directories and tried different file names, but every time I check the folder there is nothing there. Has anybody experienced this before and/or do you have any additional troubleshooting ideas?
write.csv(x = map.data, file= "map.data.csv", row.names=F)
EDIT: I finally figured it out. It's because I was changing the WD inside a Markdown chunk, and I did not know that R automatically resets it after the chunk is run. Once I made the WD a global setting in the Markdown file, the CSV started showing up.
First, I would say saving a file as "map.data.csv" might be causing some issue. Try "map_data.csv".
Next make sure the working directory is the desired location (getwd and setwd)
Finally, make sure you have a non-empty data frame map.data
Otherwise I'm not sure what could be causing the issue with the info provided.

Output of saved .R files in r

I want to save my code in R. I did:
save(Data,file="Code_Data.R")
When I open the file in R again, the code looks like hieroglyphics.
How can I save the code in a way, that I can read the code in an editor or RStudio again?
save outputs a binary copy of the objects you tell it to save, not R code. Because you are naming this file with a ".R" extension, RStudio is blindly trying to open this binary file as R code, and you are seeing the results of that mess.
Technically, the R language doesn't care what the extension of the file is. As long as you know that the file contains, you can load it back in with the command load("Code_Data.R"). However, if you want to get RStudio to recognize that this is actually a file containing binary data and not R code, try saving the file with the canonical ".RData" extension:
save(Data, file="Code_Data.RData")
Using the ".RData" extension will also help you and other programmers who look at your code avoid this confusion in the future.

Reading Excel: evaluation error with zip file '..file.xlsx' cannot be opened

I am using R with readxl package. I try to import an Excel file with the following command:
library(readxl)
city_codes <- read_excel("./data/file.xlsx", sheet = "city_codes")
It says it is a zip file and cannot be opened:
Error in sheets_fun(path) :
Evaluation error: zip file './data/file.xlsx' cannot be opened.
Any ideas?
I had this error, but for me, it was just that I had the sheet open in Excel while trying to read it into R. I guess the package wrongly perceives it as a zip file when it's trying to read it while Excel has partial ownership of it (and this blocks the read).
The error message is readxl's funny way of saying "file not found". That exact line of code gives me the exact same error, and the file doesn't even exist for me.
Note: I'm on version 1.0.0 of readxl
For me the "Evaluation error: zip file" error message also appeared when the excel spreadsheet was still opened in MSExcel.
You can specify a path to a file, only if it is nested on the working directory.
For example: If your working directory is MyWD and there is a folder in it, named MyData and another folder within MyData named MyNestedData, and finally myExcelFile.xlsx
read_excel("MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will work
read_excel("MyWD/MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will not work
If your excel worksheet is password protected, read_excel won't be able to access it and will give you this error.
If it needs protection, I would suggest p/w protecting the folder it's in, and then unprotecting the worksheet.
You may also get this error if you are using the wrong read function.
For example, read_xlsx("file.xls" ...) will throw the error.
This can happen if you forget to do the following before you use the read_excel function
setwd("C:\\map\\map_in_map\\map_in_map_in_map_where_the_file_is")
Try to change file restrictions if you are working on company's computer. This worked for me to solve this issue.
You may try to specify the full path instead of ./path/to/file
Try to make a new file. click "save as" and make a new name. Run again your code. it worked well to me :)
Although the solution was already given, but for the sake of documentation I will leave my situation here:
I ran into the same problem and couldn't figure out why. It seems that my excel file had closed with an error or something similar. I had to open the excel file, and save it again. When I ran R, everything worked smoothly.
The issue was that there was hidden files inside my working directory. I copied them to outside and exclude all directories. Now it works when I run the code.
In my case the xlsx was created using Excel 2007, which caused this issue. Xlsx files from newer Excel versions work.
In my case the file was not visible in the directory although I had deleted it. I had to search for the file using a software called "Everyting" and I deleted it. After that it worked.
For me, I was downloading a file from Google Drive into a tempfile with extension .xlsx... Realised the file in Google Drive was .xls so I changed it to that and it worked. Kinda rare situation though.
I got the same error message.
In my case the line that worked properly specified the sheet by a string (copied from excel). Suddenly, it did not work anymore (with the mentioned error message). I tried several things explained here. Ended up specifying the sheet by its number in the excel file - worked!
read_xlsx("table.xlsx", sheet="name_of_sheet") # suddently corrupt
read_xlsx("table.xlsx", sheet=4) # works
Why? No update of the package or something. I dislike to have to use a less specific line.
UPDATE: Restarting the R session made the initial line work again (although, I HAD checked the wd before...)

Resources