Using shell commands in knitr and displaying output

117 views Asked by At

I am trying to implement shell commands in knitr and display the output in the knitted pdf document as shown here:

```{r shell commands, engine="sh"}
wc -wlmc en_US.blogs.txt 
```

I am not sure whether this is even being evaluated, as there is no output.

2

There are 2 answers

0
Nicole Goebel On

just realized that I can call this with system(), which will print to device! Therefore,

system("wc -l en_US.blogs.txt")

will print to the display.

0
Griffith Feeney On

Use intern=TRUE to return result of system() as character vector, then cat with past and collapse. Example.

x <- system("tree", intern=TRUE)

cat(paste(x, collapse="\n"))

places tree of working directory in output document using knitr.