interactive R startup script - r

I'm trying to run interactive R (Windows XP) with an input script that runs a few commands, and then leaves me at the R command line prompt. However, when I run it, it exits.
For example, here's the input file:
test.r:
x = 1
x
Here's what happens when I run it with the input file as a parameter:
C:\>R --file test.t
>x = 1
>x
[1] 1
C:\> <--- exits and returns to prompt
Is there any way to run this without R exiting?

I'd do it the other way around.
Just write a script that starts the normal R GUI or terminal application (per your choice), but then also place a file .Rprofile in the same directory which contains the code you want executed each and every time.
See help(Startup) on details about the files R looks up at startup and this may become clearer.

Related

R.exe and Rscript.exe

I'm new to programming and I'm confused on the difference between the two. I have googled this and I am still confused on the difference after reading the responses.
Part of the reason I am confused is I am thinking in terms of running script in Batch files. For instance, lets say I have a script in R and I create a batch file that runs the script where I use R.exe. When I put this in the command prompt and run the batch file, it just takes the script I made and runs it in the console of R right?
I've seen that you can run batch files uses Rscript.exe, which confuses me because when if I take an R script I made and put it into the script part of R (above the console) how would this do anything since the script must be put into the console for it to run. (Unless Rscript.exe runs whatever it is in the script part of R?)
If anyone could please explain how this all works to me, I would greatly appreciate it.
Thanks!
First, some terminology: even though the concept of batch processing is generic, and it means unassisted execution, the term batch file is usually reserved for MS-Windows files processed by cmd.exe, MS-Windows traditional script files. The term used for files containing R commands is usually R scripts, or Rscripts.
That said, please consider the following simple R script, named HelloFriend.R:
my.name <- readline(prompt="Enter name: ")
print(paste("Hello, ", my.name, "!"))
When run directly in R console, as
> source('HelloFriend.R')
it will show the output
Enter name:
If the user types Some Name and hits Enter, the program will output
[1] "Hello, Some Name !"
If it's run in the command line as R --no-save --quiet < HelloFriend.R, it will generate the output
> my.name <- readline(prompt="Enter name: ")
Enter name:
> print(paste("Hello, ", my.name, "!"))
[1] "Hello, !"
>
And finally, if run with Rscript --vanilla HelloFriend.R, it will generate the output
Enter name:
[1] "Hello, !"
In other words, when run inside the R console, the user input will be expected. When run under R, but in the command line, the program will not give the user the opportunity to type anything, but the command echo will be shown.
And finally, under Rscript, the user input will also not be expected, but the command echo will not be shown.
Rscript is the preferred form of running R scripts, as its name suggests. The passing of R scripts in the command line to R via redirection also gives batch processing but will echo the commands executed. Therefore it can help debug code, but it's not the preferred way of executing production code.
The analogy with batch files is a good one. R.exe is for interacting with the language, entering one statement at a time, and evaluating the results before entering the next statement. Rscript.exe is for running an existing script (file) containing R commands. You generally invoke Rscript.exe with the name of the script.
Running Rscript.exe my_script.R from the command-line is sort of like running
source("my_script.R")
q("no")
from the R console.

system2 does not save the output of a java command line to the file system, only output in console is created

I am trying to run a Java application (beagle2vcf) from R.
I tried with system() but no runs are performed only the help of the function is returned in the R console. But when I copy the same command to my cmd console the run is performed.
Then I try with system2() and only output in the console is created, but I need a file on my filesystem.
I am using the stdout parameter, but this does not seem to work.
Perhaps the fact that the Java application uses ">output" to generate files plays a role? Other Java applications that do not have this syntax to output the results run fine with system().
Example of command using system():
"java -jar beagle2vcf.jar 1 OneChrom.markers beagleTrain.bgl NA Chrom1Train.vcf"
Example system2 call:
system2(command="java",args=c("-jar","beagle2vcf.jar", j,"OneChrom.markers","beagleTrain.bgl","NA"),stdout=outnameTrainVCF,invisible=TRUE)
I run R3.5.0 on a windows 10 machine
The output should be a vcf file on my file system.

Run shiny application with arguments in terminal

Like in the topic, I'd like to run shiny app with parameters. I need to specify the database file's path from which I will grab the data. The problem is that the file changes sometimes thus I have to modify the file.path every time.
This is the command I use when running application from terminal
R -e "shiny::runApp('../Shiny_visualization')"
I tried
R -e "shiny::runApp('../Shiny_visualization')" --args 'db_path' yet I got an error.

Can I start an Rcmdr session from a unix shell?

I want to launch Rcmdr as a command from bash (or any unix shell), perhaps as an alias. R accepts the CMD argument and I could also pipe a script in with <. I would like the R console to stay open, and an interactive RCommander session to be started (Rcmdr is a popular GUI for R, for any newbies reading along, and it seems that you start up R, type library(Rcmdr) and then Commander() to start it up).
I am aware of how to add Rcmdr to my profile, and it appears to always start up if I include library(Rcmdr) in my .Rprofile, on my Linux workstation.
If I pipe my input in with < then this script works up to the point where it says that Commander GUI is launched only in interactive sessions:
library(Rcmdr);
Commander();
However if I run R CMD BATCH ./rcommander.r it just starts up and shuts down immediately, probably giving me some warning about interactive sessions that I didn't see, because CMD BATCH puts R into non-interactive mode and is thus useless for the purpose of "injecting" Rcmdr into an interactive R session.
It appears impossible to "source a file on the command line but run interactively" in R. It also appears that there are command line options to ignore the global and the user profile, but not to specify a custom profile like R --profile-custom ./.Rprofile2
Either I would like to specify a profile that means "Right now I want to start up and use RCmdr" and still be able to run R without it sometimes.
Working on an Ubuntu machine here, I was able to use the advice provided by Dirk in this mailing list post:
nathan#nathan-laptop:~/tmp$ cat rcommander.r
#!/bin/bash
r -lRcmdr -e'while(TRUE) Commander();'
nathan#nathan-laptop:~/tmp$ cat rcommander2.r
#!/bin/bash
Rscript --default-packages=Rcmdr -e 'while(TRUE) Commander();'
The first script uses Dirk's littler package, available on CRAN, and the second uses the standard Rscript executable. As noted, you can kill the process with ctrl + c from your terminal.

Start RScript without DOS box (on Windows)?

For a tcltk application, I would like to start an R script without opening a console window or starting a DOS box. I already figured out that by creating a link to RScript.exe I can force the console window to start minimized, but I wonder if I can avoid the console window at all?
You want to "run R in batch mode". It's quite straightforward; there are some instructions here.
EDIT: I don't see a console window; here are the steps I took.
1) I created a file named r.bat containing the line Rterm --vanilla and saved it in the R startup working directory (as given by Sys.getenv("USERPROFILE")).
2) I created a test R script, test.r, that would take several seconds to run (so I'd have chance to see any windows popping up.
n <- 1e3
for(i in 1:10)
{
qr.solve(matrix(runif(n*n), nrow = n), seq_len(n)/(n+1))
}
Obviously you can run any script that you like.
3) I opened a dos command prompt in the same dir as r.bat and typed R <test.r> test.txt.

Resources