6

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()
```

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

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.

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?


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).

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.

Example of multiple diversions:

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
MathIsKey
  • 195
  • 8
  • 3
    `knitr` (used by `rmarkdown`) [uses `capture.output`](https://github.com/yihui/knitr/search?utf8=%E2%9C%93&q=capture.output) which [uses `sink`](https://github.com/wch/r-source/blob/e5b21d0397c607883ff25cca379687b86933d730/src/library/utils/R/capture.output.R#L35). [`sink` deals with "diversions"](https://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html), and while I admittedly don't fully understand everything about it, I believe they are exclusive, meaning output goes to one or the other (unless you create a function analogous to the shell command `tee`). – r2evans Aug 24 '16 at 19:14

3 Answers3

1

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.

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

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

Jm M
  • 558
  • 4
  • 10
  • Thanks for this suggestion. I think I eventually just ran the code twice: once in rmarkdown and once just in R. But this is a much better solution. – MathIsKey Feb 04 '18 at 01:50
0

This issue is due to (I believe) an error in the evaluate package's use of sink(), combined with rmarkdown::render's line-by-line evaluation.

rmarkdown::render() will submit each line to evaluate::evaluate at a time, but evaluate will fail if a new sink() is created but not closed within that line. A workaround is to define these steps as a function in which both the initial sink(...) and the final sink() are submitted in one line. Of course all of the code you are attempting to capture the results of will need to be in the same function as well.

Alternately, wait to see if the rmarkdown or evaluate developers fix the issue:

Patrick
  • 653
  • 1
  • 6
  • 17
0

I wanted to do a parametric rendering of rmarkdown. The parameter would be a random number. I needed to save/see the output (a vector of number and string) to a file (save_out.txt).

str00 = c('test','some','here',2323)
cat(str00,file="save_out.txt",sep=",",append=TRUE)
cat('\n',file="save_out.txt",append=TRUE)

Result:

> test,some,here,2323

sink or writeLines do not work for me when I want to append data while rendering the rmarkdown.

I got some help from here: https://stackoverflow.com/a/55225065/1247080

Stat-R
  • 5,040
  • 8
  • 42
  • 68