簡體   English   中英

R中的下載文件錯誤

[英]Error in download file in R

我有一個excel文件,其中包含公司名稱和其.pdf文件的可下載鏈接。 我的目的是根據excel列中的公司名稱創建目錄,並將pdf文件下載到新創建的目錄中。

這是我的代碼

##Set the working directory
txtsrc<-"C:\\FirstAid"
        setwd(txtsrc)

##make a vector  file names and links
 pdflist <- read.xlsx("Final results_6thjuly.xlsx",1)
colnames(pdflist)

##Check if docs folder exists
if (dir.exists("FirstAid_docs")=="FALSE"){
dir.create("FirstAid_docs")
}

##Change the working directory
newfolder<-c("FirstAid_docs")
newpath<-file.path(txtsrc,newfolder)
setwd(newpath)

##Check the present working directory
getwd()

    ## Create directories and download files
    for( i in 1:length(pdflist[,c("ci_CompanyName")])){

    ##First delete the existing directories
    if(dir.exists(pdflist[,c("ci_CompanyName")][i])=="TRUE"){
        unlink(pdflist[,c("ci_CompanyName")][i], recursive = TRUE)
      }

    ##Create a new directory
    directoryname<-pdflist[,c("ci_CompanyName")][i]
    dir.create(directoryname,recursive = FALSE, mode = "0777")


      ##Get the downloadable links
      ##Link like :www.xyz.com thus need to add https to it
      link<-pdflist[,c("DocLink")][i]
      vallink<-c("https://")

       ##Need to remove quotes from link
      newlink<-paste0(vallink,link)
      newlink<-noquote(newlink)

      ##Set paths for the downloadble file
      destfile<-file.path(txtsrc,newfolder,directoryname)


      ##Download the file
      download.file(newlink,destfile,method="auto")

      ##Next record
          i<-i+1
    }

這是我得到的錯誤/結果

 > colnames(pdflist)
[1] "ci_CompanyID"   "ci_CompanyName" "ProgramScore"   "ID_DI"          "DocLink"       
> download.file(newlink,destfile,method="auto")
Error in download.file(newlink, destfile, method = "auto") : 
  cannot open destfile 'C:\Users\skrishnan\Desktop\HR needed\text analysis proj\pdf\FirstAid/FirstAid_docs/Buckeye Partners, LP', reason 'Permission denied'

盡管設置了chmod,為什么仍會出現錯誤。 我在Windows 64位計算機上使用CRAN RGui(64位)和R版本3.5.0。 任何幫助是極大的贊賞。

download.file destfile必須是特定文件,而不僅僅是目錄。 例如,

'C:\Users\skrishnan\Desktop\HR needed\text analysis proj\pdf\FirstAid\FirstAid_docs\Buckeye Partners, LP\myFile.pdf'

最終的工作代碼:

> ##Set the working directory txtsrc<-"C:\\FirstAid"
>         setwd(txtsrc)
> 
> ##make a vector  file names and links  pdflist <- read.xlsx("Final results_6thjuly.xlsx",1) colnames(pdflist)
> 
> ##Check if docs folder exists if (dir.exists("FirstAid_docs")=="FALSE"){ dir.create("FirstAid_docs") }
> 
> ##Change the working directory newfolder<-c("FirstAid_docs") newpath<-file.path(txtsrc,newfolder) setwd(newpath)
> 
> ##Check the present working directory getwd()
> 
>     ## Create directories and download files
>     for( i in 1:length(pdflist[,c("ci_CompanyName")])){
> 
>     ##First delete the existing directories
>     if(dir.exists(pdflist[,c("ci_CompanyName")][i])=="TRUE"){
>         unlink(pdflist[,c("ci_CompanyName")][i], recursive = TRUE)
>       }
> 
>     ##Create a new directory
>     directoryname<-pdflist[,c("ci_CompanyName")][i]
>     dir.create(directoryname,recursive = FALSE, mode = "0777")
> 
> 
>       ##Get the downloadable links
>       ##Link like :www.xyz.com thus need to add https to it
>       link<-pdflist[,c("DocLink")][i]
>       vallink<-c("https://")
> 
>        ##Need to remove quotes from link
>       newlink<-paste0(vallink,link)
>       newlink<-noquote(newlink)
> 
>       ##Set paths for the downloadble file
>       neway<-file.path(newpath,directoryname) 
>       destfile<-paste(neway,"my.pdf",sep="/")
> 
>     
>      
>       ##Download the file
>       download.file(newlink,destfile,method="auto")
> 
>       ##Next record
>           i<-i+1
>     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM