How can I call a Windows Terminal inside an R script? - r

I need to call a Windows Terminal inside a R script,
I asked a similar question before, but using a Linux Terminal:
How to call linux terminal code from R script
I am using cdo (Climate Data Operators) in Linux, inside a R code
system(paste0("cd ~/Data/; cdo -f nc copy file1.grb2 file2.nc;"))
The answer in Linux is so simple, and it did the trick perfect. How can I do this in Windows?

Related

Run windows CMD in R

I am a beginner at R. If this question sounds childish to you then pardon me.
I need to run the following command in R studio by using Windows CMD.
copy *.txt combinedfiles.txt
This command actually combines all the files into one file in Windows CMD.
I tried using system() and system2(). But I could not get the results.
e.g.:
system(cmd.exe copy *.txt combinedfiles.txt)
Kindly help me out with this problem.
copy is not a standalone program, but a builtin command of the Windows commandline interpreter. To run this, use shell in R.
shell("copy *.txt combinedfiles.txt")

How to implement this solution to make R scripts interactive?

I have found this answer to a problem I had discussed previously here: How to get dialog/message box working in executable R file
The answer is here:
https://thesquareplanet.com/blog/interactive-r-scripts/
Specifically this bit:
We can use this to provide an interactive R script executor in /usr/local/bin/Rint: #!/bin/sh f=$1; shift; env "R_PROFILE_USER=$f" "ARGS=$#" R --no-save -q
My problem is that I know nothing about the command prompt or batch files. So what does this line mean, and do I just paste it into the command prompt once and then add "#!/usr/local/bin/Rint" to the top of my executable R script?
This line is not for a Windows cmd.exe shell. This is for a UNIX/Linux shell.
We can use this to provide an interactive R script executor in /usr/local/bin/Rint:
#!/bin/sh
f=$1;
shift;
env "R_PROFILE_USER=$f" "ARGS=$#"
R --no-save -q
For anyone still looking, this is the best resource I can find about executable R GUIs in Windows:
http://oddhypothesis.blogspot.com.au/2014/04/deploying-self-contained-r-apps-to.html
Still working through this tutorial, will update if it answers the question.

Detect if R Script is Running in Interpreter or from Command Line [duplicate]

Is it possible to determine - from within the script - whether the script is running in the R-GUI (specifically R.app on OS X) or whether it has been called from Terminal/command line (i.e. R --vanilla -f script.R)? If so, how is this possible?
I'm asking because I have a script that can run parallelized (using the doMC library), which should not be used from the GUI. Sometimes I need to further process the data calculated in the script, so I'd like to call the script from the GUI on these occasions.
Perhaps you are looking for interactive()?
In C, you would use the isatty function. If you could find an equivalent function in R (probably in a UNIX or file system library), that should help.

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.

How can I print R documentation from a Linux command shell (e.g. bash)?

How can I check documentation for R code from a Linux command shell such as bash? I DO NOT mean an interactive session.
With Perl, I can use perldoc to print out documentation at the command line:
perldoc lib
I was hoping for something simple like that for R. I don't always want to pull up a full interactive R session just to look up some documentation.
There might be other ways, but one that works for me is using the -e flag to execute code on the command line. I also use the --slave flag, which prevents anything from being printed to standard output (e.g. no R startup messages, etc.):
R --slave -e '?function'
I actually created a super small script I call rdoc to act like a simple R version of perldoc:
#!/bin/bash
R --slave -e "?$1"
After installing that in my ~/bin directory (or however you install it in your PATH), it's easy:
rdoc function
If you want to look at documentation of a function from a particular package, prepend the library name followed by two colons. For example, to pull up documentation of the dmrFinder function from the charm package:
rdoc charm::dmrFinder

Resources