6

I'm new to using Markdown, and have looked for a similar problem to this on SO without success. I'm using Rmarkdown (with Rstudio and knitr) to write a vignette that describes reading in a datafile which is imported as part of the package. I can correctly access the datafile using

> system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")

I want to show the first few lines of this file in the vignette, so my code reads

```{r, results=as.is}

datafile <- system.file("extdata", "Marlin-tag38606.txt", package = "xtractomatic")

system(paste("head -n5 ",datafile))

```

The problem is that the results of this call are output to the Rmarkdown console and NOT to the vignette html file.

The output in the Rmarkdown window of RStudio is (but formatted nicer):

 |...................                                              |  29%
label: unnamed-chunk-8
date    lon lat lowLon  higLon  lowLat  higLat
4/23/2003   203.899 19.664  203.899 203.899 19.664  19.664
4/24/2003   204.151 19.821  203.912597  204.389403  18.78051934 20.86148066
4/30/2003   203.919 20.351  203.6793669 204.1586331 18.79728188 21.90471812
5/1/2003    204.229 20.305  203.9943343 204.4636657 18.90440013 21.70559987
  |....................                                             |  31%

Which is what I wanted outputted to the vignette text, but it is not there. Within the resulting vignette all I have is the two lines of R code, but not the output from the system call. Any advice would be appreciated. Thanks.

Cara Wilson

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419

2 Answers2

5

Use intern = TRUE for system(), then cat() the output:

cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • This is kind of ugly. Is there no better way? I'm trying to show a simple use of `system` in an intro-level presentation and having to do it like this makes it seem much more dense than need be. My workaround for now is to print out the regular `system` code in a chunk with `eval = FALSE` then actually run the code in a chunk with `echo = FALSE`. – MichaelChirico Apr 28 '16 at 15:54
  • It is ugly, but there is not a better way that I'm aware of. The problem is the output of `system()` is printed differently and cannot be easily captured via `capture.output()`. – Yihui Xie Apr 28 '16 at 17:26
  • If you don't mind a hackish solution, see https://github.com/yihui/knitr/issues/1203 – Yihui Xie Oct 07 '16 at 06:06
1

Using a bash chunk in the Rmarkdown document does the job for me. For example with the package testdat which has .csv file in its extdata directory:

```{bash}
head -n5 ~/R/x86_64-pc-linux-gnu-library/3.3/testdat/extdata/2012.csv
```

will give in your html file:

## 14,,2012,Censo,1775351,,
## 14,,2012,Votantes,1135568,64.0,
## 14,,2012,Nulos,9168,0.8,
## 14,,2012,Válidos,1126400,99.2,
## 14,,2012,Blancos,14640,1.3,

I am not sure though this option existed in 2014 when you've asked the question.

Vincent
  • 353
  • 2
  • 7