下载表格为图像r

时间:2017-03-06 15:33:36

标签: r shiny

我希望能够将表格下载为图像(PNG或JPEG)。我们假设我的数据帧是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)
    }
)

1 个答案:

答案 0 :(得分:1)

要将表格下载为图像,您可以使用库gridExtra中的grid.table函数。这是一个可以用作模板的代码:

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)

希望它有所帮助!