Correct Mathjax source in RStudio and RMarkdown - r

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).

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
---

Post-processing R Markdown-generated html files

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

Rmd output format not compiled correctly in blogdown when HTML file already exists

I have a webpage (www.vegardlysne.no) built with blogdown, using the academic theme. In one section of my page, I publish my notes taken during conferences and courses I attend, and I want them to be knitted with a different output format. I store these .Rmd files in the static folder, and specified a "build.R" script to knit .Rmd files in the static folder to HTML before compiling the page.
Specifically, I want to knit these files with theme: darkly and a floating toc, properly specified in the YAML header. This has worked perfectly before, and in principle still works when compiling an Rmd file for the first time.
The problem, however, occur when the Rmd file is updated when they have been previously knitted (corresponding HTML file exists in the static folder). When I update the Rmd file, the HTML file lose the formatting and is compiled as text on white background, with the YAML header as a single line at the top like this:
--- title: "My title" output: html_document: toc: true toc_depth: 2 toc_float: true theme: darkly ---
I can escape the problem by deleting the HTML file in the static folder before running blogdown::build_site(). Then the HTML is knitted properly, but once I make changes to the Rmd file, the problem reoccurs.
I don't know what causes this behavior, and would appreciate any input. I have not updated the theme (Academic 4.6.0) or hugo version (0.59.1, minimum required for the theme version is 0.58). I have the latest version of R and Rstudio.
You might have run into a bug that was fixed two months ago in the development version of blogdown:
remotes::install_github('rstudio/blogdown')
Please restart R, and try to knit the document again.

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

Embed leaflet map created by R into github

I am struggling to embed leaflet map created R into my github account
The file exported into single HTML file and I can view it on my computer
When I upload and then open from github, it gives raw HTML code, not the interactive map
And I'm not even sure how to embed this interactive map in ReadMe.md. When I place the link to the html and click it, I get the same raw map.
Help!
Update
I created follow-up question, If HTML doesn't work well in github, how can some people make it work
You can prepend http://htmlpreview.github.io/? to the url of where you put the html file. In your case:
http://htmlpreview.github.io/?https://github.com/Sonihal/yorbalinda_interactive_map/blob/master/web_Yorba_Linda.html
Just in case anyone is still looking for a solution, I wrote a post explaning how to do it, assuming that you use Jekyll to build your website hosted on GitHub.
https://dieghernan.github.io/201905_Leaflet_R_Jekyll/
In short,you have to perform several steps:
On GitHub/Jekyll
Identify the code you need to include. I already did it for you See on my GitHub
Copy/paste it in the code that builds your <head>. This would depend on your Jekyll implementation. After doing that, the needed libraries would be loaded every time that Jekyll builds the website.
On Rstudio
Create a map with leaflet and knit it to a github_document. It is important to knit it using always_allow_html: yes in the front matter.
On Markdown
Check that every leafletmap has produced an html code in the .mdfile. Something like this:
<!--html_preserve-->
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
Paste this chunk before the <script> part:
<div id="htmlwidget-SOMEIDNUMBER" style="width:100%;height:216px;" class="leaflet html-widget"></div>
So you got something like this for each leaflet map:
<!--html_preserve-->
<div id="htmlwidget-SOMEIDNUMBER" style="width:100%;height:216px;" class="leaflet html- widget"></div>
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
Now publish it on GitHub. Now when Jekyll builds your blog/web hosted on GitHub the libraries are loaded and the leaflet map displays correctly. Note that on local or markdown the map is still not visible.
Once that you have done it a couple of times you will see that is basicaly copy/paste/modify the chunk that I presented on step 5.

Resources