Knitr hook to postprocess pdf output? - r

I have foo.Rnw file which uses the extrafont package for generating figures with text elements that use non-standard font families. After knitr calls pdflatex on my document, I want to run extrafont::embed_fonts on the resulting foo.pdf file.
I can do this manually, but is there a way to do this from within knitr? (e.g., some knitr package option I can set to automatically call extrafont::embed_fonts after it knits my file and runs it through pdflatex)

As explained in this answer, it is possible to modify the behavior of the "Compile PDF" button in RStudio by setting the YAML option knit. This allows to run arbitrary code when hitting the "Knit" button. Note that the code must be formatted as a one-liner and you need to use single quotes for character data (double quotes won't work).
I don't know the extrafont package, so here an example that crops the generated PDF. Calling extrafont::embed_fonts should work analogously:
---
knit: (function(inputFile, encoding) { rmarkdown::render(input = inputFile, encoding = encoding); knitr::plot_crop(paste0(basename(tools::file_path_sans_ext(inputFile)), '.pdf')) } )
output: pdf_document
---
```{r}
print("Hello world!")
```
It's actually pretty simple; the most complicated part is composing the output file name: (paste0(basename(tools::file_path_sans_ext(inputFile)), '.pdf') (see here).

Related

Code / Process for running rmarkdown in base R

All my codes developed in base R and I don't want to use RStudio, however I want to use rmarkdown feature in base R which is available in Rstudio.
I have downloaded rmarkdown package in base r, but not able to derive a code to publish my work
All the output of my codes written in R should be view able through web browser.
First make sure you're using .Rmd as your file extension. If not, rename it to a .Rmd extension. Make sure you have Pandoc installed on your OS.
Next, add the following to the top of the file:
---
title: "Your notebook title"
output: html_document
---
output: could take any value. You can pass in the value of ioslides_presentation for example if you want but it looks like html_document fits the criteria of what you want pretty well.
Once you have that, write your code in any editor (or the R console if you prefer). Use the code chunks and markdown text formatting as you normally would:
```{r}
plot(1:10)
```
In my base R Console, this is how mynotebook.Rmd looks like:
Finally, use the render() function from rmarkdown. You can either attach it and run render():
library(rmarkdown)
render("mynotebook.Rmd")
Or, run rmarkdown::render("mynotebook.Rmd").
Notice that the use of RStudio is not required at all since Pandoc is the document converter performing this task. For the so inclined, this is what its documentation has to say:
When you run render, R Markdown feeds the .Rmd file to knitr,
which executes all of the code chunks and creates a new markdown (.md)
document which includes the code and it's output.
The markdown file generated by knitr is then processed by pandoc
which is responsible for creating the finished format.
This may sound complicated, but R Markdown makes it extremely simple
by encapsulating all of the above processing into a single render
function.

run an R Markdown (check.Rmd ) and an R knitr (test.Rnw ) file together

I have the following problem; There are 2 big documents, one written in R Markdown (check.Rmd ) and the other in R knitr (test.Rnw ). In the first doc we have a code like the following:
\section{Organisations Test}
\textbf{Running Organisations Checks}
<<CreateOrganisations, echo=FALSE, progress=TRUE, warning=FALSE, eval=TRUE>>=
source("OrganisationsTest.R")
OrganisationsTest(current_schema,server,user,pass)
#
and in the other as follows:
2. check the downwards shock
```{r chunk_Int_Sh_p2, echo=FALSE}
unique(param.int.shock.tab[SHOCKTYPE=="SHOCK_DOWN"&PERIODEND<21|PERIODEND==90, list( Maturity=PERIODEND, Shock_value=100*SHOCKVALUE)])
```
Now the question: how can I combine both so that I have just one script which runs and compile both one after each other. Just for clarification, I mean without any changes in both documents how can I have just one script which applyes to the first doc knit PDF to create pdf and to the other one CompilePDF ?
I suppose in Linux one can write a shell script but what a bout using RStudio in windowes?
I am really grateful for every hint I am a little bit helpless!
Addendum: In principle it is as follows: we have 2 files if you would compile a knitr file you would use bottom in RStudio, and for a Markdown file one may use bottom in RStudio, BUT we want to put both together and klick on one bottom. How is it possible?
The RStudio buttons "Compile PDF" (for RNW documents) and "Knit PDF" (for RMD documents) are convenient, but in cases like this one it is important to understand what they do in order to reproduce the same or similar behavior.
Summing the question up, it asks for a way to convert two files (a RMD and a RNW document) to PDF, preferably using a button like the two buttons mentioned above.
Unfortunately, (up to my knowledge) it is not possible to add any user-defined buttons to the RStudio GUI. But it is straightforward to write an R script that compiles both documents.
In the following I assume two files:
first.Rmd:
This is a RMD file.
```{r, echo=FALSE}
plot(1)
```
second.Rnw:
\documentclass{article}
\begin{document}
This is a RNW file.
<<>>=
plot(1)
#
\end{document}
To compile first.Rmd to PDF, we need the following (see How to convert R Markdown to PDF?):
library(knitr)
library(rmarkdown)
knit(input = "first.Rmd")
render(input = "first.md", output_format = "pdf_document")
The knit call generates first.md from first.Rmd, execting the R code in the chunks. render converts the resulting markdown file to PDF. [Note the addendum at the bottom!]
To compile first.Rnw to PDF, we can simply use knit2pdf:
knit2pdf("second.Rnw")
Copying both snippets into one R script and clicking "Source" is as close as possible to a "one-button-solution".
However, note that the snippets do something very similar to the "Compile / knit PDF" button, but it is not identical. The "Compile" buttons start a new R session while the solution above uses the current session.
Before executing the snippets make sure to use the correct working directory.
Both knit and knit2pdf by default use envir = parent.frame(). That means R code in chunks is executed in the calling enironment (see What is the difference between parent.frame() and parent.env() in R). This can be a useful feature, for example to "pass" variables to chunks, but it is important to know about it. Otherwise a document might compile just fine in one session (where certain variables exist in the calling environment) but cannot be compiled in another session (that is missing these variables). Therefore, this feature is a little bit dangerous in terms of reproducibility. As a solution, envir = new.env(parent = as.environment(2)) could be used; see knitr inherits variables from a user's environment, even with envir = new.env() for more details on that topic.
I just realized to following about render:
If the input requires knitting then knit is called prior to pandoc.
(Source: ?render)
Therefore, knit(input = "first.Rmd"); render(input = "first.md", output_format = "pdf_document") can be simplified to render(input = "first.Rmd", output_format = "pdf_document"). The envir issues of knit from above apply to render as well.

function to get filename without full path in YAML header of RMarkdown document

I am trying to modify the behavior of RStudio's knit button, by changing the directory to which it writes the output of knitting the Rmd file. I have started with this answer, but instead of having the filename given by a fixed string, I'd like to have the output filename based on the Rmd filename. However, the variable inputFile includes the full path to the Rmd file. Is there a way to get only the filename without the path?
The header I am working with that produces the full path+filename where I'd like just the filename (test2 is a directory that I created in the current working directory):
---
knit: (function(inputFile, encoding) {rmarkdown::render(inputFile,encoding=encoding, output_file=file.path(dirname(inputFile), "test2", paste0(substr(inputFile,1,nchar(inputFile-4),".html"))) })
output: html_document
---
I'm still interested in a command that would directly give me the input filename, as specified in the question, but I found a workaround for the particular issue, using regex in the substr call, based on this:
knit: (function(inputFile, encoding) {rmarkdown::render(inputFile,encoding=encoding, output_file=file.path(dirname(inputFile), "test2", paste0(substr(inputFile,rev(gregexpr("/", inputFile)[[1]])[1]+1,nchar(inputFile)-4),".html"))) })
Nowadays, knitr comes with a function current_input() that gives you the name of the Rmd-file as a string. And tools::file_path_sans_ext() will remove the extension.
But to solve the exact problem of the OP, there are probably better options today, for example, knitr options, the package ezknitr, RStudio "Knit Directory" button, or here::here().

KnitR XeTeX Beamer issue

I am trying to use a Beamer theme that requires \usepackage[no-math]{fontspec} with a KnitR document. Here is a minimal example test.Rnw:
\documentclass[handout]{beamer}
\usepackage[no-math]{fontspec}
\begin{document}
\maketitle
<<include=FALSE>>=
options(tikzDefaultEngine='xetex')
#
\begin{frame}{test}
\begin{figure}[htbp]
<<echo=FALSE, dev="tikz">>=
plot(1:100)
#
\end{figure}
\end{frame}
\end{document}
When I Knit the doucument I get:
Error in getMetricsFromLatex(TeXMetrics) :
TeX was unable to calculate metrics for the following string
or character:
m
Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or $.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.
This is an issue specifically related to the no-math option with fontspec along with dev="tikz". If I remove either of these, the document compiles fine. Any suggestions on how to get this to work? The entire purpose of using a tikz device is to allow the format of my graphics to match up with my theme that requires I am trying to use a Beamer theme that requires \usepackage[no-math]{fontspec}. Thanks!

R Markdown - variable output name

With one R markdown file, I would like to create different possible output pdf documents, where the output file name should be defined within the document. Is there any way to convince markdown to manipulate the output filename in such a way? Ideally I would like to pass the filename by an r chunk.
You can keep the simplicity of using the RStudio Knit button and reproducibility of a YAML header by using the undocumented knit hook to redefine what the button does (default function called is rmarkdown::render). The output_file parameter of the render function specifies the file name, so by setting it you override the standard behaviour of using the same prefix as the input filename.
e.g. to always output a file called myfile.pdf
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), 'myfile.pdf')) })
The function can be an anonymous one-liner as well as imported from a package, as seen here with slidify.
You can set your own YAML headers (I don't know if this is generally advised anyway), accessible under rmarkdown::metadata$newheader but they don't seem available from within this sort of function as far as I can see.
As for passing file name in from an R chunk... if you're referring to code chunks below the YAML header, from my experience I don't think that's possible(?). Headers can contain inline R commands (single backtick-enclosed, starting with r), but seemingly not for this hook function.
Related:
Rmarkdown GitHub repo issue — output format-specific output_file
Blog post I wrote following this question [invalid link, domain for sale as of 20210216] / corresponding GitHub wiki notes
This is pretty much what I do:
rmarkdown::render('my_markdown_report.Rmd',
output_file = paste('report.', Sys.Date(),
'.pdf', sep=''))
I have three scripts - one pulls the data and process it, second created charts & tables for report. Third one creates report based on markdown file. Code you see above is the part of the third script
I played around with the Knitr-hook without fully understanding how it works and ran into an ugly workaround. The below coding seems to do the trick.
Would be nice if somebody can either explain why it works and/or if it can written less ugly.
For now I lost the shiny input screen but believe this can even be added later. The good thing is that the R-Studio Knit button can still be used.
Please note that the subtitle and the file name are both: This Works! even with space and exclamation mark. The file is saved as This Works!.pdf
The filename and subtitle are set by assigning the text to the object pSubTitle.
Note that the params are still in the YAML but are not resulting in a shiny popup screen as they are assigned in the Knitr-hook
---
params:
sub_title:
input: text
label: Sub Title
value: 'my_Sub_Title_and_File_Name'
title : "Parameterized_Title_and_output_file"
subtitle : "`r params$sub_title`"
output:
pdf_document:
keep_tex: false
knit: (
function(inputFile, encoding) {
pSubTitle <- 'This Works!'
rmarkdown::render(
input = inputFile,
encoding = encoding,
params = list(sub_title = pSubTitle),
output_file = pSubTitle) })
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. ....
My current solution for this and similar questions is through 2 scripts:
Script1: "xxx.md" file with flexible yaml header, similar to Floris Padt's. This header allows you to generate flexible pdf files with specified title, dates, and other features if you change the params. However, it could not specify flexible pdf names when you render it.
---
params:
feature_input: "XXXA"
date: "08/18/2022"
title: "`Test For `r params$feature_input``"
author: "You Name"
date: "`r params$date`"
output:
pdf_document:
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown and data process
```
#Get parameter from yaml head
input_para <- params$feature_input
input_para
```
Script2: "YYY.R", which specify params in xxx.md file, and specify pdf file names by output_file when rendering.
featureNames <- c("aaa", "bbb", "ccc")
setwd("path to xxx.md")
for (currentFeature in featureNames) {
rmarkdown::render("xxx.Rmd",
params = list(feature_input = currentFeature,
date = Sys.Date()),
output_file=paste0("output/",currentFeature))
}
You could update featureNames in yyy.R file, and run yyy.R to get the most flexible use of your xxx.md file.
This solution allows you to:
update yaml header parameters,
apply updated yaml parameters in your .md code chunk,
and save your pdf with specific and flexible names.

Resources