Combining HTML CSS with Rmarkdown - html

On my way to becoming a datajournalist, I have come across lots of fantastic tools, such as R and Rmarkdown. Now that I work for a proper newspaper, I realize that most of the time, I end up using MS Excel (which, I don't deny, is also a great tool) and a concatenate function that I paste in an HTML file. I am losing quite a lot of my R knowledge and practice, and that's sad.
I know that I will soon be faced with a task that I would like to use as a way to get back to R and Rmarkdown.
I have some html pages working just fine and I'd like to change the data that is presented in them using Rmd.
My question is : is there a package or a way to combine into one Rmarkdown file most of an HTML page (header, css and javascript files and analytics, footer), and some Rmarkdown chunks ?
Thanks a lot for your answer.

Maybe you could try something like this (http://rmarkdown.rstudio.com/html_document_format.html#includes):
---
title: "Habits"
output:
html_document:
includes:
in_header: header.html
before_body: doc_prefix.html
after_body: doc_suffix.html
---
This would allow the combination of html and Rmd in general. Furthermore there is the chunk option child, which would allow to use Rmd files as chunk content?

You can use .css files to style your Rmakdown. In the header you do something like:
---
title: "Habits"
output:
html_document:
css: styles.css
---
see this

Related

Show output but hide code when sending Rmd to other people?

I have a problem with Rmarkdown when teaching. How can I send file .Rmd to other people without code but output remains?
In python (jupyter notebook, gg collab), I write code and run. After that, I delete the code and send the notebook to other people. They can't see code, but they know the output. Their mission is to write code to have the results as mine.
Is there any platform in R for designing exercises (hide code but show output)?
Thank you for all your advice.
There are a few things you can do with Rstudio and Rmarkdown, I will be creating HTML documents using Rmarkdown and Rstudio.
First, you can specify each R chunk to hide the code, this can be done with some chunk options, or easily clicking the preferred option. This is where you can see there is an option to "show output only", no matter the code inside the R chunk.
Second, you can use the below YAML settings that allow you to render a single HTML document, and allow the end user to select if they want to see the R chunk code, or hide it, while still showing the output.
---
title: "Untitled"
author: "author"
date: "9/25/2021"
output:
html_document:
code_folding: hide
---

How to change format of Warning admonition or add Caution in Sphinx HTML output

This seems like it should be straightforward but I've been prowling the documentation and web and haven't found the answer.
I want to output HTML doc from Sphinx. Ideally I'd like to have three levels of "note" type highlighted text boxes. ReST defines several "admonitions": (http://docutils.sourceforge.net/docs/ref/rst/directives.html#admonitions) but most of the Sphinx HTML themes include special formatting only for Note and Warning. (I am using one of the preinstalled themes, Classic.)
I have two questions:
1) How can I customize the color behind Warning in my documents?
2) How can I add a formatting style for Caution?
I see that these all end up with tags like <div class="admonition warning"> ... in the HTML output. But I can't find where the formatting for that class is defined. Is it in a stylesheet? Is it in a layout.html file or some other file?
Is there anything that explains how the various files in themes actually interact with each other? I haven't found a good primer. (I am no expert on css-based HTML either, so maybe that's part of the problem.)
Okay, I figured out more and have a working workaround. (I'm still not sure how I'm supposed to handle this.)
Looks like my HTML code is reading directly from a few cascading stylesheets stored along with the output in a directory called _static. There's classic.css, which inherits from basic.css.
I don't understand how these relate to the files named like basic.css_t that live in the Python Sphinx install.
To change things, should I (A) try altering the _t files? or (B) create an altered local copy of classic.css that lives in my source directory?
If I go with B, more questions.
Will it be overwritten by the values in the css_t template at build time? (I guess this is easy enough to test)
Is it good practice to use the same filename for a modified version of that stylesheet?
Here's a workaround that avoids those questions and seems to be doing what I want - from this: https://github.com/snide/sphinx_rtd_theme/issues/117
I created an override stylesheet that includes just the formatting I want to change.
I stored it in the _static of my source directory.
I defined it in my conf.py as follows:
html_context = {
'css_files': [
'_static/theme_overrides.css',
],
}
Now, that github discussion said that this wasn't a solution for all kinds of themes (including the RTD theme mentioned in the question) but I think I'm safe for now.
What more should I know?

How to incorporate html table in Rmarkdown

I used package 'sjPlot' to run some data analyses. For instant, function 'sjt.df' gives me a html table regarding simple description of variables. Then I created a Rmarkdown (see below). But when I clicked knit HTML/pdf, the result of the html table did not incorporate into the Rmarkdown. Rather, it popped up in my browser. How can I deal with that?
{r}
library(sjPlot)
data(iris)
sjt.df(iris)
See my tutorial basics of sjt-functions, section Knitr integration of HTML tables.
If you just want to have the table, use 'r sjt.df(iris, no.output=TRUE)$knitr' - (note that the ' have to be `).
The no.output=TRUE ensures that the table is not displayed in the viewer pane or browser, and the $knitr parameter contains the HTML-snippet that will be incorporated in RMarkdown.
If you also want to display the R-code, use
```{r eval=FALSE}
sjt.df(iris)
```
However, I guess that these tables are not converted well to PDF, only to HTML.

Making nicely formatted tables in Markdown: knitr not compiling stargazer>html table

I'm trying to embed an output table, nicely formatted by the stargazer function, in a markdown document.
I tried embedding the stargazer() in an R code block in the Rmd document:
```{r}
library(stargazer)
stargazer(cor(attitude), type="html")
```
The code runs correctly, but it outputs the html code that in turn is not parsed by knitr, so the actual table html 'source' code is shown in the rendered document.
I'm aware of this similar question (here). I'm asking a separate question because most answers there indicate that stargazer does not support html output, which is no longer true. Html support was probably incorporated into stargazer after that tread (and I donĀ“t have engouth rep to reopen or post comments there).
This seems related to a more simple problem of making knitr compile the html table source code.
EDIT: #hrbrmstr gave me the answer in the comment bellow, which is:
```{r results='asis'}
library(stargazer)
stargazer(cor(attitude), type="html")
```
Remove the ' from 'asis'
ex: {r results=asis}

How to define a TOC in HTML for kindlegen to recognize

I convert a book which is written in DocBook into a single page HTML. The HTML contains a TOC:
<div class="toc">
<dl>
<dt><span class="preface">Preface</span></dt>
<dt><span class="chapter"><a href="#installation-und-versionsauswahl">1. Version Selection and
Installation</a></span></dt>
[...]
I'd like to use kindlegen to convert the HTML into a file I can use with a Kindle. That works without a problem. BUT the TOC is not recognized as a TOC. The Kindle user can't access the TOC directly with the TOC button.
What do I have to change that kindlegen recognize the TOC in my HTML file?
I'd recommend reading the official Kindle publishing guidlines from Amazon.
AFAIK kindlegen can't do that, you need a proper NCX file or an OPF with properly set TOC setting.
See also this short tutorial.
In case useful, I knocked up a quick PHP script to generate very basic NCX and OPF files to support the TOC without having to break up the document. I wrote the script based on a MS Word documented saved as HTML (so it is hard coded to use those style names). Just noting it here in case useful to anyone who comes along this post in the future. http://alankent.me/2016/03/05/creating-a-kindle-book-using-microsoft-word-quick-note/