add_tooltip in ggvis with ioslides - r

I'm trying to add a ggvis plot with a tooltip to an ioslides presentation using knitr:
---
title: "Untitled"
output: ioslides_presentation
---
```{r options}
library(knitr)
```
## Slide with ggvis plot
```{r, message=FALSE, warning=FALSE, echo=FALSE}
library(ggvis)
cars %>%
ggvis(~speed, ~dist) %>%
layer_points(fill.hover := "red") %>%
add_tooltip(function(data) data$dist)
```
If I plot the graph in RStudio the tooltip shows up properly in the Viewer, but when I knit it to html, only the fill.hover works, and no tooltip shows up.

Related

R Markdown Presentation: showing one plot in each slide

I have a list of 300 plots and want one PowerPoint slide to show one plot (300 slides). What's the best way to achieve this?
A toy example using the built-in iris dataset to create a list of plots:
purrr::map(names(iris[,-5]), function(col_name){
plot = iris %>%
ggplot(aes(x = !!as.name(col_name))) +
geom_histogram()
return(plot)
})
I hope to create PowerPoint slides with one plot on each slide.
I will be using the iris data set, which is a built-in data set often used to populate example code.
With the following code in a Rmd file:
---
title: "Test_PowerPoint"
author: "KoenV"
date: '2022-06-30'
output: powerpoint_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r include=FALSE, warning=FALSE, message=FALSE}
library(tidyverse)
library(purrr)
```
```{r, iris, fig.cap="A scatterplot.", echo=FALSE, warning=FALSE, message=FALSE}
## your code
purrr::map(names(iris[,-5]), function(col_name){
plot = iris %>%
ggplot(aes(x = !!as.name(col_name))) +
geom_histogram()
return(plot)
})
```
And after hitting the "knit" button, you will see a PowerPoint presentation appearing, with the following lay-out:
Please let me know, whether this is what you wanted.

Using bookdown commands in ioslides presentation

Using bookdown commands in an R markdown presentation with output format "beamer" works fine (see also this SO-post).
In the YAML header, one simply has to change from
output:
beamer_presentation: default
to
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
However, how to use bookdown commands in an R markdown presentation with output format "ioslides"
MWE:
---
title: "ioslides_presentation with bookdown commands"
output:
ioslides_presentation: default
# Not working:
# bookdown::pdf_book:
# base_format: rmarkdown::ioslides_presentation
---
# Set of slides
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with bookdown cross references
- Figure: \#ref(fig:plot)
- Table: \#ref(tab:table)
- Slide without ref: \#ref(some-slide)
- Slide with ref: \#ref(slide-with-ref)
## Slide with Plot
```{r plot, fig.cap='Plot caption'}
plot(pressure)
```
## Slide with Table
```{r table}
knitr::kable(head(mtcars[, 1:3]),
caption = "Table caption")
```
## Some Slide
Some text
## Another Slide {#slide-with-ref}
Some more text

How do I produce a plot in landscape [rotated by 90 degrees] orientation in knitr?

The following knitr code give me the plot below -- how do I plot it in a landscape orientation?
```{r}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
plot(tr)
text(tr)
```
If your want a pdf/LaTeX output it is quite easy with out.extra='angle=90' chunk argument :
---
title: "Rotation test"
output: pdf_document
---
```{r, out.extra='angle=90'}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
plot(tr)
text(tr)
```
In some circumnstances it is better to keep the graph as it but to rotate just one page in landscape format within you document.
You need pdflscape LaTeX package for this (included for example in the texlive-latex-base package in Ubuntu as "oberdiek").
In the following example the graph is extended to occupy a full A4 page in landscape format. NB : you must specify fig.align='center' to make it work.
---
title: "Rotation test"
output: pdf_document
header-includes:
- \usepackage{pdflscape}
---
```{r}
rm(list=ls())
library(tree)
set.seed(1111)
x1<-runif(100)
x2<-rnorm(100,mean=.3)
x3<-runif(100)
d1<-x1>0.5
d2<-x2>0.7
d3<-x3<0.2
y<-ifelse(d1,1,ifelse(d2,2,ifelse(d3,3,4)))
df<-data.frame(x1,x2,x3,y)
tr<-tree(y~.,data=df)
```
\newpage
\begin{landscape}
```{r fig.align='center', fig.width = 27/2.54, fig.height = 19/2.54}
plot(tr)
text(tr)
```
\end{landscape}
```{r}
summary(tr)
```

R Markdown Presentation not loading/ rendering interactive Plotly chart

I am using Plotly with R to create a chart that will be rendered in a R Markdown Presentation With Ioslides, but instead of showing the demo chart from the website like the following:
It is rendering the steps like this:
My code is pretty simple:
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r}
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
```
As Karthik Arumugham pointed out you need to display the plot, either by entering p or not assigning plot_ly to variable but calling it directly.
I'd suggest to explicitly state the missing variables (type='scatter', mode='markers') instead of suppressing the output messages. In addition you could add {r, warning=F} to get rid of the
Error: attempt to use zero-length variable name
message.
---
title: "R Markdown Presentation & Plotly"
author: "Eduardo Almeida"
date: "February 19, 2017"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Interactive plot with Plotly
```{r, warning=F}
suppressPackageStartupMessages({library(plotly)})
library(plotly)
plot_ly(economics, x = ~date, y = ~unemploy / pop, type='scatter', mode='markers')
```

Adding ggvis plot in RMarkdown document makes knitr::kable output render incorrectly

Reproducible example below. I lose formatting on the table whenever I include a ggvis figure.
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
library(dplyr)
library(ggvis)
library(knitr)
```
The following table looks fine...
```{r echo=FALSE, results='asis'}
cars %>% kable(format = 'markdown')
```
As long as I don't include this plot below
```{r, echo=FALSE}
pressure %>%
ggvis(x = ~temperature, y = ~pressure) %>%
layer_bars()
```
This is probably related to a bug that has been fixed in the development version of ggvis. If you install the latest with devtools::install_github('rstudio/ggvis'), it should work.

Resources