8
votes

I am building an R package (let's call it "pkg"), and would like to write a function that downloads a file from the internet and saves it in the "inst/extdata" directory in my package's directory.

 download_file <- function(link) { 
     path <- ... # path to where "pkg" is stored
                 # something along the lines of ....../pkg
     download.file(link, paste(path, "inst/extdata", "newfile", sep = ""))
 }

Could you please help me get the "path"? While developing the package, I can just do "getwd()" -- however, when a user calls my package, her working directory might not be the pkg directory. In which case, how do I get the path to my package's directory?

1
Package location can often be read-only. I would advice against doing so in a package. - Andrey Shabalin
Anyway, the function you are looking for is find.package. - Andrey Shabalin
system.file('extdata', package = 'pkg') but you should probably use tempfile/tempdir or have the user specify a location. you also will not see the /inst folder under list.files(system.file(package = 'pkg')) - rawr
thanks, both the above seem to work. - Karan Tibrewal

1 Answers

8
votes

Just run .libPaths() and it will show you the filepath to your library folder. Inside the library folder you can see all your packages.