Search code examples
rshinyjspdfshinyappsshinyjs

Is it possible to export a div having an id with all its contents using R & Shiny as done for example in javascript with JsPdf?


I know the question is a bit silly but hey it's worth it. Usually, when exporting files with datatables in R and Shiny it's quite easy but today I would like to be able to export all the content of a div which contains for example paragraphs, html tables and etc... without however losing the CSS, is it possible to do this with R and Shiny?

library(shiny)

#UI
ui = fluidPage(
  downloadButton("download", "Print as pdf"),
  fluidRow(
    box(width = 12,id="PS1AGLOBAL",dataTableOutput("table_pse1aglobal"),title="",
      collapsible = T,
       fluidRow(
        column(10, #wellPanel(p("Column width 8"))),
        column(2,tags$p(style="position: relative; top: -15px;", "PSE 1A global"))),
        fluidRow(
                  column(8, tags$p(style="text-decoration: underline; text-transform: capitalize; font-size: 20px; font-weight: bold; letter-spacing: 2px;", "DECLARATION")
         ),
     column(4)
    ),
    fluidRow(
      uiOutput("generated_declaration")
    ),
 )

 #SERVER

 server <- function(input, output, session) {
  downloadHandler(
  filename = function () {
    #i don't know
  }
)
   }
   shinyApp(ui, server)

I don't know if I have posed my problem correctly, but I just want to export the content of the div with the id "table_pse1aglobal" in a pdf file when a button is clicked. I am open to any suggestions thank you.


Solution

  • You can use the capture package.

    library(shiny)
    library(shinydashboard)
    library(capture) # remotes::install_github("dreamRs/capture")
    
    # UI
    ui <- fluidPage(
      capture::capture_pdf(
        selector = "#PS1AGLOBAL",
        filename = "box.pdf",
        icon("camera"), 
        "Take PDF screenshot."
      ),
      fluidRow(
        box(
          width = 12,
          id = "PS1AGLOBAL",
          h1("HELLO"),
          title = "TITLE",
          collapsible = TRUE,
          fluidRow(
            column(
              10,
              column(
                2,
                tags$p(style = "position: relative; top: -15px;", "PSE 1A global")
              )
            ),
            fluidRow(
              column(
                8,
                tags$p(
                  style = "text-decoration: underline; text-transform: capitalize; font-size: 20px; font-weight: bold; letter-spacing: 2px;",
                  "DECLARATION"
                )
              ),
              column(4)
            )
          )
        )
      )
    )
    
    # SERVER
    
    server <- function(input, output, session) {
    
    }
    
    shinyApp(ui, server)
    

    This package is not on CRAN. Maybe shinyscreenshot can achieve the same result, and this package is on CRAN.


    EDIT

    Looks like that does not work well with the box, so put it inside a div:

      fluidRow(
        div(
          id = "PS1AGLOBAL",
          box(
            width = 12,
            h1("HELLO"),
            title = "TITLE",
            collapsible = TRUE,
            fluidRow(
              column(
                10,
                column(
                  2,
                  tags$p(style = "position: relative; top: -15px;", "PSE 1A global")
                )
              ),
              fluidRow(
                column(
                  8,
                  tags$p(
                    style = "text-decoration: underline; text-transform: capitalize; font-size: 20px; font-weight: bold; letter-spacing: 2px;",
                    "DECLARATION"
                  )
                ),
                column(4)
              )
            )
          )
        )
      )