Copy the .rmd file included in the Rstudio add-on package to a user-defined directory.

I have the rstudio addin package located here .

One of the additives allows the user to define a directory and copy the file that is in the package to this directory.

file is located:

atProjectManageAddins/inst/Docs/RMarkdownSkeleton.Rmd 

And I'm trying to copy it to a user directory with something like this:

  file.copy("inst/Docs/RMarkdownSkeleton.Rmd", paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd")) 

Where am I trying to copy it from where it is in the package, where it defines it (based on two separate arguments, Dir and FolderName ).

But this does not seem to work. My assumption is that I am not looking at the package directory correctly. I tried ./Inst/ , ~/Inst/ and maybe a couple more. Now my assumption is that there is a more systematic reason for my inability to work file.copy() .

Any suggestions? Is it possible?

Please note that if I run the function locally through source() and runGadget() , it works fine. Only when the package is installed, and I use the graphical interface of adding RStudio, where it refers to the package with the installation, does it work. Thus, I am quite sure that I am not correctly defining the file path for the installed .Rmd files.

Edit: I changed to the following based on Carl's suggestion (as seen on github), but the files are still not being copied.

 file.copy(system.file("Docs","Rmarkdownskeleton.rmd",package="atProjectManageAd‌​dins"), paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd")) 
+5
source share
2 answers

system.file is the best function to get a file from a package. I believe this should work for you:

 file.copy(system.file("Docs","Rmarkdownskeleton.rmd",package="atProjectManageAd‌​dins"), paste0(Dir, FolderName, "/Reports/", FolderName, "_report.Rmd")) 
+4
source

You have the right idea by putting the files in inst/ .

Use this code to copy a file from the package directory to the current directory:

 file.copy(from = file.path(path.package("packagename"), "path/to/file"), to = file.path("path/to/file"), overwrite = T) 

file.path creates a path by concatenating the lines passed to it (OS-specific delimiters are automatically added).
path.package retrieves the path to the downloaded package. The files presented in inst/ are copied to the root directory of the dir package during installation, so the path "path / to /" here should be the path relative to your inst/ directory.
overwrite can be used to overwrite a file if it already exists.

In your particular case, this should do the trick:

 file.copy(file.path(path.package("atProjectManageAddins"), "Docs/RMarkdownSkeleton.Rmd", file.path(getwd(), "Reports", paste0(reportName, "_report.Rmd"))) 
0
source

All Articles