How to execute a makefile from R - r

I am new to using makefiles in R, so excuse the really simple question: How do I execute a makefile which resides in a folder using R?
Is there a way to do so using a .R script? If so, what would the command line include?

I found the following solution to my problem in calling make using R (assuming you use Windows and RStudio):
Turns out you first need to add Rtools to your PATH
Select Avanced System Settings / Environment Variables / System Variables New.
Add the following to PATH:
C:\Rtools\gcc-4.6.3\bin
C:\Rtools\bin
Now Reboot.
After this is done, reopen your R console. Now you can use #yihui's servr package, and call a folder containing a makefile as follows:
servr::make(dir="path containing Makefile")
Hope this helps other newbies to makefile too...

Related

How to make R executable file

Is it possible to create an executable file where I can just upload the excels and an output is generated based on the coding and without sharing it as well.
On Linux or OSX you can make an R script double clickable with the shebang trick:
Add #!/usr/bin/Rscript as the first line
Make the script executable with chmod +x
On OSX, there is also the option to convert an R script into an application bundle with Platypus. On Windows, I do not know whether this is easily achievable at all, especially as executables usually are not placed in the search path on Windows.
is using R-Shiny apps can cover that? Its an R based program and you can customize the output / logic there.

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"
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.

r modify and rebuild package

I'm trying to use the SemiMarkov package and I want to change one small line of code in there. I've done some digging via:
getAnywhere("semiMarkov")
& I've identified that I want to change this line:
hessian <- diag(ginv(hessian(V, solution)))
to try something like:
hessian <- diag(ginv(pracma::hessian(V, solution)))
How do I go about this? Do I need to rebuild the package from scratch, and if so do I need rTools etc for this, or is there a simple-ish workaround (I'm a relevant R novice)? I've done some searching online and can't find anything obvious. Any ideas/pointers gratefully appreciated.
If you'd like to simply test out the effect of that change in an interactive R session, you can do so using trace(). Here's how:
Type trace("semiMarkov", edit=TRUE)
In the text editor that that launches, edit the line of interest.
Save the modified file.
Close the text editor
Back in R, use the modified function.
Linux environment
Starting with downloading the package source from CRAN.
This is the landing page: https://cran.r-project.org/web/packages/SemiMarkov/index.html
This is the package source: https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
Download and extract the source:
wget https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
tar -xvzf SemiMarkov_1.4.2.tar.gz
This should result in a directory named SemiMarkov. Open up the source (cd SemiMarkov), and modify as necessary.
Next, build the changes:
cd ..
R CMD build SemiMarkov/
This will result in a new archive file named SemiMarkov_1.4.2.tar.gz.
Lastly, install your modified archive:
R CMD INSTALL SemiMarkov_1.4.2.tar.gz
Windows environment
I'm less familiar with the Windows platform. *nix tooling is available in Cygwin, but it's painful. Instead, as Josh O'Brien points out, you should follow the Windows-specific instructions in the R Installation and Administration manual.

Is there an approach for distributing R command-line scripts with an R package? [duplicate]

I am interested in providing a command line interface to an R package called Slidify that I am authoring. It uses Rscript and I think that would make it cross-platform. The scripts are stored in the subdirectory inst/slidify. In order to use the script from any directory, I added its path to my .bash_profile as I am on a Mac.
My question is
How should I handle installation of the script in an automated cross-platform way?
How can I make sure that the file permissions are retained in this process?
What should the shebang line for the script be? I am currently using
#!/usr/bin/Rscript --vanilla --slave
I would appreciate pointers on how to handle this and any examples of R packages that already do it. Just to make sure, I am clear on how this would work, a user would be able to generate a slide deck from slides.Rmd by just running slidify generate slides.Rmd from the command line.
UPDATE:
Here is how I install it on a Mac from the command line. I use the excellent sub library by 37 signals to create the scripts.
echo "$(path/to/clidir/slidify init -)" >> ~/.bash_profile
exec bash
Two follow up questions
Can I package these commands into an R function install_slidify_cli?
How can I mirror these commands for Windows users?
Lovin' slidify so would be glad to help.
But in short, you can't.
R packages simply cannot install outside of $R_HOME or the chosen library folder. Ship the script in the package, and tell users to copy it. If there was a better way, out littler package with predecessor / alternative to Rscript would long have used it, and roxygen / roxygen2 would also have shipped something.

Including Command Line Scripts with an R Package

I am interested in providing a command line interface to an R package called Slidify that I am authoring. It uses Rscript and I think that would make it cross-platform. The scripts are stored in the subdirectory inst/slidify. In order to use the script from any directory, I added its path to my .bash_profile as I am on a Mac.
My question is
How should I handle installation of the script in an automated cross-platform way?
How can I make sure that the file permissions are retained in this process?
What should the shebang line for the script be? I am currently using
#!/usr/bin/Rscript --vanilla --slave
I would appreciate pointers on how to handle this and any examples of R packages that already do it. Just to make sure, I am clear on how this would work, a user would be able to generate a slide deck from slides.Rmd by just running slidify generate slides.Rmd from the command line.
UPDATE:
Here is how I install it on a Mac from the command line. I use the excellent sub library by 37 signals to create the scripts.
echo "$(path/to/clidir/slidify init -)" >> ~/.bash_profile
exec bash
Two follow up questions
Can I package these commands into an R function install_slidify_cli?
How can I mirror these commands for Windows users?
Lovin' slidify so would be glad to help.
But in short, you can't.
R packages simply cannot install outside of $R_HOME or the chosen library folder. Ship the script in the package, and tell users to copy it. If there was a better way, out littler package with predecessor / alternative to Rscript would long have used it, and roxygen / roxygen2 would also have shipped something.

Resources