I'm wondering if there is a way to print a message containing one of the variables into Rmarkdown console at the end of knitting?
I'm currently preparing an R Markdown template for others to use. I would like to inform the users about the readability index of the document once it finishes compiling. I have already found a way to calculate it in one of the chunks. Now I would like to automatically print it to the person who compiles it in a way that it is not shown in the final document. Any ideas?
Ben Bolkers comment is the perfect answer. I ended up putting:
{r cache = FALSE, echo = F, warning = F, message = F}
message(rdblty)
at the very bottom of the markdown file. rdblty is the variable calculated before that I wanted to print out.
How about ?
cat(file="readability.txt")
Related
I have an uncleaned dataset. So, I have imported it to my R studio.Then when I run nrow(adult) in the rmarkdown file and press ctrl+Enter it works, but when i press the knit the following error appears:'
When you knit something it gets executed in a new environment.
The object adult is in your environment at the moment, but not in the new one knit creates.
You probably did not include the code to read or load adult in the knit.
If you clear your workspace, as per #sebastian-c comment, you will see that even ctrl+enter does not work.
You have to create the adult object inside your knit. For example, if your data in from a csv add
adult <- read.csv2('Path/to/file')
in the first chunk.
Hope this is clear enough.
Another option, in the same way as the previous, but really useful in case you have a lot of diferent data
Once you have all your data generated from your R scripts, write in your "normal code" ( any of your R scripts):
save.image (file = "my_work_space.RData")
And then, in your R-Markdown script, load the image of the data saved previously and the libraries you need.
```{r , include=FALSE}
load("my_work_space.RData")
library (tidyverse)
library (skimr)
library(incidence)
```
NOTE: Make sure to save your data after any modification and before running knitr.
Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:
In the global environment, I save all the objects on a file using
save()
In the knitr code I load the objects from the file using load()
Is no so elegant but is the only one that I've found.
I've also tried to access to the global environment variables using the statement get() but without success
If you have added eval = FALSE the earlier R code won't execute in which you have created your object.
So when you again use that object in a different chunk it will fail with object not found message.
When knitting to PDF
```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```
Worked fine.
But not when knitting to Word.
I am rendering in word. Here's what finally got my data loaded from the default document directory. I put this in the first line of my first chunk.
load("~/filename.RData")
I quite often make mistakes in the text part of *.Rmd documents and, in some cases,
fixing and running knit() implies re-running R commands that can take a while.
Therefore, it is an advantage running the .Rmd document without actually executing the R code first, then fix any typos and structure errors and then run again with evaluation.
I know this can be achieved by including
knitr::opts_chunk$set(eval = FALSE)
in the .Rmd
but I would prefer avoiding to actually edit the .Rmd file and using an equivalent way at the command line, something like
knit("file.Rmd", eval=FALSE)
Is actually there a way to achieve this at the command line?
You do not need to edit the .Rmd file. You can just run
knitr::opts_chunk$set(eval = FALSE)
before running knitr::knit(); knitr will respect the global chunk options you set before calling knitr::knit().
I think you might consider to add cache=TRUE to the options in knitr, that will avoid running again the code chunk again if you didn't change anything within it.
https://bookdown.org/yihui/rmarkdown-cookbook/cache.html
You can get a general effect if you add this at the beginning of the .Rmd file, like for example:
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
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!
R newbie question: I am generating PDFs using rmarkdown from the console (not using rstudio). I have written a simple r script to render the rmarkdown file from the console, it basically looks like this:
# my_r_script.R
rmarkdown::render('mydoc.Rmd', output_file = opt$out,
params = list(
something = opt$something,
else = opt$else
)
)
In the rmd file bad errors might happen, e.g. some calculations might crash due to the given parameters. Question How can I access those errors in the R script? Or in other words: I want to know if something went wrong in the RMD file how could I achieve that?
You can wrap the rmarkdown::render statement in a try catch function - there is a great example here.
You should be able to store the output in a variable for further debugging.
I'm trying to create a document with R-markdown but the document doesn't seem to recognise the variables in my current workspace.
Both the markdown document and the workspace are in the same working directory.
How can I set it to use them and update the document?
When you compile an R-markdown document the code is run inside a "clean" R Session. That means it will not have access to objects in the workspace. The R-markdown document chunks will only have access to objects created in another chunk of the document, or the same chunk.
One way around this would be to write out the workspace to a binary file
save.image("myWorkSpace.RData")
before knitting, and then in the first chunk of your R-markdown document do
load("myWorkSpace.RData")
but I don't recommend it. Much better to include the code that creates the objects in your R-Markdown document. That means the document is entirely selfcontained, increasing reproducibility.
I resolved the issue using this line on the top of the first chuck of the document.
knitr::opts_chunk$set(error = TRUE)
The side-effect is that the document has all log information. I am still looking for a better way to solve it!
Greetings!
I ran into this with knitr::opts_chunk$set(cache = TRUE) and tinkering too much with changing objects in the .Rmd.
Deleting the cache folders and knitting the document again seemed to work.
This error can occur if you are including multiple <> within the same code block in your .Rmd file.