Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 510
Cara Wilson Avatar asked Dec 09 '14 21:12

Cara Wilson


People also ask

How do I export R markdown to HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I display code output in R markdown?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

Why can't I knit to HTML in R?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

How not show output of code in R markdown?

You use results="hide" to hide the results/output (but here the code would still be displayed). You use include=FALSE to have the chunk evaluated, but neither the code nor its output displayed.


1 Answers

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

cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
like image 128
Yihui Xie Avatar answered Oct 05 '22 22:10

Yihui Xie