Can I prevent R from creating .RData files? - r

I am trying to share some R code with colleagues who don't know R. I have created a batch file so they can just double-click it and run the R script without even opening R. But it creates a .RData file.
My question is, can I prevent R from creating the .RData file?
I've read here Disable saving history that I could disable it through RStudio global options but my colleagues are installing just R and won't need to ever open it, so I am looking for some kind of solution of the likes of options(...) that I can just put in my Rscript, or maybe something that could be speficied in the batch file call.

For anyone curious, I figured it out thanks to #r2evans and this post: https://es.stackoverflow.com/questions/166211/evitar-que-r-cree-ficheros-r-data-y-r-history.
I just had to add --no-save to my batch call like this:
R CMD BATCH --no-save "%file%" NUL
where %file% is the Rscript path. The NUL part is so a .Rout file won't be created after running the batch file.
Also, doing
Rscript.exe "%file%"
runs the Rscript and doesn't produce .RData or .Rout files. The difference is that R messages get printed on the command window as they would in an interactive R session.

Related

Executing a batch file in an R script

I would like to execute a batch file from a R script. The file is in a directory like \\network\path\to\batch\file.bat.
I know I can use the system command in R to run DOS commands but I can't simply use system("start file.bat"). So how would I best use R script to execute this batch file?
Try shell.exec("\\\\network\\path\\file.bat")
The shell.exec command uses the Windows-associated application to open the file. Note the double back-ticks.
Pro tip: write.csv(file='tmp.csv',tmpdat);shell.exec('tmp.csv') is useful (assuming you've associated CSV files with your preferred application for viewing CSV files) for quickly checking output.
try
shell('\network\path\to\batch\file.bat')
I found this problem when using RSelenium in Windows as well but using this batch file made sure to close all chromedriver processes. I was ending up with a ton of these processes after a lengthy scraping session.
My solution was to execute the batch file from within the R script every so often by using:
shell.exec(file.path(getwd(), "kill_chromedriver.bat"))
This is what I did on windows platform.
cmd='\\prj\\whatkit\\test.bat'
system(cmd, intern = TRUE, show.output.on.console = TRUE)

Execute R script in R studio using batch file

I want to execute a R script in R Studio using batch file. I know how to execute R script using batch file in R though. When I try to execute using the following:
"C:\Program Files\RStudio\bin\rstudio.exe" CMD BATCH --vanilla --slave "C:\Users\kpappala\Desktop\R schedule\task.R"
It just opens R studio but doesn't execute. Is there a way?
Thanks!
Rstudio is an IDE for R. It isn't R though. It doesn't really even make sense to run it in batch through rstudio.
If you're just saying you want to run a file from within Rstudio that's different and you can just source it or run it in batch via system using a call to R CMD BATCH.
This question was answered here:
batch execute R script.
In short and as Dason said, you Rstudio is just a shell, and doesn't actually run the code. For that use R. Try this instead:
PATH C:\Program Files\R\R-3.1.0\bin;%path%
Rscript "C:\Users\kpappala\Desktop\R schedule\task.R"

Using R and with the Terminal

I have combed the internet looking for a tutorial on how to use R through the terminal and I can't find anything that exactly fits what I'm looking for. I have a group of R scripts. They need to be run in a specific order. Each produces a set of .csv files to be used as inputs for the next script that will be executed.
I have used R CMD BATCH filename.R and it makes an output file called filename.Rout
But then when I try to use cat filename.Rout, I get an error Fatal error: cannot open file 'filename.R': No such file or directory
Also I'm not sure where my output (.csv files) should be written to. In my R script, there are some write.csv lines pointing the new datasets to be written to a specific directory but when I run R CMD BATCH filename.R in the Terminal, I don't see the files where they should be. I see a filename.Rout in another directory but I can't open it.

How to run a R language(.r) file using Batch file?

I want to run a R script file (.r) using batch file.
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question #chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r
Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

How do I convert Rd files to pdf for a package that I am creating in R?

I am writing a package in R and I would appreciate some example bash code to process the Rd files to latex and then to pdf.
It is in the directory ~/mypkg/dev/. I have generated the file structure and Rd templates.
from ~/mypkg/dev/man, I have tried
R CMD Rdconv -o mypkg-package.tex --type=latex mypkg-package.Rd
mypkg-package.tex file is generated, but
pdflatex mypkg-package.tex
generates tex without any preamble.
I have read the documentation in "Writing R extensions" and "R CMD Rdconv --help" on this subject, but no examples are provided.
Thank you
There are two issues here:
First, the Rdconv command only transforms one Rd file at a time; your question suggests that you want the full manual.
Second, the Rd2dvi command is your friend. I just ran the following on a local package:
R CMD Rd2dvi --pdf --title='Test of foo' -o /tmp/foo.pdf man/*.Rd
That should be what you asked for.
Try this. It worked for me.
found from Making an R package PDF manual using devtools
pack <- "name_of_your_package"
path <- find.package(pack)
system(paste(shQuote(file.path(R.home("bin"), "R")),"CMD", "Rd2pdf", shQuote(path)))
0. From question, it is assumed that you have .Rd files.
You may have obtained these .Rd files via roxygen package or some other ways. You need .pdf of your package. One way to do this is from Windows's command line.
1. Open Windows Command Prompt (in Administrator mode, if possible).
Start - type "cmd" - (optional: right click the appearing icon - Run as administrator)
2. Pass to the R's current working directory (it can be found via "getwd()" in R's console) in the command propmt. R's current working directory contains the folder with your package source.
cd C:\Users\erdogan\Documents\Revolution
3. For the sake of argument, say the package folder was called "causfinder". Run the following command in Windows command prompt:
(be sure that you have not got any causfinder.pdf in R's working directory (You may have obtained some R-development-incompatible causfinder.pdf with some other ways outside R). If there exists, delete causfinder.pdf first.
Otherwise, you get this error: "file 'causfinder.pdf' exists; please remove it first")
R CMD Rd2pdf causfinder/
This command performs .Rd --> LateX --> .pdf process automatically. You obtain causfinder.pdf in R's working directory.
This is further described in the manual on Writing R Extensions under the section on "Processing documentation files"
I have created a basic bash function, so I can just run rdoc from my command line. It generates the Rd files, creates the pdf, and opens it. I only use it for testing, not for creating the final documentation. You can add it by adding the following function to your .bashrc (or whichever you use) file
rdoc() {
echo -e "devtools::document()" | R --no-save
rm /tmp/rdoc.pdf
R CMD Rd2pdf -o /tmp/rdoc.pdf man/*.Rd
open /tmp/rdoc.pdf
}

Resources