wittywillis
wittywillis

Reputation: 77

Download table as image r

I want to be able to download a table as an image(PNG or JPEG). Let's assume that my dataframe is df

output$statsTable <- renderTable({
    #Printing the table
    df
})

output$downloadStatsTable <- downloadHunter(
    filename = function() {
        paste(getwd(), '/test.png', sep = '')
    },

    content = function(con) {
        p <- grid.table(df)
        device <- function(..., width, height) grDevices::png(..., width = 12, height = 9, res = 300, units = "in")
        ggsave(file, plot = p, device = device)
    }
)

Upvotes: 0

Views: 1188

Answers (1)

SBista
SBista

Reputation: 7704

To download table as image you can use grid.table function from library gridExtra. Here is a code which you could use as a template:

library(gridExtra)
library(shiny)


df <- head(datasets::iris)

ui <- fluidPage(
  tableOutput("statsTable"),
  downloadButton('downloadStatsTable ', 'Download')
)


server <- function(input, output) {
output$statsTable <- renderTable({
  #Printing the table
  df
})


output$downloadStatsTable  <- downloadHandler(
  # Create the download file name
  filename = function() {
    paste("data-", Sys.Date(), ".jpeg", sep="")
  },
  content = function(file) {
    grid.table(df)
    jpeg(file=file) 
    grid.table(df) #Create image of the data frame
    dev.off()
  })      

}

runApp(list(ui = ui, server = server), launch.browser = TRUE)

Hope it helps!

Upvotes: 2

Related Questions