sink() 不会在 rmarkdown 中将输出打印到文本文件 [英] sink() won't print output to text file in rmarkdown

查看:50
本文介绍了sink() 不会在 rmarkdown 中将输出打印到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 test.Rmd 的简单 rmarkdown 文档

Say I have a simple rmarkdown document called test.Rmd

---
output: pdf_document
---

This code tries to save output to a file called 'example.txt'
```{r}
sink(file='example.txt')
sink.number()
library(MASS)
summary(cars)
sink()
sink.number()
```

如果我在 RStudio 中运行它(使用 knit PDF 按钮),那么我会得到很多输出,但我相信最重要的是以下内容(我可以根据要求包含其他输出)

If I run this in RStudio (using the knit PDF button) then I get lots of output but I believe the most important is the following (I can include the other output on request)

processing file: test.Rmd

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\wammonj\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" 
output file: test.knit.md


Output created: test.pdf
Warning message:
In sink() : no sink to remove

文件 example.txt 已制作,但输出不在其中,而 Rmarkdown 制作了一个名为 test.pdf 的文件,其中包含 summary(cars) 的输出.

The file example.txt is made but the output is not in there while Rmarkdown made a file called test.pdf with the output of summary(cars) in it.

这是问题吗?Rmarkdown 是否使用 sink() 来制作它的文档?有没有办法解决这个问题,这样输出会同时出现在 pdf 文件和文本文件中?

Is that the problem? Does Rmarkdown use sink() to make its documents? Is there a way around this so the output will appear in both the pdf file and the text file?

补充:从@r2evans 的评论看来,rmarkdown 确实使用了 sink().我玩过 sink() 一点,似乎您可以同时进行多个转移,但您只能写入最近激活的转移(参见下面的示例).

Addition: It looks like from @r2evans comment that rmarkdown does indeed use sink(). I have played with sink() a little and it seems like you can have multiple diversions going at the same time but you can only write to the one that was most recently activated (see example below).

因此,从输出看来,Rmarkdown 立即关闭了我的接收器,因为当我查看 sink.number() 时,它始终为 1.

So it seems from the output that Rmarkdown closes my sink down right away because when I look at sink.number() then it is always one.

我仍在尝试为此找到解决方法,因此任何帮助都会很好.

I am still trying to find a workaround for this so any help would be nice.

多次改道的例子:

sink(file = 'example1.txt')
sink(file = 'example2.txt')
sink.number() # prints 2 to example2.txt
x = seq(1,10)
x # prints to example2.txt
sink()
sink.number() # prints 1 to example1.txt
y = sum(x)
y # prints to example1.txt
sink()
sink.number() # prints 0 to R console

推荐答案

我遇到了类似的问题.将输出保存到本地文件的一种解决方案是改用 write.csv() 函数,该函数也适用于非 csv 文件.

I run into a similar issue. One solution of saving the output to local files is to use write.csv() function instead, which also works with non-csv files.

下面的 R 代码尝试将输出保存到名为example.txt"的文件中.

The R code below tries to save output to a file called 'example.txt'.

write.csv(data.frame(data_to_save), file='example.txt')

这篇关于sink() 不会在 rmarkdown 中将输出打印到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆