Search code examples
rshinyshinydashboardrtf

How to have shiny app save gt ouput as an .RFT file on the users browser?


I have this simple shiny app that takes a user's input and passes it to a gt table. I also have two buttons that can save the table as an png or rtf.

The png works as intended and downloads the table through the user's browser.

My question is with the rtf button. Technically it works, but it saves it to my shiny app's directory and I would like the .rtf file to be downloaded through the users browser like the png button.

enter image description here

Here you can see it saved the .rtf file to my shiny apps directory enter image description here

Code

library(data.table)
library(shiny)
library(gt)
library(shinyscreenshot)

data <- datasets::mtcars 
setDT(data, keep.rownames = TRUE)[]


ui <- navbarPage("Save this to RTF",
                 tabPanel("Table", icon = icon("table"),
                          
                          sidebarLayout(
                            sidebarPanel(
                              selectInput("input",
                                          label = "Choose # of cyl",
                                          choices = c("All", data$cyl)),
                              screenshotButton(selector="#table", label = 'Download Png', filename = 'screenshot'),
                              actionButton('downloadData', 'Download RTF')
                            ),
                            
                            mainPanel(
                              gt_output("table")
                            )
                          )
                 )
)



server <- function(input, output, session) {
  
reactive_tab  <- reactive({
    d <- data
      if(input$input != "All")
        d <- subset(d, cyl == input$input)
      d 
  })

output$table <- render_gt( 
  reactive_tab() %>% gt()
  )


  observeEvent(input$downloadData,{
    gtsave(reactive_tab() %>% gt(), 
           paste0(Sys.Date(),".rtf"))
  })


}
shinyApp(ui, server)

Solution

  • downloadHandler() was the solution

    library(data.table)
    library(shiny)
    library(gt)
    library(shinyscreenshot)
    
    data <- datasets::mtcars 
    setDT(data, keep.rownames = TRUE)[]
    
    
    ui <- navbarPage("Save this to RTF",
                     tabPanel("Table", icon = icon("table"),
                              
                              sidebarLayout(
                                sidebarPanel(
                                  selectInput("input",
                                              label = "Choose # of cyl",
                                              choices = c("All", data$cyl)),
                                  screenshotButton(selector="#table", label = 'Download Png', filename = 'screenshot'),
                                  downloadButton('downloadData', 'Download RTF')
                                ),
                                
                                mainPanel(
                                  gt_output("table")
                                )
                              )
                     )
    )
    
    
    
    server <- function(input, output, session) {
      
      reactive_tab  <- reactive({
        d <- data
        if(input$input != "All")
          d <- subset(d, cyl == input$input)
        d 
      })
      
      output$table <- render_gt( 
        reactive_tab() %>% gt()
      )
      
      
      output$downloadData <- downloadHandler(
        filename = paste0("gttable-",Sys.Date(),".rtf"),
        content = function(file){
          gtsave(reactive_tab() %>% gt(),
                 file)
        }
      )
      
      # observeEvent(input$downloadData,{
      #   gtsave(reactive_tab() %>% gt(), 
      #          paste0(Sys.Date(),".rtf"))
      # })
      # 
      
    }
    shinyApp(ui, server)