How to print htmlwidgets to HTML result inside a function? - r

---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
library(DT)
xsx = function(){
print(getOption("viewer"))
print(datatable(data.frame(d =1)))
1
}
xsx()
```
Inside xsx() function, DT widgets will not be rendered in HTML result. How can I get widgets print inside a function?

Related

Links between child documents in RMarkdown

I am writing my PhD thesis in R Markdown and I would like to add links to one child document (Introduction) allowing the reader to jump to another child document (one of the papers).
This is my main document:
---
output:
pdf_document:
latex_engine: pdflatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```
```{r chapter1, child = 'child1.Rmd'}
```
\newpage
```{r chapter2, child = 'child2.Rmd'}
```
This is how I tried to link my child1 and child2:
This is my second child document. Here I want a link to [`Child document 1`](./child2.Rmd#header1).
But when I click on the "Child document 1", it jumps to the original rmd file.
TLDR: is it possible to add clickable links between different child documents??
R version 4.2.2, Ubuntu 20, rmarkdown 2.18
Hi it works the same way as internal links.
Parent document:
---
output:
pdf_document:
latex_engine: pdflatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```
# Parent Header
implicit internal Link to header [Parent Header]
## Child links:
This implicit links to child 1 [C1 Second header]
Here I give a explicit link to [Go to second child](#child2_header1)
\newpage
# Child 1
```{r chapter1, child = 'child1.Rmd'}
```
\newpage
# Child 2
```{r chapter2, child = 'child2.Rmd'}
```
Child 1:
---
title: "child1"
author: "Valentin Haberl"
date: "`r Sys.Date()`"
output: pdf_document
---
## I am a child document {#child1_header1}
## C1 Second header
Some more Info
Child 2.
---
title: "child2"
author: "Valentin Haberl"
date: "`r Sys.Date()`"
output: pdf_document
---
## I am a second child {#child2_header1}
Text for second child

Scrolling for a gt table chunk in Flexdashboard or Shiny

I have the following dashboard:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(gt)
library(dplyr)
library(tidyr)
Tab 1
=======================================================================
Column
-----------------------------------------------------------------------
### Chart A
```{r}
mtcars %>%
gt()
But I can't see all the table generated by gt because I can't scroll it. So I tried to add a css style chunk:
.chart-stage {
overflow: auto;
}
But I can achieve what I want, which is to add a scroll bar to see all the table. Please, any help will be greatly appreciated.
After reviewing the css selectors, this is the solution for the question, you'll have to add this chunk to allow scrolling:
```{css}
.chart-shim {
overflow: auto;
}
```
I had the same problem, tried many things and ultimately found this solution using the htmltools package
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(gt)
library(dplyr)
library(tidyr)
require(htmltools)
```
Tab 1
=======================================================================
Column
-----------------------------------------------------------------------
### Chart A
```{r}
div(style='height:800px; overflow-y: scroll', gt(mtcars))
```

ShinyAlert in Rmd Flexdashboard

I am trying to render a popup in an Rmd flexdashboard.
Here is my code:
---
title: "Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include= FALSE}
library(shinyalert)
```
```{r}
useShinyalert( )
actionButton("helpBtn", "Help")
```
```{r}
observeEvent(input$helpBtn, {
shinyalert(title = "Help Me!", text = "Please contact your instructor")})
```
The button shows up but when clicked it does not show the popup. Any ideas?
I've been having the same issue, and I don't think you can do this with shinyalert because of the need for useShinyAlert() - adding extra dependencies into Rmd documents doesn't seem to be supported very well.
A workaround is to use sendSweetAlert from the shinyWidgets package:
---
title: "Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include= FALSE}
library(shinyWidgets)
```
```{r}
actionButton("helpBtn", "Help")
```
```{r}
observeEvent(input$helpBtn, {
sendSweetAlert(session, title = "Help Me!", text = "Please contact your instructor")})
```
Not sure if your issue was resolved or not, but setting the rmd parameter in useShinyalert to TRUE should solve your problem.
useShinyalert(rmd = TRUE)

xaringan slide separator not separating slides

In this example xaringan presentation, why are both the ## blank page and the leaflet map on the same slide, given I've separated them by the new-slide separator --- ?
---
title: "map test"
output:
xaringan::moon_reader:
css: ["default"]
nature:
highlightLines: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## blank page
content
---
leaflet page
```{r}
library(leaflet)
leaflet() %>%
addTiles()
```
---
Looks like you've got an unintended space after the new slide separator after blank content as "--- ". Remove that space and it'll be recognized as real slide separator:
---
title: "map test"
output:
xaringan::moon_reader:
css: ["default"]
nature:
highlightLines: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## blank page
content
---
leaflet page
```{r}
library(leaflet)
leaflet() %>%
addTiles()
```
---
In my case I'm adding mathjax/latex equations and I had:
$$
\begin{aligned} P(Y= k)=\comb{k-1}{r-1} * p^r q^{k-r}, \qquad k= r,r+1
\end{aligned}\label{pascal}\tag{5}
$$
And I had to remove the breaklines
$$\begin{aligned} P(Y= k)=\comb{k-1}{r-1} * p^r q^{k-r}, \qquad k= r,r+1
\end{aligned}\label{pascal}\tag{5}$$
and then it worked. I've that it renders mathjax better if all the code is in a single line.

can I produce a figure caption in r markdown with eval=false?

If I have the MWE:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
summary(cars)
```
then I do not get a caption. But if I do:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers"}
summary(cars)
```
i.e. remove eval=FALSE then the caption no longer loads.
why I wish to do this?
I want to put example bits of code into my document. the code won't actually work, hence why I want to supress it. Something like
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
for (i in 1:length(c){
#do something
}
```
where I am merely demonstrating a for loop, but not actually running the code.
As far as I know, knitr doesn't support captions for code by default. The easiest way to label your code blocks would be to add an explanation below the box in the markdown.
If you must have captions in the r code, you can use chunk hooks. Here's an example for your case:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
```{r}
library(knitr)
knit_hooks$set(wrapper = function(before, options, envir) {
if (!before) {
sprintf(options$comment)
}
})
```
```{r comment="Figure 1. Some random numbers",wrapper=TRUE,eval=FALSE}
for (i in 1:length(c){
#do something
}
```
We have defined a hook (wrapper), where if we call wrapper=TRUE in any chunk options, the comment argument is printed below.

Resources