Default R output with a command line parameter - r

I am trying to provide a way of running unattended R scripts through Rscript. The problem I am having is that the default output for graphics is a PDF file in the current directory. I would like to redirect this output to a separate folder but without having to change the script too much.
If my script is something simple as:
args = commandArgs(trailingOnly = TRUE)
mydata <- get some data frame somehow
plot(tayside)
And I execute the following commandline:
Rscript.exe --vanilla --default-packages=RODBC,graphics,grDevices sample.R > C:\temp\sample.Rout
I get a Rplots.pdf in the current folder and the sample.Rout file in the C:\temp\ folder.
Is there a way to specify an output folder and have Rscript put all output there? I have tried playing with the pdf.options(...) to pre-pend a default folder to the file parameter but no can do.

Ok, apparently it was easier than I thought, no need to use pdf.options() but simply pdf() at the top of the file (after getting the arguments):
pdf(paste0(args[1], "MyFile.pdf"))
or, for multiple files:
pdf(paste0(args[1], "MyFile%03d.pdf"), onefile=FALSE)

Related

Outsource function/script permanently

In every Script I paste the same path to get some functions/r-scripts. Is there a possibility to save this path globally and permanently? It should be independently of the workingspace or other loading data paths and should be available on every new R-Session.
Thanks.
1) Place this in your .Rprofile file. The messsage line is optional but will allow you to confirm that .Rprofile was run when you start R.
message("Hello")
options("mypath" = "...path goes here...")
and then in your script get it using the following which will use that path if the mypath option was defined or the current directory otherwise. Omit the second argument if you don't want to have a default.
getOption("mypath", ".")
2) You could also just put this in your .Rprofile
mypath <- "...your path goes here..."
and then just refer to mypath in the script but that will cause mypath to clutter up your workspace which you may not want.
3) R also will read the environment variables defined in .Renviron so if that file contained:
MYPATH="...path goes here..."
then the script could get it via Sys.getenv("MYPATH") .
See ?Startup for more information on .Rprofile and .Renviron .
4) You could also create an exported shell variable on Linux or environment variable on Windows and then read it in the script using Sys.getenv .

Setting R environmental variable in Tortoise SVN

I have a collection of functions in a file called some_functions.R and saved in a SVN directory in C:\blah1\blah2\Rcodes\some_functions.R . I have several Rprojects which uses this code file. Say a R project is available in the directory C:\blah1\blah2\Rprojects\project1. I can use hard coded path to refer the file and it works.
source("C:/blah1/blah2/Rcodes/some_functions.R")'
But I would like to set the path as environmental variable.
Looking at How to unfold user and environment variable in R language? and setting the home directory in windows R I add the following line in the RProfile.site file
Sys.setenv(R_CODE_PATH = "C:/blah1/blah2/Rcodes")
and in the project1.Rnw file
source("R_CODE_PATH/some_functions.R")
But the project file can not read the some_functions.R file. I tried with %R_CODE_PATH% without any luck.
Not sure what I'm missing here. Any help is much appreciated.
You retrieve environment variables using Sys.getenv(). Try:
r_code_path <- Sys.getenv("R_CODE_PATH")
Then, for example:
source(paste(r_code_path, "some_functions.R", sep = "/"))
I would use the .Renviron config file to define environment variables. Put it in whatever directory the R command Sys.getenv("HOME") returns and include lines like this:
R_CODE_PATH=C:/blah1/blah2/Rcodes

Calling Skim from inside R

I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.

How to write an R program that copies its source code to a file?

I'm writing an R script whose contents can change from time to time. It would be really helpful if I could insert a command that would copy the current contents of the script to a file, so I can go back later and see exactly what commands I executed during that run of the code.
How can I do this?
You can do this with the teaching demos package:
install.packages("TeachingDemos")
library(TeachingDemos)
#Will write to a file in the working directory
txtStart("captureCode.txt")
#This comment will not appear in the file, all commands and output will
Sys.Date()
#This command ends writing to the file
txtStop()
Source

How to provide file input from command line arguments in R

I want my R script to accept data from a .csv file. Is there a way to do this from the command prompt.
Just like if I write Rscript myscript.R 20, it passes a value 20 as input. I want to know if specifying the absolute address of the csv file will allow my script to use the data inside the csv file. If not what do I have to do to achieve what I want?
Have a look at ?commandArgs. Minimal example:
#!/usr/bin/Rscript
print(commandArgs(trailingOnly=TRUE))
Run it:
./myscript.R yourcsvfile.csv
[1] "yourcsvfile.csv"
Maybe you will be interested in the getopt package, too.

Resources