Download_this button and plotly graph in RMarkdown

Viewed 178

I have a Rmarkdown document with various plotly graph. I want the users to be able to download the dataset behind every graph in the document. I have tried using the "download_this" package and it is working but it impacts my graph and moves the legend and axis names.

What can I do to avoid this ?

enter image description here

Here's my code :

---
title: "Fiche"
output:
  rmdformats::material:
    highlight: kate
    self_contained: no
---
```{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(ggplot2)
library(DT)
library(rmarkdown)
library(plotly)
library(scales)
library(plyr)
library(Cairo)
library(ggiraph)
library(shiny)
library(knitr)
library(extrafont)
library(openxlsx)
library(downloadthis)
library(kableExtra)
library(rmdformats)
# Evolution
```{r, echo = FALSE}
test2 <- read.xlsx("C:/Users/cccc/Documents/Classeur1.xlsx",sheet="Feuil2",colNames = TRUE)

q <- plot_ly(
  data = test2,
  x = ~Annee,
  y = ~Effectifs,
  color = ~Secteur,
  type = "scatter",
  mode = "lines+markers",
  marker =list(hoverformat = ',.0f')) %>% 
  layout(title = list(text ='Evolution des effectifs par secteur (indice 100)'), 
         xaxis = list(title = list(text ='Année scolaire')), 
         yaxis = list(title = list(text = 'Effectifs indice 100')), 
         legend = list(title=list(text='Secteur')),
         hovermode = ("x unified"))

ggplotly(q) %>% config(displaylogo = FALSE,
modeBarButtonsToRemove = c(
    'sendDataToCloud',
    'zoom3d',
    'zoom2d',
    'toggleSpikelines',
    'autoScale2d',
    'resetScale2d',
    'select2d',
    'lasso2d',
    'hoverClosestCartesian',
    'hoverCompareCartesian',
    'pan2d'
)
)

test2 %>%
  download_this(
    output_name = "test2 data set",
    output_extension = ".csv",
    button_label = "Download data",
    button_type = "warning",
    has_icon = TRUE,
    icon = "fa fa-save"
  )

Here's the information on "download_this" : https://fmmattioni.github.io/downloadthis/index.html

Thank you for your help!

1 Answers

In the download_this function, there's an argument called "self_contained" which its default value is FALSE. Specify the argument as TRUE and it should fix the issue:

test2 %>%
  download_this(
    output_name = "test2 data set",
    ...,
    self_contained = TRUE
  )
Related