Post-processing R Markdown-generated html files - r

Is there a way to automatically apply some post-processing to html files generated from R Markdown?
For example, if we use MathJax to render LaTeX code (primarily in RStudio), the resulting html file includes src= links to load MathJax from a CDN. The problem is, we also need to be able to display these files on internal network without internet access, and those links need to be replaced to point to a local copy of MathJax.
It would be convenient if there was some hook that I could use to do that after the html file is generated, but I haven't been able to find any.

If your post processing is specifically about MathJax, you might consider setting your output yaml to the following which will download local copies of libraries such as MathJax and link to them.
output:
html_document:
self_contained: FALSE
mathjax: local

Related

Transferring/emailing files created by Quarto

When I would create a html document in RMarkdown, the file transfer/email process was straightforward; I would simply transfer/email the resulting html file.
However, when I try to do the same with a Quarto document, I do not get all of the functionality in the transferred file. For example, my Quarto document includes tabs using the ::: {.panel-tabset} option. When I open the transferred html document, the panel-tabsets are not displayed properly.
I believe that this is because Quarto also produces files in a separate directory. However, I trying to programmatically email the html document out to a distribution, and sendmailR is not letting me attach this directory.
Furthermore, my distribution list are NOT technical folks. I am not looking to have to tell them to save these files and then open the html; I was hoping to just attach the html file like I did with RMarkdown and have them natively open it just like an regular attachment.
What is the best way to transfer/email Quarto produced html documents?
Someone directed me to use the html: self-contained in the YAML header.
Based on that, I found this post which states what to do.
By putting the following in my YAML header, the resultant html file is self-contained:
---
format:
html:
self-contained: true
---

TOC not displaying in Blogdown post (Hugo theme Even)

I have added a new post to my site made using blogdown. Despite the presence of toc: yes in my YAML, the table of contents is not displaying on the rendered webpage. I've also tried adding:
output:
blogdown::html_page:
toc: yes
which doesn't solve the problem either. I'm currently using the Even theme (Hugo), and in the .config file, I also have toc: yes present.
I checked out this post, but I wasn't able to find the files they are referencing, nor those particular lines of code. Also my issue is with new posts, not the home page.
Does anyone know what the problem might be?
EDIT: I can see in my index.html file (generated from index.Rmd) that the code for a toc is present. For some reason, it's not being rendered on the page?
TL;DR Solution: to get the table of contents to show in the Even theme, your posts must render to the .markdown format instead of .html format. In order to make this happen, you simple use the .Rmarkdown file extension when creating the post instead of .Rmd. Now, when you use blogdown::serve_site(), your post will use the output format .markdown instead of .html. Your post will now render with a beautiful table of contents.
A note for latex math mode users: when typing in your .Rmarkdown document, you will now have to escape your $'s and \'s. So for example, $\le 0.2$
will have to be typed as \$\\le 0.2\$.
I hope this overly elaborate problem and solution is helpful to someone!
I figured out a workaround for this.
First, I'm not sure how to provide a reproducible example in this context, but I can say that if you open an RStudio project, run blogdown::new_site(theme = "olOwOlo/hugo-theme-even"), add a New Post using the Addin dropdown menu, add some headings to your post, make toc:true, use Serve Site from the Addin menu. You will see your post working fine in every respect - only it will be missing the table of contents.
Workaround:
I don't know why, but it appears that the table of contents will show if I keep a copy of the post in .md format, and delete the .html document. The default behavior seems to use the .html document if it is present; failing that, it will use the .md document.
To keep a copy of the post in .md format, use the following code in the YAML of your .Rmd document:
output:
blogdown::html_page:
keep_md: yes
Serve the site, delete the .html document (but keep the .md document), and the table of contents appears in all of its glory (you may have to actually knit the file to get the .md).
I don't know why this works...I've seen elsewhere (although I can't seem to find the post again) that some hugo themes don't play nicely with .html files, and prefer .md files, and that appears to be the case for the Even theme.
In sum: My workaround for now is thus to work on my posts, and when I'm done, knit the document with the option above, delete the .html file, and be on my way.
Caveat: blogdown seems to automatically re-render all the old posts when using Serve Site. According to this stack answer, Serve Site should only render documents that have undergone changes. Regardless, the old posts are most definitely being re-rendered, even when working on an unrelated post. I'm not sure if this behavior is attributable to blogdown per se, or the RStudio project.
I tried going to Tools -> Project Options -> Build Tools, and unchecking the boxes "Preview site when building", yet the behavior persists. Which means I will have to continuously delete these new .html files for all old posts every time a I make a change to any other file. Currently I only have one post, so not a big deal...yet.
If someone knows of a solution to this caveat, I would be extremely grateful. If I find one, I will edit this question and add it.
EDIT: it appears that blogdown will re-render the old post if there is no .html document present. Otherwise, it does not re-render it.
EDIT 2: Aha! According to the answer to this question, simply changing the file extension of the blog post from .Rmd to .Rmarkdown will produce a .markdown document instead of a .html document.

Use Rmd files for content pages in blogdown website

I am considering switching my website to blogdown. My current set-up only uses .Rmd files with a less-than-ideal blog implementation. It seems like one advantage of blogdown is that it automates the blogging part of the website (posts, lists, dates, etc.) and allows RMarkdown. This is my main motivation to switch. However, the downside, I think, is that I cannot create content pages in RMarkdown (i.e., content > about.Rmd)... it seems this is limited to .md files. I would like to use .Rmd for all the pages so that I can incorporate r code. My question: Is it possible to create .Rmd content pages in blogdown?
EDIT...
Specifically, I created by hand research.Rmd in the content folder. I can include a trivial example, such as...
`r 2 + 2`
and when I run serve_site() the r code is not rendered. I can knit the file by hand, but the file does not have the style of the theme I am using and once I again try to serve the site the file is rendered as if it were a .md file.
```
EDIT 2...
You can reproduce the behavior I am referring to by doing the following...
Create a new blowdown site: new_site(dir = ".", theme = 'gcushen/hugo-academic')
Create new content: new_content('test/index.Rmd')
Set draft to false and add trivial r code: I used 2 + 2 as shown above.
Serve site and go to test/index.html. There is no r code rendered.
From your Edit 2, you seem to have fallen into a common Hugo trap: content/test/index.Rmd does not necessarily generate test/index.html. Hugo uses the convention _index.md to generate a homepage for a subfolder, and you have to use content/test/_index.Rmd in your case. For content/about.Rmd or content/research.Rmd, they will generate about/index.html and research/index.html respectively by default.

In RStudio, making slide deck with RMarkdown, how can I easily change the theme?

I'm writing an .rmd file in RStudio, and using the Knit button to create a slide deck. I have two options, it appears, ioslides and slidy. I don't really like the default look-and-feel of either. (I think ioslides uses screen real estate inefficiently, and slidy feels like a graphic designer wasn't involved.)
I did a web search for alternative templates/styles/themes, and came upon pages like this gallery, but the tools there seem to be for use outside RStudio, as completely independent projects. In other words, they're not just themes for what I've got, but entirely other solutions that would change my workflow.
Is there a simple way to tell RStudio to use a different template/theme with either ioslides or slidy? For example, can I download a CSS file from somewhere, and point RStudio to it with the css: option in the yaml header of my .rmd file? If so, is there a gallery of such CSS templates?
Here's what I ended up doing. It's not as smooth as I'd like it to be, but it's decent. I'm glad to entertain better answers.
Download any of the style files linked to from this knitrBootstrap code into the same folder as your R Markdown file.
Edit it so that the URL at the start of the file begins with http: so that knitr knows not to look on your filesystem for the additional resources (fonts), but on the Web.
Edit it further to remove any pipes (|) in that URL, because knitr can't handle them (as far as I can tell). (Remove everything after the pipe, too.)
Edit it further to change everything of the form url('../fonts/***.eot'); to now instead be of the form url('https://netdna.boostrapcdn.com/bootswatch/3.0.0/fonts/***.eot');.
Link to it from your R Markdown file with the css: attribute in the YAML header.
Have you tried using the built in Bootswatch themes? They're fairly limited, but in doing so you can quickly customise many elements.
e.g
title: "Test"
author: "John"
date: "5 June 2017"
output:
slidy_presentation:
fig_width: 12.5
font_adjustment: -1
theme: readable
Other themes may be found here Bootswatch.
edit: removed a typo

Correct Mathjax source in RStudio and RMarkdown

I create a R Markdown file in RStudio , then I click on the Knit HTML button on the top of the screen to create and save an HTML page which I want to use elsewhere, say store it on my server.
The problem with the HTML file, is that it contains
<script type="text/javascript" src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
which is not valid anymore, and when I edit the HTML and change the source to the following it works.
https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
Any way I don't have to edit every time the HTML, but have the correct source from RStudio?
This was changed in the rmarkdown package just about a month ago (see this commit). You just need to install the latest version of the rmarkdown package:
> devtools::install_github("rstudio/rmarkdown")
(of course, you'll also need to install.packages("devtools") if you don't have devtools.)
Incidentally, even without this step, you can tell rmarkdown where you want to get MathJax from. This is useful if you want to point to a different CDN.
output:
html_document:
mathjax: https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
Finally, you can eliminate your MathJax CDN dependency entirely by using mathjax: local to use a built-in copy of MathJax in rmarkdown (note that you'll also need to set self_contained: no to do this).

Resources