Extract html dependencies for .Rmd file (containing htmlwidgets) - r

Question
How could I create a function which would take a .Rmd file (containing htmlwidgets code) as its input, and output a html file containing its JavaScript/CSS dependencies?
Specifically, the tempfile rmarkdown generates for pandoc's --include-in-header argument, when rendering to html.
Details
Example -- myfile.Rmd:
This is some text
```{r}
library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures")
```
Running rmarkdown::render('myfile.Rmd') passes a tempfile to the --include-in-header pandoc argument, containing all the JavaScript and CSS needed to render the interactive graphic (either compressed data:uri format, or html links to copied files). This is included in the final html file, so that everything renders in the browser. It's this tempfile I'd like to be able to generate.
It's appears to be output by passing a dependencies object to the internal function rmarkdown:::html_dependencies_as_string. I'd like to be able to generate this file for arbitrary .Rmd documents containing htmlwidgets code.
Context
I'm interested in blogging using knitr and Jekyll, as Yihui outlines here. However, the disadvantage of this approach is that none of the htmlwidgets functionality works, because the dependency injection step is missed. This is because pandoc is called by Ruby/Jekyll, and so does not benefit from htmlwidgets/rmarkdown's invisible magic. I'd like to add a function to my build process which writes the dependencies to a html file, which can be included in the html header by Jeykll if required.

This is a possible solution http://benjcunningham.org/2016/06/13/hacking-together-htmlwidgets-for-jekyll.html, that refers to an older post that also offers a solution: https://brendanrocks.com/htmlwidgets-knitr-jekyll/.
A bit hacky. An official solution to this problems is still pending. There is an open issue in yihui/knitr-jekyll: https://github.com/yihui/knitr-jekyll/issues/8

Related

Quarto equivalent to "exclude:" YAML command in distill

In R, I am moving a distill website to quarto website. In distill, I can prevent all the Rmd documents in a directory called "internal" from being knitted by adding this to the _site.yml
exclude:
- internal
The same trick does not work in quarto. Is there a quarto equivalent, somehow to indicate which directories should be excluded during the knitting/rendering process?
Thanks to C Wickham for answering the question:
Yes, you can control which documents are knitted by specifying the render targets. Two options that might work for you are:
Renaming internal/ to _internal/, and relying on the automatic ignoring of files and directories with a prefix of _.
Explicitly, specifying that files in internal/ should not be rendered in _quarto.yml, e.g. (edited from example in docs linked above):
project:
render:
- "*.qmd"
- "!internal/"
The "*.qmd" is necessary to describe which files should be rendered, so if you've got some .Rmd files as well, you might need to add another line with "*.Rmd" to make sure they are rendered.

How to use libraries across an r notebook?

I wish to use libraries across multiple .Rmd files in an r notebook without having to reload the library each time.
An example: I have loaded the library kableExtra in the index.Rmd file but when I call it in another .Rmd file such as ExSum.Rmd I would get this error:
Error in Kable....: could not find funciton "kable" Calls:...
If I load the kableExtra library again this problem goes away. Is there a workaround?
R Markdown files are intended to be standalone, so you shouldn't do this. There are two workarounds that come close:
If you process your .Rmd files within the R console by running code like rmarkdown::render("file.Rmd") then any packages attached in the session will be available to the code in the .Rmd file.
You can put all the setup code (e.g. library(kableExtra)) into a file (called setup.R, for example), and source it into each document in the first code chunk using source('setup.R'). Every file will run the same setup, but you only need to type it once.
The second is the better approach.

How to pass options to a LaTeX font in R Markdown?

rmarkdown, following {xe|lua}latex, allows to specify fonts for main text, sans-serif text, monspaced text (most notably code chunks !) and math fonts in the YAML header. At least for PDF rendering via xetex, this works.
However, I found no (documented) way to pass options to the underlying setxxxfont \LaTeX command. For example, the YAML fragment :
```
monofont: Inconsolata
```
generates the following \LaTeX fragment :
\setmonofont[Mapping=tex-ansi]{Inconsolata}
I have two questions with this:
why is the Mapping=tex-ansi added ? And how to control it ? (I'm working in UTF8...).
How could I set additional arguments for the font options i.e. \setmonofont[Scale=0.91]{TeX Gyre Cursor}?
The R Markdown book and the Pandoc's User's Guide did not reveal anything pertinent.
Background:
When R Markdown converts the knitted code to the output format (PDF), a pandoc template is used. The template is stored within the package, and any variables which get replaced by the YAML variables are contained in the $$ Notation
1. Encoding
The Mapping=tex-ansi is added to the code as a workaround an issue as reported on GitHub. Therefore I would be cautious of deleting this for potential side effects.
If you do indeed wish to change this code, you will have to make a copy of the LaTeX template file used to convert the document. You can find the default template here. See here for some more information on providing custom templates.
2. Additional Font Options
You can use the monofontoptions YAML argument to add additional arguments to the font options.
Documentation of the variables which can be parsed by the LaTeX output are available in the pandoc documentation

Build .md vignette using devtools

I’m using knitr::rmarkdown (but knitr::knitr does the same) as my VignetteEngine. I then build my package vignette using devtools::build_vignettes().
This works, but only creates HTML and R output files in inst/doc. What I want is a Markdown output file, since only that can be displayed directly within the Github project pages (for HTML files, Github shows the source and for Rmd files it shows a rendered output but — obviously — without executing the R blocks).
I’ve tried finding out how to specify the output for custom VignetteEngines and I think that it should be possible (after all, other packages use this to build PDF vignettes, at a minimum) but I cannot find a way of doing this via devtools::build_vignettes. Is there no way around building the vignette manually (i.e. via knitr::knit or similar mechanisms that ignore the VignetteBuilder directives)?
I can’t find relevant information in the documentation/source either.
The only output formats for vignettes are HTML and PDF (and LaTeX, but it is converted to PDF, not displayed). Markdown isn't supported.
You can have arbitrary documentation files in your package (by convention you put them in inst/doc), but they aren't considered to be vignettes, so they won't be automatically built, functions like browseVignettes() will ignore them, etc.
To convert an Rmd file to md, just run knitr::knit on it.

knitr: Document does not change anymore

I have a .Rnw document in which I include childs. The childs produce tables via the 'latex' command of the Hmisc library in R.
When I make changes in the child documents, these changes do not anymore change the pdf document. My first guess was to use the chunk option 'eval=TRUE', but this does not change anything. Then, I saw, that the tables are actually saved to a .tex file with same name as the .Rnw document. I deleted this file and after compilation with knitr I got an error:
Error: Latexmk: Could not find file documentname.tex.
I assume, this is not the way to do it. Now I am out of ideas what to do. I appreciate some help on my problem.
Best
Simon
Allright, when trying to construct a simple example, I actually found out, that neither the packages I included nor the nesting of child documents interfere with the compilation via knitr. The reason was a simple error in the low-level .Rnw document, where a Hmisc latex table had a label, that missed a closing speech mark.
This causes then the output pdf not to change - I assume, that in this case the already constructed .tex file is included instead of letting knitr recompile the .Rnw documents and this hasn't changed since the last compilation?
What I wonder about is the different format of the landscape ctable in the document. Using a simple knitr document just with \documentclass{article} produces well placed tables. In my document using a template for the JFE, I get a table that extends over the whole page and even in footnotesize it is far away from the great appearance in the simple document. There is only a margin of less than half a cm on the right and the left. Page size is the same: both US letter... Can I probably control that via knitr or only via resizebox?

Resources