2017-03-06 2 views
0

Ich möchte in der Lage sein, eine Tabelle als Bild (PNG oder JPEG) herunterzuladen. Nehmen wir an, dass meine Datenrahmen ist dfDownload Tabelle als Bild r

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

Was ist Ihr Problem? Funktioniert dein Code nicht? Welche Fehler haben Sie? –

+0

Das Problem ist, ich habe eine Schaltfläche, die beim Klicken ein Bild der Tabelle herunterladen und ich weiß nicht, wie es geht. Der obige Code funktioniert nicht, da es keine zu downloadende Handlung gibt. – wittywillis

Antwort

0

Um Tabelle herunterladen, wie Bild, das Sie grid.table Funktion aus der Bibliothek gridExtra verwenden können. Hier ist ein Code, den Sie als Vorlage verwenden könnten:

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) 

Ich hoffe, es hilft!