ShinyAlert in Rmd Flexdashboard - r

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)

Related

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.

runjs to change location in observeEvent in flexdashboard?

I have a flexdashboard and there is an action button that does this:
actionButton(inputId="submit",
label = "Submit answers",
class="btn btn-success",
onClick="location.href='#section-your-results';")
then an observer that does this:
observeEvent(input$submit,{
some calculation
})
The problem is that the onClick part is executed before the observeEvent is triggered. So the calculation doesn't apply to the section I'm jumping to.
What i want to do is this:
actionButton(inputId="submit",
label = "Submit answers",
class="btn btn-success")
and then have an observer that does this:
observeEvent(input$submit,{
some calculation
useShinyjs()
runjs("location.href='#section-your-results';")
}))
But for some reason the runjs command isn't being executed (I also tried replacing the location.href with an alert to confirm. Any idea on what is going wrong? I have seen this answer and tried that approach as well with no luck.
Here is (for me) a reproducible example - I expect an alert box to pop up when I click submit, but it doesn't for me
---
title: "reprex shinyjs rmd"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shinyjs)
useShinyjs(rmd = TRUE)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
onclick("submit",runjs('alert("Hello! I am an alert box!!");'))
```
### Chart B
```{r}
actionButton(inputId="submit",
label = "Submit answers",
class="btn btn-success")
```
So to get this to work I had to do two things:
Switch the location of the useShinyjs() to a non-setup chunk.
Change the onClick to an onEvent and watch for the event of the input being clicked.
I think that this is because the shiny input element disables to default click event for a button and thus shinyjs is watching for something that never happens.
Here's the reproducable example that works.
---
title: "reprex shinyjs rmd"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shinyjs)
```
Column {data-width=650}
-----------------------------------------------------------------------
```{r}
useShinyjs(rmd = TRUE)
actionButton(inputId="submit",
label = "Submit answers",
class="btn btn-success")
```
```{r}
observeEvent(input$submit, {
runjs('alert("Hello! I am an alert box!!");')
})
```

How to disable button based on a condition in a R Markdown document with shiny elements?

Suppose we have a group of 8 checkboxes (8 letters) and an action button which prints the label of all the selected checkboxes. What I want to do, enable and disable the state of the action button based on a condition. The condition is that if the number of selected checkboxes are between 2 and 5, then the button should be enabled, else disabled. For changing the state of the button I want to use the functions enable, disable or toggleState functions from the shinyjs package. And when the button is enabled, I will be able to trigger an event to print the numbers of selected items.
Here is what I tried until now:
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
if(result()>1 & result()<=5)
enable("action")
else
disable("action")
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
```
Took me awhile to find it, but you have to enable shinyjs to use R-markdown explicitly, it needs to setup its javascript differently in this case.
You do this by calling: useShinyjs(rmd=T) in the chunk where you are using it.
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
useShinyjs(rmd=T)
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
useShinyjs()
if(result()>1 & result()<=5){
enable("action")
} else {
disable("action")
}
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
Screen shot:

How to print htmlwidgets to HTML result inside a function?

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

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