Render interactive treemap with highchart in flexdashboard - r

I am using flexdashboard to generate a interactive treemap. But the plot doesn´t render properly.
My code and output are the following:
---
title: "flexdashboard: Shiny Embedding"
output:
flexdashboard::flex_dashboard:
social: menu
source_code: embed
runtime: shiny
---
```{r global, include=FALSE}
library(tidyverse)
library(highcharter)
library(treemap)
## generate a sample dataset
sample<-tbl_df(data.frame(c("City1","City2","City3","City1","City2","City3",
"City2","City3"),
c("A","B","C","D","D","A","A","B"),
c(12,14,15,12,12,14,8,10)))
colnames(sample)<-c("City","Country","Amount")
variable<-"City"
trmap<-function(variable)
{
d<- cbind(sample[,variable],sample[,"Amount"])
tm <- treemap(d, index = variable,
vSize = "Amount")
highchart() %>%
hc_title(text = paste("Treemap de", variable ),
style = list(fontSize = "15px")) %>%
hc_add_series_treemap(tm)
}
```
Column {.sidebar}
-------------------------------------------
```{r}
selectInput(inputId="variable",label="group by",choices=c("City","Country"))
```
Column
------------------------------------------
### Plot
```{r}
renderPlot(trmap(input$variable))
```
The output of this code is the next dashboard:
But the plot initially is dynamic, because it shows the numbers of amount when I put the cursor in the box of every city.
How to render properly the plot to get the dynamic plot that shows the numbers??

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.

How do I stop a chart from showing on R markdown?

Aims
I have got the ouput that I want i.e. an interactive tree map. Yet, the R Markdown output also shows the static tree map, which I do not want to include.
I also want to adjust the font size on the interactive chart. I know how to adjust the font sizes of the labels in the static diagram using "fontsize.labels", but this does not affect the interactive tree map.
Reprex
---
title: "Untitled"
date: "03/05/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure, echo=FALSE}
library(treemap)
library(d3treeR)
# dataset
group <- c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup <- paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value <- c(13,5,22,12,11,7,3,1,23)
data <- data.frame(group,subgroup,value)
# basic treemap
p <- treemap(data,
index=c("group","subgroup"),
vSize="value",
type="index",
palette = "Set2",
bg.labels=c("white"),
align.labels=list(
c("center", "center"),
c("right", "bottom")
)
)
# make it interactive ("rootname" becomes the title of the plot):
inter <- d3tree2( p , rootname = "General" )
inter
Output
How do I get rid of the top static tree map?
How do I increase the font size in the interactive tree map?
If you can only answer one of the questions, please do.

Set the proportions of a gauge (from flexdashboard) plot in a RMD report

I'm using rmarkdown to make a report containing some gauge charts from flexdashboard, the problem is that I can't change the proportion of the chart.
Here's a reproducible example of the code I wrote:
---
title: "Reproducible Example"
author: "Name"
date: "3/6/2020"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
mtcars <- tibble::rownames_to_column(mtcars,"model_car")
MazdaRX4 <- mtcars[1,]
```
## Cars Gauge
A gauge displays a numeric value on a meter that runs between specified minimum and maximum values.
```{r fig1, fig.height = 1}
flexdashboard::gauge(MazdaRX4$mpg, min = 0, max = 50, symbol = 'mpg')
```
But when I knit the report my chart looks like this:
Is there a way to set the proportion of this kind of chart?
thanks in advance to anybody who can help!

add_tooltip in ggvis with ioslides

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.

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