`Terminal` vs `system()` in R - r

I tried running the following in R
system("Message=HelloWoRld;echo $(sed 's/R/r/' <(echo ${Message}))")
but it fails, while
Message=HelloWoRld
echo $(sed 's/R/r/' <(echo ${Message}))
works fine when copy pasted in the terminal. The issue seems related to <(..). When I do either which bash or system("which bash"), I get /bin/bash.
Why does the same command via system() or directly on the terminal window does not yield to the same output?
FYI, I am on Mac OS X 10.11.3. Bash is GNU bash, version 3.2.57(1) and R is R version 3.2.3.

system is not a terminal emulator, and it’s not running Bash. Your terminal runs Bash. To get the same effect with system, run the command inside Bash. E.g.
system('bash -c \'echo $(date)\'')
What’s more, your current Bash command is quite convoluted and uses unnecessary command invocations; you can achieve the same via the much simpler
sed s/R/r/ <<< $Message
#chepner makes the excellent point that another solution can be used directly in system without need to pass execution to Bash:
system("Message=HelloWoRld; echo $Message | sed 's/R/r/'")

Related

How to run an awk command?

I am trying to learn how to calculate polygenic risk scores and I am following a step-by-step tutorial (this one: https://choishingwan.github.io/PRS-Tutorial/plink/). However, I have been stuck trying to figure out how to run this command:
awk 'NR!=1{print $3}' EUR.clumped > EUR.valid.snp
Obviously, this is not something I can make run in R, but apparently by using system(), people said it should work. But it doesn't. I then tried to run this command in my own Windows command prompt but it doesn't recognize awk as an intern command.
I then tried to maybe update my command prompt with wsl --install (because that's the only conclusion I could come up to) but apparently my administrator account needs permission to do so.
It would be useful if you could mention a couple of things:
Does R give you an error?
I assume you are trying to run it on a linux system with awk installed?
To check if awk is installed, try running this in your linux terminal:
which awk
To run your awk command from within R, you should escape the ' characters with a \ in your awk command, and put the entire command within quotes:
system('awk \'NR!=1{print $3}\' EUR.clumped > EUR.valid.snp')

Embed R code into BASH unix script

We know that we can embed AWK or SED directly into a BASH script by
awk { ....}
. I wanted to do something similar to R such as
mybashscript.sh
R { .... }
without having to create a .R file and calling the .R file from bash using Rscript myRscript.R
Is there a way to do this. I have searched everywhere but was not able to find an answer. Please help.
You can easily embed R code directly into a bash script using the Rscript command, and specifically, with regards to your question, the -e expr construction. Check man Rscript.
An example:
#!/bin/bash
echo "This is a bash script"
echo "Running R code..."
Rscript -e 'cat("hello world")'
Bash is able to execute a statement from an arbitrary scripting language. It is in the form VARIABLE_NAME=$(INTERPRETER -c "EMBEDDED CODE"). In the case of R INTERPRETER -c would be replaced with R --arch i386 -q --slave -e. The --arch option can be any supported architecture such as i386 or x64, -q keep the interpreter from printing start-up info and --slave keeps the output as clean as possible.

Unexpected nohup behaviour

I have an executable file test, it contains
a="$RANDOM"
echo "$a">>out
Now, if I simply ./test then out contains a random number. But if I nohup ./test & then out is empty. Why?
Transferring comment into an answer
Are you on Ubuntu (or another Linux distro)? Is /bin/sh a link to /bin/dash rather than /bin/bash? If so, when you run it with bash as your shell, then $RANDOM works, but when nohup runs it with /bin/sh (aka dash), you get nothing.
You might be able to fix it by using #!/bin/bash as the shebang line.
Actually I'm on Debian. But anyway I forgot the shebang indeed; now it works perfectly.
Great!
Incidentally, however tempting it is, it's generally a bad idea to call programs test because there is a shell built-in called test (also known as [) that can readily cause confusion if it is run instead of your test program.

Rscript in silent mode

I am using Rscript to run an R script but I get a lot of output on my screen. Can I run Rscript in silent mode (meaning without any screen output)?
Several options come to mind:
within R: use sink() to divert output to a file, see help(sink)
on the shell: Rscript myscript.R 2>&1 >/dev/null
edit the code :)
on Linux, use our littler frontend as it runs in --slave mode by default :)
Options 3 is the most involved but possibly best. You could use a logging scheme where you print / display in "debug" or "verbose" but not otherwise. I often do that, based on a command-line toggle given to the script.
You can redirect the output with
Rscript myscript.R >& >/dev/null (linux)
or
Rscript myscript.R >$null (windows)
or use R directly:
R --quiet --vanilla < myscript.R
or
R CMD BATCH myscript.R
(That last version writes the output to a file myscript.Rout)
One more option: if you want to separate the output and the error message into different files, which makes it easier to identify the problems, you can use the command on the shell:
Rscript myscript.R >a.Rout 2>a.Rerr
This will write the program output to a.Rout and the error messages to a.Rerr. Note that the files of a.Rout and a.Rerr should be removed beforehand, to avoid an error.

Executing Shell Script via Automator

via Terminal/Totalterminal or iTerm, this script works very well:
cd ~/go/to/dir/ && R -e "shiny::runApp("/go/to/dir", launch.browser=TRUE)"
but as/in a App via Automator, the second Part wont work.
In Automator: run Shell-Script.
Where is the difference of "normal" Terminal and the Terminal used by Automator.
In both /bin/bash
Is r on the path in Automator? If not, replace r with the full path like /usr/local/bin/r (shown by which r).

Resources