Rmarkdown不将系统命令的结果输出到html文件 [英] Rmarkdown not outputting results of system command to html file
问题描述
我是Markdown的新手,并且在SO上寻找与此类似的问题,但没有成功.我正在使用Rmarkdown(与Rstudio和knitr一起)编写一个小插图,该小插图描述了作为包的一部分导入的数据文件中的读取.我可以使用
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))
```
问题是此调用的结果输出到Rmarkdown控制台,而不输出到小插图html文件.
The problem is that the results of this call are output to the Rmarkdown console and NOT to the vignette html file.
RStudio的Rmarkdown窗口中的输出为(但格式更好):
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%
这是我想要输出到小插图文本的内容,但是它不存在.在生成的小插图中,我所拥有的只是R代码的两行,而不是系统调用的输出. 任何意见,将不胜感激.谢谢.
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.
卡拉·威尔逊
推荐答案
将intern = TRUE
用于system()
,然后将cat()
用作输出:
Use intern = TRUE
for system()
, then cat()
the output:
cat(system(paste("head -n5", datafile), intern = TRUE), sep = '\n')
这篇关于Rmarkdown不将系统命令的结果输出到html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!