0

In R packages there can be a directory exec which contains some executable scripts. I have such a script called json_merge.R in my package numericprojection. This gets installed to ~/R/x86_64-redhat-linux-gnu-library/3.6/numericprojection/exec/json_merge.R.

To execute it I can of course specify that particular path and call it with Rscript from the command line. I was wondering whether there is some way to have R resolve this path such that I could just specify json_merge.R and numericprojection.


In the meantime I constructed this here:

r_libs_user="$(Rscript -e "cat(Sys.getenv('R_LIBS_USER'))")"                       
script="$r_libs_user/numericprojection/exec/projected_merge.R"                     
script="${script/#\~/$HOME}"  # https://stackoverflow.com/a/27485157/653152        

"$script" 
Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
  • Why don't you just wrap `json_merge.R` in an internal or exported function? – JBGruber Dec 02 '19 at 11:47
  • As far as I understand the use of `exec` is discouraged and also the idea seems to be that you place scripts in other languages in this folder: https://stackoverflow.com/a/26109815/5028841 – JBGruber Dec 02 '19 at 11:50
  • Having a stand-alone script has the advantage that I can execute it line-by-line in RStudio. But actually I can do that with an argument-less function as well. And I can make all the utilities private. Yes, that makes more sense! – Martin Ueding Dec 02 '19 at 11:55
  • I answered it anyway since there is an easy enough way to do that. If `json_merge.R` is something you want to show the user, you could think about writing a vignette instead if the script. This way it's also easier to provide comments to what the script is doing. – JBGruber Dec 02 '19 at 11:57

1 Answers1

1

That's what the system.file command is for. In your case that command should look like this:

system.file("exec", "json_merge.R", package = "numericprojection")

And will return:

~/R/x86_64-redhat-linux-gnu-library/3.6/numericprojection/exec/json_merge.R

If that is where the file was installed.

However, I think that your question is likely based on a misunderstanding as outlined in the comments.

JBGruber
  • 11,727
  • 1
  • 23
  • 45