Is it possible to capture output that is printed by a function and instead output it via message? I am running a report using knitr that deals with very large tables (millions of rows) and I have message statements in the code so I can have an idea of where I am. This works fine as I simply set the global chunk option to message = F and I can see the message in the console and it does not show up in the final pdf. However, I use fread to read the data from a file, and the showProgress option does not print a message! Setting the chunk option to results='hide' prevents the progress from being printed to the final document, but it also prevents me from seeing it at all, so that is unhelpful. Any help would be appreciated, thanks!
Related
I have a report made of several Rmd files (several layers of child documents).
I always end up doing a copy paste and forget to change the chunk names, it's annoying because the report takes minutes to knit before the error pops up.
I use the chunk names to debug, and in the end they're often the ones that make my code crash, there has to be a better way.
How can I check programmatically that everything's clear before attempting to knit ?
Apparently there exists this option that allows you to keep duplicate chunk labels and still knit:
options(knitr.duplicate.label = 'allow')
I am using fread to read in data as part of a .Rnw file I am working on. The file is big, 6 million rows, and I like the showProgress option. However, when I knit, the output appears in the final pdf. Elsewhere in the file, I print progress using message(), which I suppress in opts_chunk. Is there any way to print the progress as a message or in some other way that it doesn't appear in my final output?
You could set options(datatable.showProgress = FALSE) in your first code chunk. This will disable data table progress bar printing (by default) for the rest of the document.
I have an rsweave file that I run almost twice a week. Last time I used it a change a couple of things and when I run it to compile to pdf I got the following errors:
The pdf compiles complitly, and the only thing I notice that the error did is that the the pdf output has a extra page (the first one) all blank. I don't know how to make a reproducible example of the errors because I don't know whats the cause of it. But any way I just want to know generally how to debug a rsweave file when getting latex error like the ones in the picture
You don't say how you are running Sweave, but that looks like RStudio. To debug something like this, just run Sweave explicitly in the R console, e.g. if your input file is source.Rnw, run
Sweave('source.Rnw')
This will produce source.tex. Open that file in a text editor and look at the start of it. You will see that \Schunk is used on line 27, but \begin{document} doesn't occur until sometime later.
My guess is that you added some text or a code chunk to the header. All text belongs after \begin{document}.
Edited to add: It turns out from the comments below that you were using print(...) in a code chunk before \begin{document}. In Sweave, print output goes into the document. If you want a message to show up in the console log but not the document, use message("some text"). You'll also need to suppress the echoing of the command if you want to do this in the document header. For example,
<<echo=FALSE,results=hide>>=
message("Started at ", Sys.time())
#
will result in something like this in your console log:
1 : keep.source term hide (Untitled.Rnw:6)
Started at 2017-09-27 08:28:33
and nothing in the document.
Perhaps I am not using R markdown properly, but my first line of code load a very large data set and then does analysis. Every time I knit the pdf to see what it looks like, it runs all the code again, this takes quite a while. The data is already stored in the environment so is there a way of getting R to not run all the code again but display the pdf with the alterations made?
In case loading your very large data set is the problem, try special packages for reading your data like readr.
Alternative, since you working on the design or representation in you PDF, you can work on a subset of your data like only on the first 100000 rows.
Otherwise, I use the following code in my first code chunk
library(knitr)
# global setting to create this document
opts_chunk$set(cache=TRUE,
echo=TRUE, # set to FALSE to remove the code output
warning=FALSE,
message=FALSE)
so I don't need to set cache=TRUE in each chunk.
Hope this helps.
My set of tricks is evolving:
a parameter to use on the chunk's eval=params$do.readdata
this type of construct:
if (exists('<name of data table>') {
load(<file>, verbose=TRUE) or st_read_feather...
} else {
<read in data>
}
Until recently, i was using a cache option on the chunk, but recently learned that putting the import and processing of data in a function in order to remove it from the global environment (if memory is an issue) and return a smaller subset or result that is needed later. So, caching may not be an option.
I want only the output not the commands, and I want to see the output on the console at the same time.
I tried sink and capture.output , but tried to work through the examples but I can accomplish only one of the task i.e. either to console or to a file.
I am new to R, and was thinking whether there is a function that can help me see the output on the console and save it to a text file as well?
Do sink(file="file.txt", split=TRUE).