I tried to download file from function "download.file" and I find this problem when I tried to write this code
fileurl1<-"https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl1, destfile = "./C:/Users/DELL/Desktop")
Error in download.file(fileUrl1, destfile = "./C:/Users/DELL/Desktop") :
cannot open destfile './C:/Users/DELL/Desktop', reason 'Invalid argument'
Destination needs to be a path to csv file and not to the folder. Try :
download.file(fileurl1, './C:/Users/DELL/Desktop/temp.csv')
Related
I am trying to download an nc file with R. It downloads well but I get this error when trying to open it:
Error in R_nc4_open: NetCDF: Unknown file format Error in
nc_open("SM_D2010323_Map_SATSSS_data_1day.nc") : Error in nc_open
trying to open file SM_D2010323_Map_SATSSS_data_1day.nc
(return_on_error= FALSE )
url <- "https://www.star.nesdis.noaa.gov/data/socd1/coastwatch/products/miras/nc/SM_D2010323_Map_SATSSS_data_1day.nc"
destfile <- "***/SM_D2010323_Map_SATSSS_data_1day.nc"
download.file(url, destfile)
nc_data <- nc_open('SM_D2010323_Map_SATSSS_data_1day.nc')
But when I use the same URL on my web browser, I can open the file without any problems with R.
I'm having trouble download a .pdf from the internet into Rstudio. I would like to analyse the .pdf using the pdftools package. I have a directory called files that I want the .pdf to go to. I'm using this code.
download.file('https://www2.gov.scot/Resource/Doc/352649/0118638.pdf', 'files')
I get this error:
Warning messages:
1: In download.file("https://www2.gov.scot/Resource/Doc/352649/0118638.pdf", :
URL https://www2.gov.scot/Resource/Doc/352649/0118638.pdf: cannot open destfile 'files', reason 'Is a directory'
2: In download.file("https://www2.gov.scot/Resource/Doc/352649/0118638.pdf", :
download had nonzero exit status
Is there way to get around this message?
The destfile has to be the filename (not the directory name) for the downloaded file.
For example, if we were to download the file above and save it as "Commission.pdf" in the files folder we would do the following:
download.file(url='https://www2.gov.scot/Resource/Doc/352649/0118638.pdf',
destfile="files/Commission.pdf")
You're passing in file to the destfile, which prompts R to throw the error warning that the argument you specified is a directory.
You miss the function assignature. It is
download.file(url, destfile, ...)
Therefore, when you're using download.file('https://www2.gov.scot/Resource/Doc/352649/0118638.pdf', 'files'), you are downloading the file https://www2.gov.scot/Resource/Doc/352649/0118638.pdf and saving it with the name files.
What you need to do is to modify the second argument to encopass the complete file path. It can be something like this:
download.file('https://www2.gov.scot/Resource/Doc/352649/0118638.pdf', 'files/0118638.pdf')
I try to download an excel file using download.file().
If I go directly to the link using the browser, I can download the file without problems.
However, using download.file does only download a broken file with Excel error: "The file you are trying to open is in a different format than specified by the file extension."
Here is my code:
url <- "http://obieebr.banrep.gov.co/analytics/saw.dll?Download&Format=excel2007&Extension=.xlsx&BypassCache=true&path=%2Fshared%2fSeries%20Estad%c3%adsticas%2F1.%20Tasa%20Interbancaria%20%28TIB%29%2F1.1.TIB_Serie%20hist%C3%B3rica%20IQY&lang=es&NQUser=publico&NQPassword=publico&SyncOperation=1"
download.file(url, destfile = paste0(base_dir, "test.xls"), mode = "wb", method="libcurl")
Any ideas how to download this file?
Many thanks for your help!
Try this, it works for me:
download.file(url,destfile = "./second.xlsx",mode = "wb")
The file you are trying to download is simply not an excel file. Actually what you obtain is an html file (try to change the file extension to '.html', then open in your browser). So your code is not the problem.
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"
I have an url, and I want to download the file via R, I notice that download.file would be helpful, but my problem seems different:
url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile)
It doesn't work! I notice that if my url is in the form of xxx.pdf, then the code above is no problem, otherwise the file that is downloaded is corrupt.
Does anyone know how to solve this problem?
Setting the mode might be required to treat the file as binary data while saving it. If I leave that argument out, I get a blank file, but this way works for me:
url <- "http://journal.gucas.ac.cn/CN/article/downloadArticleFile.do?
attachType=PDF&id=11771"
destfile <- "myfile.pdf"
download.file(url, destfile, mode="wb")
I am trying to download an nc file with R. It downloads well but I get this error when trying to open it:
Error in R_nc4_open: NetCDF: Unknown file format Error in
nc_open("SM_D2010323_Map_SATSSS_data_1day.nc") : Error in nc_open
trying to open file SM_D2010323_Map_SATSSS_data_1day.nc
(return_on_error= FALSE )
url <- "https://www.star.nesdis.noaa.gov/data/socd1/coastwatch/products/miras/nc/SM_D2010323_Map_SATSSS_data_1day.nc"
destfile <- "***/SM_D2010323_Map_SATSSS_data_1day.nc"
download.file(url, destfile)
nc_data <- nc_open('SM_D2010323_Map_SATSSS_data_1day.nc')
But when I use the same URL on my web browser, I can open the file without any problems with R.