1

I am working in R that invokes linux commands.
This is my code:

system2("wc", args = c("-l",(paste0(" ", file1),stdout = TRUE))  

I need to save the word count value to a variable and call it in R. How can I do that?

1
  • WC = system2("wc", args = c("-l",(paste0(" ", file1),stdout = TRUE)) ? Commented Feb 16, 2017 at 20:49

1 Answer 1

1

Your approach seems to be correct, but check your parentheses:

file1 <- tempfile()
cat("There are seven words in this sentence.", file = file1)
count <- system2("wc", args = c("-w", file1), stdout = TRUE)
as.numeric(sub(file1, "", count))
# [1] 7
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this and it didn't work. It gave a 0 while the actual value is 2993. Could you suggest something?
Please include a minimal working example in your question that reproduces your problem.
when I run the Rscript, system2("wc", args = c("-l",(paste0(" ", file1),stdout = TRUE)) printed 2993 without a print command. But I need to call this value later in my code. So, when I tried to save this value to a variable, expected<- system2("wc", args = c("-w", file1), stdout = TRUE) and print it, it printed it as zero.
Change the "-w" to "-l"?
I need the line count of the file so can I still do that?

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.