Reputation: 71
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
Upvotes: 6
Views: 2628
Reputation: 353
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.
Upvotes: 1
Reputation: 30104
Use intern = TRUE
for system()
, then cat()
the output:
cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
Upvotes: 5