Adding button next to code in RMarkdown to hide/show code - r

I have created an Rmarkdown file in a Kaggle Kernel and I am trying to do the following:
1) Set some code chunks as hidden but with a button next to them to allow the viewer to hide or display the code
2) Set some other codes chunks as not hidden with again a buttopn next to them to allow the viewer to hide or display the code
From what I could see in previous questions a suggested solution is
---
title: "test"
output:
html_document:
code_folding: hide
---
That unfortunately is not working for me. I have also set the following in the start of my script
```{r setup, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(echo= TRUE)
and before each code I have the following:
```{r, message = FALSE, warning=FALSE }
# Load packages
library("ggplot2")
library("pastecs")
library("dplyr")
library("corrplot")
So my question would be, what should I do to get 1) and 2)?
Thanks in advance.

Related

How to insert an image from a url into R markdown html vignette?

Similar questions have been asked before about inserting an image from a url (for example here and here ). However, I can't seem to get those solutions to work for me.
I am trying to create an R package vignette and a github.io vignette page. Initially I was saving my images to my machine and when building the vignette I was calling them locally via:
knitr::include_graphics("file/path/image.png")
However, when I use devtools::build_site() to build the github.io site, the images are understandably missing from the vignette. I have uploaded all the images to my GitHub repo and I was wondering if there is any way to display to these images when building the GitHub.io site?
I have tried the following but neither have worked. I just get a blank space where the image should be.
<center><src="https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"></center>
```{r out.width="100%"}
url <- "https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```
My YAML looks like this:
---
title: "myPackage"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{vivid}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "vig/"
)
options(rmarkdown.html_vignette.check_title = FALSE)
```
I figured out the solution to my problem from this. I needed to use the "raw" absolute link from the image. So in the example above, I should have used:
```{r out.width="100%"}
url <- "https://raw.githubusercontent.com/tidyverse/ggplot2/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```

knitr::include_graphics runs, but image cannot be displayed

I wanted to try using knitr::include graphics (link) in my R-markdown document. I am using it as suggested by this link (using the full path):
---
title: "Sample Document"
output:
word_document
---
```{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%'}
knitr::include_graphics("Z:/../SEM/semflow.png")
```
This is the picture. I tried installing the png package and also tried simply using:
```{r}
knitr::include_graphics("Z:/../SEM/semflow.png")
```
The code is accepted by knitr, which means that it found the file, but it shows up in the Word document like this:
Does anyone have an idea how this could happen?
Related question
A related question (without an answer) can be found here.
Help file: ?knitr::include_graphics

Can R Markdown be made to preview from anywhere other than the start of the document?

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 = "")
```

Rmarkdown tab name in browser

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>

Company logo in PDF output only on the first page

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.

Resources