Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

r - Rmarkdown not outputting results of system command to html file

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

cat(system(paste("head -n5", datafile), intern = TRUE), sep = '
')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...