I require the number of Firefox processes running on Linux (Ubuntu) be stored in a variable in an R script. By itself the system2 command I use seems to work. However, when I add stdout = TRUE to capture the info in a character vector I get a warning. Why the warning?
system2(command = "ps", args = "aux | grep [f]irefox -c")
# 0
system2(command = "ps", args = "aux | grep [f]irefox -c", stdout = TRUE)
Warning message:
In system2(command = "ps", args = "aux | grep [f]irefox -c", stdout = TRUE) :
running command ''ps' aux | grep [f]irefox -c' had status 1
Use ef instead of aux as the argument to ps. aux for BSD and ef and variants for standard syntex, as per man ps.
system2('ps', '-ef | grep [f]irefox -c', stdout = TRUE)
[1] "12"
Related
I have created a salmonify function in R which takes all the fastq files in a folder and then runs the salmon quant function in command line one after another using the system function. However, after the system function completes the list successfully, the rest of the salmonify function will not run. ie. it does not print the statement, and if I try nesting the salmonify function in another function (to run it for multiple study folders), it stops at the same place.
I am using a linux ubuntu 18.04. The salmon function is the latest (1.1.0), and R is version is 3.6.
The code is:
salmonify_single = function(hardrive, study, index){
salmon_file = "salmon quant -i "
code1= " -l A -r fastq_files/"
code3 = " -o Salmon/quant/"
name = paste0(hardrive, "/", study, "_data/fastq_files")
L = list.files(name)
O = gsub(x = L, pattern = "_1.fastq", replacement = "")
code4 = O[! duplicated(O)]
lst = as.list(L)
lst3 = list()
for (i in seq_along(lst)) {
x = lst[[i]]
lst3[i] = paste0(salmon_file, index, code1, x[1], code3, code4[i])
}
for (i in lst3) {
print(i)
}
fold_name = paste0(hardrive, "/", study, "_data")
setwd(fold_name)
for (i in lst3) {
system(i)
}
print(paste0("Completed salmonify for ", study))
}
The output for printing the list that will run from
for (i in lst3) {
print(i)
}
is :
> salmonify_paired("DATA/RNAseq_Analysis3",
+ "dikovskaya", "salmon_index_k31")
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095296_1.fastq -2 fastq_files/SRR2095296_2.fastq -o Salmon/quant/SRR2095296"
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095297_1.fastq -2 fastq_files/SRR2095297_2.fastq -o Salmon/quant/SRR2095297"
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095298_1.fastq -2 fastq_files/SRR2095298_2.fastq -o Salmon/quant/SRR2095298"
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095299_1.fastq -2 fastq_files/SRR2095299_2.fastq -o Salmon/quant/SRR2095299"
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095300_1.fastq -2 fastq_files/SRR2095300_2.fastq -o Salmon/quant/SRR2095300"
[1] "salmon quant -i salmon_index_k31 -l A -1 fastq_files/SRR2095301_1.fastq -2 fastq_files/SRR2095301_2.fastq -o Salmon/quant/SRR2095301"
Then the salmon function begins, and then I can't get it to print Completed salmonify for.... Any help here would be much appreciated.
Best,
James
I'm trying to achieve what you might naively write as:
R -e "
rmarkdown::render(
'MyDocument.Rmd',
params = list(
year = 2017
),
output_file = 'ExampleRnotebook.html'
)
"
So that I can make nicely formatted submission scripts to run on a cluster.
I've tried some variants on the below, I'm wondering if there might be an alternative approach to do this with the R -f flag?
read -r -d '' EXP << EOF
rmarkdown::render(
'MyDocument.Rmd',
params = list(
year = 2017
),
output_file = 'ExampleRnotebook.html'
)
EOF
R -e "$EXP"
but I get a series of errors that look like this:
ARGUMENT 'params~+~=~+~list(' __ignored__
for the different lines of the expression, followed by:
> rmarkdown::render(
+
+ Error: unexpected end of input
To reproduce:
MyDocument.Rmd =
---
title: "R Notebook"
output: html_notebook
params:
year: 0000
---
```{r}
params$year
```
This works fine:
read -r -d '' EXP <<- EOF
rmarkdown::render('MyDocument.Rmd', params = list(year = 2017 ), output_file = 'ExampleRnotebook.html')
EOF
R -e "$EXP"
but gets hard to read with longer param lists
This works for me (R version 3.5.0):
R --no-save <<code
for(i in 1:3) {
i +
2
}
print(i)
runif(5,
1,10)
code
Note: line-breaks and paddings are intentional.
I need to run a perl command from within an R script. I would normally do this via:
system(paste0('my command'))
However, the command I want to paste contains both single and double quotes and an escape character. Specifically, I would like to paste this command:
perl -pe '/^>/ ? print "\n" : chomp' in.fasta | tail -n +2 > out.fasta
I have tried escaping the double quotes with more escape characters, which allows me to pass the command, but it then prints all 3 escape characters, which causes the command to fail. Is there a good way around this, such that I can save the above perl line as a string in R, that I can then pass to the system() function?
Hey I haven't tested your particular perl call (since it involves particular file/directory etc) but tried something trivial by escaping the quotes and it seems to work. You might want to refer this question for more as well.
My approach,
# shouldnt have any text expect for an empty string
my_text <- try(system(" perl -e 'print \"\n\"' ", intern = TRUE))
my_text
[1] ""
# should contain the string - Hello perl from R!
my_text2 <- try(system(" perl -e 'print \"Hello perl from R!\"' ", intern = TRUE))
my_text2
[1] "Hello perl from R!"
So based on the above trials I think this should work for you -
try(system(command = "perl -pe '/^>/ ? print \"\n\" : chomp' in.fasta | tail -n +2 > out.fasta", intern = TRUE))
Note - intern = TRUE just captures the output as a character vector in R.
I need to build up long command lines in R and pass them to system(). I find it is very inconvenient to use paste0/paste function, or even sprintf function to build each command line. Is there a simpler way to do like this:
Instead of this hard-to-read-and-too-many-quotes:
cmd <- paste("command", "-a", line$elem1, "-b", line$elem3, "-f", df$Colum5[4])
or:
cmd <- sprintf("command -a %s -b %s -f %s", line$elem1, line$elem3, df$Colum5[4])
Can I have this:
cmd <- buildcommand("command -a %line$elem1 -b %line$elem3 -f %df$Colum5[4]")
For a tidyverse solution see https://github.com/tidyverse/glue. Example
name="Foo Bar"
glue::glue("How do you do, {name}?")
With version 1.1.0 (CRAN release on 2016-08-19), the stringr package has gained a string interpolation function str_interp() which is an alternative to the gsubfn package.
# sample data
line <- list(elem1 = 10, elem3 = 30)
df <- data.frame(Colum5 = 1:4)
# do the string interpolation
stringr::str_interp("command -a ${line$elem1} -b ${line$elem3} -f ${df$Colum5[4]}")
#[1] "command -a 10 -b 30 -f 4"
This comes pretty close to what you are asking for. When any function f is prefaced with fn$, i.e. fn$f, character interpolation will be performed replacing ... with the result of running ... as an R expression.
library(gsubfn)
cmd <- fn$identity("command -a `line$elem1` -b `line$elem3` -f `df$Colum5[4]`")
Here is a self contained reproducible example:
library(gsubfn)
# test inputs
line <- list(elem1 = 10, elem3 = 30)
df <- data.frame(Colum5 = 1:4)
fn$identity("command -a `line$elem1` -b `line$elem3` -f `df$Colum5[4]`")
## [1] "command -a 10 -b 30 -f 4"
system
Since any function can be used we could operate directly on the system call like this. We have used echo here to make it executable but any command could be used.
exitcode <- fn$system("echo -a `line$elem1` -b `line$elem3` -f `df$Colum5[4]`")
## -a 10 -b 30 -f 4
Variation
This variation would also work. fn$f also performs substitution of $whatever with the value of variable whatever. See ?fn for details.
with(line, fn$identity("command -a $elem1 -b $elem3 -f `df$Colum5[4]`"))
## [1] "command -a 10 -b 30 -f 4"
Another option would be to use whisker.render from https://github.com/edwindj/whisker which is a {{Mustache}} implementation in R. Usage example:
require(dplyr); require(whisker)
bedFile="test.bed"
whisker.render("processing {{bedFile}}") %>% print
Not really a string interpolation solution, but still a very good option for the problem is to use the processx package instead of system() and then you don't need to quote anything.
library(GetoptLong)
str = qq("region = (#{region[1]}, #{region[2]}), value = #{value}, name = '#{name}'")
cat(str)
qqcat("region = (#{region[1]}, #{region[2]}), value = #{value}, name = '#{name}'")
https://cran.r-project.org/web/packages/GetoptLong/vignettes/variable_interpolation.html
I want to combine awk and R language. The thing is that I have a set of *.txt files in a specified directory and that I don't know the length of the header from the files. In some cases I have to skip 25 lines while in others I have to skip 27 and etc. So I want to type some awk commands to get the number of lines to skip. Once I have this value, I can begin processing the data with R.
Furthermore, in the R file I combine R an bash so my code looks like this :
!/usr/bin/env Rscript
...
argv <- commandArgs(T)
**error checking...**
import_file <- argv[1]
export_file <- argv[2]
**# your function call**
format_windpro(import_file, export_file)
Where and how can i type my awk command. Thanks!
I tried to do what you told me about awk commands and I still get an error. The program doesn't recognize my command and so I can not enter the number of lines to skip to my function. Here is my code:
**nline <- paste('$(grep -n 'm/s' import_file |awk -F":" '{print $1}')')
nline <- scan(pipe(nline),quiet=T)**
I look for the pattern m/s in the first column in order to know where I have my header text. I use R under w7.
Besides Vincent's hint of using system("awk ...", intern=TRUE), you can also use the pipe() function that is part of the usual text connections:
R> sizes <- read.table(pipe("ls -l /tmp | awk '!/^total/ {print $5}'"))
R> summary(sizes)
V1
Min. : 0
1st Qu.: 482
Median : 4096
Mean : 98746
3rd Qu.: 13952
Max. :27662342
R>
Here I am piping a command into awk and then read all the output from awk, that could also be a single line:
R> cmd <- "ls -l /tmp | awk '!/^total/ {sum = sum + $5} END {print sum}'"
R> totalsize <- scan(pipe(cmd), quiet=TRUE)
R> totalsize
[1] 116027050
R>
You can use system to run an external program from R.
system("gawk --version", intern=TRUE)