Martin Ueding
Martin Ueding

Reputation: 8699

Execute a script from the exec directory

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" 

Upvotes: 0

Views: 242

Answers (1)

JBGruber
JBGruber

Reputation: 12420

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.

Upvotes: 1

Related Questions