-1

I'm currently writing a statistical study on rmarkdown (rmdformat readthedown) which contains a lot of plotly graphs. I had the issue that plotly graphs where not rendering and just leaving blank spaces after the second/third graph I was inclunding. I found a clever - or I thought it was - way to bypass it by using HTML widget as in the block below :


ggplot(graph) +
  aes(
    x = ab,
    y = ord,
    fill = fill
  ) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Set1", direction = 1) +
  labs(
    title = ""
  ) +
  theme_minimal() +
  theme(legend.position = "none") -> GME
ggplotly(GME)->GMED

htmlwidgets::saveWidget(GMED, 
                        file.path(results_dir, 'GMED.html'))

and then using an iframe to include it in the doc

<embed scrolling='no' seamless='seamless' style='border:none'
src='results\GMED.html' width='800' height='500'></EMBED> 

But my dumbass didn't think that html widgets aren't self-contained so I can't juste send a single html document to my clients or else it just doesn't appear.

Is there a way to go around this ? Maybe I should just try to resolve the plotly not displaying issue but I spent several hours on it and just couldn't do it.

Thanks in advance for you time and help.

  • `htmlwidgets` are self-contained (at least by default). The problem is that in your `rmd` code you are linking to another file. If you send the output html to someone else and do not include the other file, this is not going to work. You could share both either locally (e.g. email a folder to be unzipped) or online (i.e. the `iframe` would link to a url which would exist). However I think you already know the best way to solve the problem is to fix the issue of the plotly graphs not rendering. If you post a minimal example of the issue you are having with that, perhaps we can help! – SamR Apr 04 '22 at 09:56
  • I pasted my code in the answer below ! Hope it is renough to be a reproducible example. – Julien Kalamon Apr 04 '22 at 11:27
  • I'm afraid this is not reproducible, as you are reading in a spreadsheet, which anyone answering the question does not have access to. It is also not minimal - it's almost 300 lines of code! Could you create a [minimal example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), without the css and unnecessary packages, which just has the `plotly` plots? This might actually help you isolate the problem - see if it occurs in a basic example. It is also worth posting as a separate question as really you are not asking about `htmlwidgets` anymore. – SamR Apr 04 '22 at 11:38
  • I will do this ! Thanks a lot for your advice – Julien Kalamon Apr 04 '22 at 11:40

1 Answers1

0

As I was asked to provide an example of the plotly issue, here is the code I use. Both graphs in the section "D.Notes" do not appear on the final document.

It gets worse if I replace the Text blocs with actual text.

Please let me know if I can make it any easier for it to be a reproducible example.

Thanks for all your insights

---
title: "test file for stackoverflow"
author: "Julien Kalamon"
date: "`r Sys.Date()`"
output:
  rmdformats::readthedown:
    highlight: kate 
    css : style.css
---

{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = TRUE, echo=FALSE)
library(ggplot2)
library(esquisse) 
library(readxl)
library(dplyr)
library(plotly)
library(knitr)
library(htmltools)
library(IRdisplay)
assur <- read_excel("C:/Users/LENOVO/Downloads/Benchmark appli assurance.xlsx")



<style>
p {
    font-size: 16px;
    line-height: 24px;
    margin: 0px 0px 12px 0px;
}

h1, h2, h3, h4, h5, h6, legend {
    font-family: Arial, sans-serif;
    font-weight: 700;
    color: #1A658F;
} 
body {
text-align: justify; 
}
</style>

# 1. Présentation de l'étude 

## 1.1 Motivation & Objectifs

### A. Constats de départ

Text

### B. Objectifs

Text

### C. Hypothèses de départ 

Text

## 1.2 Périmètre & Définitions

### A. Définitions

Text

### Périmètre d'étude 

Text

{r, echo = FALSE}
library(rmarkdown)
paged_table(assur)
assur$`Téléchargements (en k)` <- factor(assur$`Téléchargements (en k)`, levels = c("5-10","10-50","50-100","100-500","500-1000","5000-10000"))



# 2. Phénomènes observés 

## 2.1 Présentation du groupe d'étude

{r aldo, echo=FALSE}
ggplot(assur) +
  aes(x = Type) +
  geom_bar(fill = "#1A658F") +
  labs(
    y = "Total",
    title = "Répartition du groupe test par type de structure"
  ) +
  theme_minimal() -> d
ggplotly(d)



text
 
### A. Applications

{r adio, echo=FALSE}
ggplot(assur) +
  aes(x = Applications, fill = Type) +
  geom_histogram(bins = 5L) +
  scale_fill_hue(direction = 1) +
  labs(x = "Nombre d'applications", y = "Total", title = "Nombre d'apllications clients par assureur", 
       fill = "Type d'assureur") +
  theme_minimal() -> p1
ggplotly(p1) ->p1 
p1


Text

### B. Chiffre d'affaires

{r ada, echo=FALSE}
ggplot(assur) +
 aes(x = "", y = `Chiffre d'affaire (en M\200)`) +
 geom_boxplot(fill = "#1A658F") +
 labs(x = "groupe test", 
 title = "Répartition des chiffres d'affaires dans le groupe test") +
 theme_minimal() -> p3
ggplotly(p3) 



Text

{r pressure, echo=FALSE}
assur %>%
 filter(Type %in% c("Généraliste", "Prévoyance")) %>%
 ggplot() +
 aes(x = Type, y = `Chiffre d'affaire (en M\200)`, fill = Type) +
 geom_boxplot() +
 scale_fill_hue(direction = 1) +
 labs(x = "Type d'assureur", title = "Répartition des chiffres d'affaires") +
 theme_minimal() +
 theme(legend.position = "none")->p5
ggplotly(p5)


Text

### 

{r pressure2, echo=FALSE}
assur %>%
 filter(Type %in% c("Startup", "Courtier")) %>%
 ggplot() +
 aes(x = Type, y = `Chiffre d'affaire (en M\200)`, fill = Type) +
 geom_boxplot() +
 scale_fill_viridis_d(option = "viridis", 
 direction = 1) +
 labs(x = "Type d'assureur", title = "Répartition des chiffres d'affaires") +
 theme_minimal() +
 theme(legend.position = "none") -> p4
ggplotly(p4)


Text

### C. Téléchargements 

{r}
results_dir <- 'results'
if(!dir.exists(results_dir)) dir.create(results_dir)

ggplot(assur) +
 aes(x = `Téléchargements (en k)`) +
 geom_bar(fill = "#1A658F") +
 labs(y = "Total", title = "Nombre de téléchargements des applications du groupe test") +
 theme_minimal() -> dark
ggplotly(dark) 


Text

### D. Notes

{r}

ggplot(assur) +
 aes(x = "", y = `Note moyenne`) +
 geom_boxplot(fill = "#185F87") +
 labs(x = "Groupe test", 
 title = "Répartition des notes dans le groupe test") +
 theme_minimal() -> deli 
ggplotly(deli)




Text

{r}

assur %>%
 filter(!(Type %in% "Auto")) %>%
 ggplot() +
 aes(x = Type, y = `Note moyenne`, fill = Type) +
 geom_boxplot() +
 scale_fill_hue(direction = 1) +
 labs(x = "Type d'assureur", title = "Répartition des notes par type d'assureur") +
 theme_minimal() +
 theme(legend.position = "none") -> BOOKER
ggplotly(BOOKER)





Text 



## 2.2 Principales observations

### A. Notes & Chiffre d'affaires

{r}
ggplot(assur) +
  aes(
    x = `Chiffre d'affaire (en M\200)`,
    y = `Note moyenne`,
    colour = Type,
    label = Assureur
  ) +
  geom_jitter(shape = "circle", size = 2) +
  scale_color_hue(direction = 1) +
  labs(
    title = "Note moyenne en fonction du chiffre d'affaire de l'assureur"
  ) +
  theme_minimal()-> Ayton
ggplotly(Ayton)



Text

{r, echo=FALSE, warning=FALSE}
library(car)
scatterplot(`Note moyenne`~`Chiffre d'affaire (en M\200)`, data=assur) 


Text

### B. Notes & Nombre d'applications

{r, echo=FALSE, warning=FALSE}
ggplot(assur) +
  aes(x = Applications, y = `Note moyenne`, colour = Type, label = Assureur) +
  geom_point(shape = "circle", size = 2L) +
  scale_color_viridis_d(option = "viridis", direction = 1) +
  labs(
    title = "Nombre d'applications utilisateurs & note moyenne"
  ) +
  theme_minimal()-> blop 

ggplotly(blop)


Text 

{r,warning=FALSE}
as.character(assur$Applications)->assur$Applications
ggplot(assur) +
  aes(
    x = Applications,
    y = `Note moyenne`,
    fill = Applications
  ) +
  geom_boxplot() +
  scale_fill_brewer(palette = "Set1", direction = 1) +
  labs(
    title = "Répartition des notes par nombre d'applications"
  ) +
  theme_minimal() +
  theme(legend.position = "none") -> GME
ggplotly(GME)





text