Download from automatic download link and save changing directory in R

Asked

Viewed 276 times

2

I have a list of links that automatically download pdf files and would like to save them with a new name in a folder that I created with dir.create().

I thought of originally importing the file as an R object but the pdf is not available online, just for download. I also could not change the download settings with the download.file().

A piece of my code:

i <- "/consulta/dowload-arquivo-anexado/id/1229" 
#isso aqui vai ser um loop futuramente, por isso esse i

#url do documento
url2 <- paste0("http://oportunidades.mda.gov.br" , i)

#criando diretório que eu irei salvar meu documento
pasta <- gsub('.*/ ?(\\w+)', '\\1', i)
dir.create(paste0(dir_base, pasta))
destino <- paste0(dir_base, pasta)

#o que não deu certo   
download.file(url= url2,
              destfile= destino)
[1]Error in download.file(url = url2, destfile = destino) : cannot open destfile '.Documents/1229', reason 'Permission denied'

Thanks for your help!

  • the problem pointed out by R is the permission of the folder ". Documents", are you sure this is the folder you want to save the file in? (with the . before Documents)

  • Provide the content of dir_base can help.

  • I ended up deleting the part I create dir_bases. It is dir_base <- "C:/Users/coliv/Documents/" only a base folder to create the other directories.

  • i changed the text of the document folder just to make it cleaner. It points to a folder that actually exists.

  • And the mistake remains the same?

  • yes, the error remains the same

  • 2

    Tries to redefine the destino before. Thus destino <- paste0(dir_base, pasta, "download.pdf"). And then run the download line again

  • 2

    @Tomásbarcellos worked! In addition I also needed to enter the method = "Curl" parameter. Thank you very much!!

Show 3 more comments

1 answer

1


The error was happening because the content of destino was a folder and not the name of a file. Change destino for

destino <- paste0(dir_base, pasta, "/download.pdf")
destino
# [1] ".Documents/1229/download.pdf"

fixes the problem by pointing to a file and not a folder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.