I am using the summarytools package in Rmarkdown and knitting into an HTML. The tables look fine, but whenever I print the HTML document, the formatting of the tables (ctable in particular) is off. See picture below
As you can see, there are those borders within each cell. I believe this has to do with RMD using the default bootstrap.css file, and upon exploration of that file, I found some "#media print" lines. Does anyone have a quick solution to fixing this issue, or would I have to change the underlying bootstrap.css file?
Thanks.
I believe this has to do with RMD using the default bootstrap.css
file...
If this is the case, you may try canceling Bootstrap’s CSS with st_css(bootstrap = FALSE).
Also, you may find helpful to include results = "asis" to knitr chunk options.
Finally, check out Dominic's "Recommendations for Using summarytools With Rmarkdown" and section about ctable(). Acording to Dominic 'rendering method is preferred' with method = 'render':
---
title: "Title"
author: "Author"
date: "16/06/2020"
output:
html_document:
toc: TRUE
toc_float: TRUE
---
```{r setup, include = FALSE}
library(knitr)
library(summarytools)
knitr::opts_chunk$set(results = "asis")
```
```{r summarytools-css, echo = FALSE}
# with summarytools’ CSS we can cancel Bootstrap’s CSS (both are included by default)
# without it odd layout are expected, especially with dfSummary()
st_css(bootstrap = FALSE)
```
```{r summarytools-rmarkdown, echo = FALSE}
ctable(tobacco$gender, tobacco$smoker, style = 'rmarkdown')
```
```{r summarytools-html, echo = FALSE}
print(ctable(tobacco$gender, tobacco$smoker), method = 'render')
```
Related
I'm currently writing something in R Markdown. Whenever I Knit the document, RStudio's preview takes me back to the very beginning of the document. Is there a way to make this new preview display a location closer to where I've been working by default? For example, can I make it preview at a location near where my cursor for typing is?
The comments have suggested a number of workarounds. So far, my best is to just type the section number of the section where I'm working in to the search bar that RStudio's preview window provides. I'd click on the relevant entry in the table of contents, but I use output: github_document: toc: true number_sections: true, which is waiting on a patch to its numbered tables of contents.
Not quite as simple as you had in mind, but it is possible to use javascript to control what section of an html document is displayed:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{js}
location.hash = "#goto";
```
```{r, results='asis'}
cat(sprintf("# %s\n\nSection text here.\n\n", 1:10), sep = "")
```
# GOTO
Scroll Here
```{r, results='asis'}
cat(sprintf("# %s\n\nSection text here.\n\n", 11:20), sep = "")
```
I'm struggling with what to Google to start solving this one. It might be a Windows question and not an R question but I could do with some help.
I am using rmarkdown::render to generate html reports. I have a master.Rmd which calls some child_docs. I use the argument output_file = to name the html document. This works fine and I can successfully generate documents called my_report1.html. When I open the html document in my browser (both Chrome and FireFox) the browser tab is labelled as master.utf8.md:
In the past the tab label used to be my_report1.html. I want to fix this because I regularly have multiple reports open and navigating between the tabs to find which report I want is now painful.
Any thoughts on what to check?
The YAML:
---
output:
html_document:
toc: true
toc_float: true
params:
lot: 1
editor_options:
chunk_output_type: console
---
Chunk setup:
```{r setup, include=FALSE}
## GLobal chunk options
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
Update: I think this is to do with the YAML title:. I'm going to open a new question with a better example.
I managed to cobble together a work around for this. The tab name in a browser comes from the title declared in the YAML even if you supply a file name to output_file in rmarkdown::render. I wanted to set my title dynamically and this can be done with using the methods described here, example:
---
output: html_document
---
```{r}
title_var <- "Sample Title"
```
---
title: `r title_var`
---
However, I needed an extra work around to suppress the title because I didn't actually want it to make a title. This can be done by using the following css (top of doc or separate file):
<style>
.title{
display: none;
}
</style>
I was wondering if there is a way to include the company logo to a PDF document created by R Markdown only on the first page of the document. I have searched for an answer and the closest I could find is this. There are multiple solutions presented in the answers, however, neither of them works for me because they either:
include the logo at every page of the document (not just first); or
include the chapter title in the header (and a horizontal line below it) along with the logo.
I am looking for a way to include just the plain logo, with no chapter titles only on the first page of a PDF R Markdown document and I've ran out of all the resources I could find online. Is there someone who could aid me?
Before anyone asks: yes, it strictly has to be a PDF document, not HTML.
The \includegraphics code line is insert after the R SETUP chunk in markdown.
Here is an example of where I put the line \includegraphics :
---
geometry: "margin=1.5cm"
classoption: landscape
output:
pdf_document: # Export to pdf
number_sections: yes
includes:
in_header: ltx.sty
---
```{r SETUP, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
results = "asis")
library(knitr)
library(kableExtra)
library(some_packages)
options(scipen = 999,
digits = 2,
width = 110)
```
\definecolor{astral}{RGB}{87,146,204}
\allsectionsfont{\color{astral}}
\setcounter{tocdepth}{5}
<!-- Title page -->
\includegraphics[width=7cm]{logo.jpg}
\begin{center}\begin{Large}
Project 1
\end{Large}\end{center}
\vfill
# Some R chunk
```{r results='asis'}
# Table, code, graphics.
```
So the line is insert between the R SETUP chunk and the next R chunk.
Is it possible to add a CSS class to a certain code chunk?
Assume the following file:
---
title: "Untitled"
output: html_document
---
```{r cars}
summary(cars)
```
I want to give the chunk labeled 'cars' a certain CSS class, e.g. .myClass.
Is there any possibility like
```{r cars} {.myClass}
summary(cars)
```
or so? I am aware of hacks like wrapping the whole chunk in another <div>. I am interested in a straight forward solution.
Edit: this feature was introduced in knitr v.1.16 (05/18/17)
class.source and class.output options apply additional HTML classes to source and output chunks (see knitr documentation).
To add myClass to source chunk:
```{r cars, class.source='myClass'}
summary(cars)
```
Previous answer that inspired the class.source options (see here)
You can add a class using the fenced_code_attributes pandoc's extension (which is intended to add attributes to the <pre> tag, see here) and a knitr output hook.
The following example works fine:
---
title: "Untitled"
output:
html_document:
md_extensions: +fenced_code_attributes
---
```{r, include=FALSE}
knitr::knit_hooks$set(source = function(x, options) {
return(paste0(
"```{.r",
ifelse(is.null(options$class),
"",
paste0(" .", gsub(" ", " .", options$class))
),
"}\n",
x,
"\n```"
))
})
```
```{r cars, class="myClass1 myClass2"}
summary(cars)
```
After knitting this .Rmd file, the HTML document looks like this:
<pre class="r myClass1 myClass2">
<code>
summary(cars)
</code>
</pre>
The fenced_code_attributes extension is enabled by default: in standard cases, you don't need to include the line md_extensions: +fenced_code_attributes in your YAML header.
I don't know if there's more straightforward solution using knitr.
I would like to edit a single rmarkdown (Rmd) document with a list of "problems", each followed by its solution. Each solution may contain the results of the R console, but also some explaining (markdown and LaTeX formatted) text. Besides, I would like use knitr in 2 versions: with and without the solutions, changing the source as less as possible, and compiling.
I know that I can use a logical variable in order to conditionally evaluate R code and show plots and R output, but I don't know how to show/hide blocks of (markdown and LaTeX) formatted text, unless I put all that text into R character vectors, which seems hard for keeping things clean and readable.
I found this old question,
Conditionally display a block of text in R Markdown
where the solution was given for simple short text, which was included as an argument of the R print() function.
This other old question,
insert portions of a markdown document inside another markdown document using knitr
was for having a father document and child documents which were conditionally compiled, but I don't want to slice my document into so many pieces.
You could use the asis engine to conditionally include/exclude arbitrary text in knitr, e.g.
```{asis, echo=FALSE}
Some arbitrary text.
1. item
2. item
Change echo=TRUE or FALSE to display/hide this chunk.
```
But I just discovered a bug in this engine and fixed it. Unless you use knitr >= 1.11.6, you can create a simple asis engine by yourself, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) paste(options$code, collapse = '\n')
})
```
If you want to include inline R expressions in the text, you will have to knit the text, e.g.
```{r setup, include=FALSE}
library(knitr)
knit_engines$set(asis = function(options) {
if (options$echo && options$eval) knit_child(text = options$code)
})
```
There is a way to hide parts of the document (including text and chunks): to comment them out with html comment marks.
And comment marks can be generated by R in a block according to a variable that can be set at the beginning of the document.
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
And just to show a complete working example, in the example below the middle section of the document can be shown or hidden by setting the hide variable to FALSE or TRUE. That might be useful in case there are several sections to hide or show at once - for example, solutions of course problems.
---
title: "Untitled"
date: "15/10/2020"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
hide <- TRUE #TRUE to comment out part of the document, FALSE to show.
```
## Start
Always shown.
```{r}
hide
```
```{r results='asis', echo=FALSE}
if (hide) {cat("<!---")}
```
## To hide or not to hide
To be hidden or shown according to *hide* variable.
```{r}
"Also to be hidden according to 'hide' variable"
hist(rnorm(10))
```
```{r results='asis', echo=FALSE}
if (hide) {cat("-->")}
```
<!--
Never shown.
-->
## End
Always shown.
Just a caveat: in html output the hidden parts are kept as comments and can be seen just by viewing the source. On the other hand, PDF (LaTex) and Word outputs ignore html comments and the hidden parts aren't included in the knitted documents.
Therefore, when the hidden parts are supposed to be somehow confidential (e.g. exam solutions) PDF or Word output should be used instead of html.
For those looking for a solution when knitting to pdf through LaTex, the answer from #Pere won't work for you (because LaTex doesn't understand the <!--- --> pair as indicating a comment).
Below is one possible workaround:
---
output:
pdf_document
---
\newcommand{\ignore}[1]{}
```{r echo=FALSE}
include <- TRUE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```
```{r echo=FALSE}
include <- FALSE
```
```{r results='asis', echo=FALSE}
if(!include){cat("\\ignore{")}
```
NOT Included bla bla
```{r results='asis', echo=FALSE}
if(!include){cat("}")}
```