Permission Denied Error when downloading a file - r

I am trying to download an excel zip file into a R Project folder that I have started. I'm new to R so I'm a little puzzled at the error message that I'm receiving.
The file is an excel file and so first I created a variable for the file:
excel2file="http://op.nsf.data/dataFiles/Housing2013EXCEL.zip"
Then I used the coding:
download.file(excel2file, destfile= "~/Home/Documents/Data")
I receive this error message:
Error in download.file(excel2file, destfile = "~/Home/Documents/Data") :
cannot open destfile '~/Home/Documents/Data', reason 'Permission denied'
I tried looking at other examples of permission denied and I think it may be my destination file but I am not sure why or the steps to trouble shoot it.

destfile should be a filename, not a directory. For example:
download.file(excel2file, destfile= "~/Home/Documents/Data/Housing2013EXCEL.zip")
Also, that URL doesn't seem to be valid, but that's a different (non-R) problem.

Make sure you don't have the file already open in the destfile, as the Permission denied error also comes from being unable to overwrite an open file.
This tripped me up for a while.

You could use the package readr and its function read_csv. It allows you to download the file, but it has to be a downloaded files such as a CSV file, something like this worked for me
library(readr)
newz <- read_csv("https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2020-financial-year-provisional/Download-data/annual-enterprise-survey-2020-financial-year-provisional-csv.csv")
View(newz)
about the "Permission Denied" I still doesn`t solved that problem

You can also use basename with paste, which would be useful if downloading a bunch of files.
For example:
(excel2file="http://op.nsf.data/dataFiles/Housing2013EXCEL.zip")
(file_name <- basename(excel2file))
download.file(excel2file, destfile= paste("~/Home/Documents/Data",file_name, sep="/"))

add a name in destfile like /downloadedfile.csv"

Related

R unable to write CSVs post update [duplicate]

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:
I create a simple data vector to export
x <- c(1,3,4,3,5,7,5,8,2,5,7)
I try to export with:
write.csv(x,file='whatever.csv')
And I get an error:
error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open file 'whatever.csv': Permission denied
How can I solve this?
First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.
To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.
I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.
write.csv(data, file="C:/project/file.csv")
If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.
I got this error today and fixed it by granting everyone write permission to the folder.
Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"
I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.
Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:
library(dplyr)
library(hflights)
path_to_hflights_as_csv <-
file.path(path.expand("~/MyHomeSubDir"),
"hflights.csv")
write.csv(hflights, path_to_hflights_as_csv)
path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".
Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):
https://en.wikipedia.org/wiki/Home_directory
https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).
I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'censoredpath.file.csv': Permission denied
I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

How to remove a 'permission denied' file from folder within R

I am downloading a large xlsx file as a part of a function. This file is removed with file.remove() in linux and mac but I have permission denied in windows machines. Below is the code for my function.
download.file(
'http://mirtarbase.mbc.nctu.edu.tw/cache/download/7.0/miRTarBase_MTI.xlsx',
'miRTarBase.xlsx', mode = "wb")
readxl::read_excel('miRTarBase.xlsx') -> miRTarBase
write.csv(miRTarBase, 'miRTarBase.csv')
read.csv('miRTarBase.csv', row.names = 1) -> miRTarBase
file.remove("miRTarBase.xlsx")
I get the following error message in my console
Warning message:
In file.remove("miRTarBase.xlsx") :
cannot remove file 'miRTarBase.xlsx', reason 'Permission denied'.
Again this warning only appears in windows.
Furthermore, after checking the properties of the file itself the 'Read-only' attribute is unchecked.
Following this, the following code works perfectly fine so I do not think the issue is with the folder either.
file.remove("miRTarBase.csv")
I believe the issue lies in how .xlsx files are treated in windows.
When I try to delete the .xlsx file while Rstudio is still running I get a File in use warning message. After closing the R session the .xlsx file can be deleted with no hassle.
This has confused me because I am not used to working with windows. Has anyone had this issue before? Would appreciate any help that can be given. Many thanks.
Have you tried saving as a temporary file in windows?
tmp <- tempfile()
download.file(
'http://mirtarbase.mbc.nctu.edu.tw/cache/download/7.0/miRTarBase_MTI.xlsx', tmp, mode = "wb")
readxl::read_excel(tmp) -> miRTarBase
write.csv(miRTarBase, 'miRTarBase.csv')
read.csv('miRTarBase.csv', row.names = 1) -> miRTarBase
file.remove(tmp)

R ftpUplad error: cannot open the connection

I am trying to upload a data.frame called 'ftp_test' via ftpUpload command
library(RCurl)
ftpUpload("Localfile.html", "ftp://User:Password#FTPServer/Destination.html")
and am getting an error:
Error in file(what, "rb") : cannot open the connection
In addition: Warning message:
In file(what, "rb") :
cannot open file 'ftp_test': No such file or directory
Could anyone tell me what is the issue here? Can I actually use data.frame and upload from r global environment ?
If I can't use the data.frame is there any workaround?
Many thanks,
Artur
You problem is, that you are trying to send an R object with an file transfer protocol. Since you are saving it there, you have to tell how to save it. A workaround is to save it as a file, upload it and then delete it on your local afterwards. Also saving as R.History is fine, but you need to transfer the R object to a file in some way. This example is used with an open ftp sever (uploads get deleted immediately, but you can try if it works)
filename="test.csv"
write.csv(df, file=filename)
#use your path to the csv file here instead of ".~/test.csv", you can check with getwd()
ftpUpload("~/test.csv", paste("ftp://speedtest.tele2.net/upload/",filename, sep=""))
file.remove(filename)
Also make sure your server is running. You can try your code with the open ftp server.

Getting "invalid entry size" when trying to import Excel xlsx file to R

Every time I enter this line
cameradata <- read.xlsx("./data/cameras.xlsx" , 1)
I get error:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod",
cl, : java.util.zip.ZipException: invalid entry size (expected 500
but got 502 bytes)
I have tried to clear RAM but file size is 10kp
Try this :-
May be this can work. It worked for me
1) when downloading the xlsx file, use this
download.file(fileURL, destfile="./whatever", mode="wb")
2) Switched to regular R, not R Studio,
The xlsx file you are trying to read maybe damaged. Try redownload the file or read another "healthy" xlsx file.
I had experience exactly the same issue.
What I did to resolve the problem was:
I had defined separate variable "fileURL2" and assigned "XLSX" download link
I had defined separate variable "cameraData2" and loaded XLSX file to it
I had downloaded file directly with Firefox and open it with MS Excel to assure it is OK, then I had save it to the working directory of "R" overwriting existing "cameras.xlsx" file
After new attempt to read the file with "R" read.xlsx() - was successful
In conclusion it seems that "R" had corrupted XLSX file during the download - which might be caused by a BUG inside of current version of the language.

Cannot export data to a file in R (write.csv)

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:
I create a simple data vector to export
x <- c(1,3,4,3,5,7,5,8,2,5,7)
I try to export with:
write.csv(x,file='whatever.csv')
And I get an error:
error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open file 'whatever.csv': Permission denied
How can I solve this?
First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.
To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.
I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.
write.csv(data, file="C:/project/file.csv")
If you don't specify a filename (i.e. C:/temp.csv) and just provide a file path, this same error pops up with both write.csv and write_csv.
I got this error today and fixed it by granting everyone write permission to the folder.
Steps: locate the folder on your PC, right-click and select properties, look for the "Security" tab and edit the permission for all to include "Write"
I got this error today because the file I try to rewrite on was open in another program. After I closed it, the problem solved.
Related: I was trying to save a csv to a relative path, that I built incrementally in Windows, but in my case the problem wasn't an error really, but a misunderstanding on my part - in the following code:
library(dplyr)
library(hflights)
path_to_hflights_as_csv <-
file.path(path.expand("~/MyHomeSubDir"),
"hflights.csv")
write.csv(hflights, path_to_hflights_as_csv)
path.expand("~/MyHomeSubDir") is mapped to "C:/Users/my.username/Documents/MyHomeSubDir" instead of "C:/Users/my.username/MyHomeSubDir".
Searching if it was some faulty config while installing R, I found that "home dir" in various Windows versions is indeed "C:/Users/my.username/Documents/" (and not "C:/Users/my.username"):
https://en.wikipedia.org/wiki/Home_directory
https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f
And when you pass a path including a sub directory that doesn't exist to utils::csv.write the error is similar (only the reason to not opening file is different - cannot open file 'C:/Users/my.username/MyHomeSubDir/hflights.csv': No such file or directory).
I just stumbled across this question in trying to figure it out myself. I had the exact same error message pop up a few times:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
After searching around and finding nothing that worked for me I restarted R and received the same message, but also a new error:
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'censoredpath.file.csv': Permission denied
I went to my file explorer and attempted to open the .csv in Excel and it notified me that it was locked by another user (someone else had the file open on their computer). So if it's not a problem with having access to the directory like what's already been suggested, try opening it in Excel to see if that might be the problem.
In case you tried everything and did not work, check your antivirus. In my case AVAST was causing this issue for some reason.

Resources