0

I'm trying to make life a little bit easier for myself but it is not working yet. What I'm trying to do is the following:

NOTE: I'm running R in the unix server, since the rest of my script is in R. That's why there is system(" ")

system("TRAIT=some_trait")

system("grep var.resid.anim rep_model_$TRAIT.out > res_var_anim_$TRAIT'.xout'",wait=T)

When I run the exact same thing in putty (without system(" ") of course), then the right file is read and right output is created. The script also works when I just remove the variable that I created. However, I need to do this many times, so a variable is very convenient for me, but I can't get it to work.

0

2 Answers 2

2

This code prints nothing on the console.

system("xxx=foo")
system("echo $xxx")

But the following does.

system("xxx=foo; echo $xxx")

The system forgets your variable definition as soon as you finish one call for "system".

In your case, how about trying:

system("TRAIT=some_trait; grep var.resid.anim rep_model_$TRAIT.out > res_var_anim_$TRAIT'.xout'",wait=T)
Sign up to request clarification or add additional context in comments.

Comments

0

You can keep this all in R:

grep_trait <- function(search_for, in_trait, out_trait=in_trait) {
  l <- readLines(sprintf("rep_model_%s.out", in_trait))
  l <- grep(search_for, l, value=TRUE) %>% 
  writeLines(l, sprintf("res_var_anim_%s.xout", out_trait))
}

grep_trait("var.resid.anim", "haptoglobin")

If there's a concern that the files are read into memory first (i.e. if they are huge files), then:

grep_trait <- function(search_for, in_trait, out_trait=in_trait) {
  fin <- file(sprintf("rep_model_%s.out", in_trait), "r")
  fout <- file(sprintf("res_var_anim_%s.xout", out_trait), "w")
  repeat {
    l <- readLines(fin, 1)
    if (length(l) == 0) break;
    if (grepl(search_for, l)[1]) writeLines(l, fout)
  }
  close(fin)
  close(fout)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.