Ich bin ziemlich neu in Shiny (und R) und kämpfe mit dem Export der Handlung, die ich in Shiny zu einer PNG-Datei.Herunterladen von PNG von Shiny (R)
ich an diesen beiden Themen gesucht, aber kann es nicht herausgefunden:
Save plots made in a shiny app Shiny downloadHandler doesn't save PNG files
Ich verwalte den Download-Button in der Benutzeroberfläche zu erstellen und der Server scheint alles zu tun, ich mag auch tun. Wenn ich den Download-Button im Vorschaufenster drücke, erscheint ein Pop-Up-Fenster, in dem ich den Speicherort und den Namen der Datei angeben muss, aber keine Datei gespeichert wird. Wenn ich das gleiche in einem Browserfenster mache, wird eine PNG-Datei erstellt, aber sie ist leer.
Jeder Einblick wird sehr geschätzt!
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("This is a scatterplot"),
sidebarLayout(
sidebarPanel(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("varselect1"),
uiOutput("varselect2"),
downloadButton('downloadPlot', 'Download Plot')
),
mainPanel(
h4("Here is your scatterplot"),
plotOutput("plot1")
)
))
)
server.R
library(foreign)
shinyServer(function(session,input, output) {
DataInput <- reactive({
infile <- input$datafile
if (is.null(infile)) {
return(NULL)
}
read.csv(infile$datapath)
})
output$varselect1 <- renderUI({
if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)
cols <- names(DataInput())
selectInput("var1", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))
})
output$varselect2 <- renderUI({
if (identical(DataInput(), '') || identical(DataInput(),data.frame())) return(NULL)
cols <- names(DataInput())
selectInput("var2", "Select a variable:",choices=c("---",cols[3:length(cols)]), selected=("---"))
})
plotInput <- reactive({
a <- which(names(DataInput())==input$var1)
x_lab <- as.numeric(DataInput()[,a])
b <- which(names(DataInput())==input$var2)
y_lab <- as.numeric(DataInput()[,b])
main.text <- paste("Scatterplot of the variables",colnames(DataInput())[a],"and", colnames(DataInput())[b],sep = " ", collapse = NULL)
plot(x_lab, y_lab, main=main.text, xlab=colnames(DataInput())[a], ylab=colnames(DataInput())[b], xlim=c(min(x_lab),max(x_lab)*1.05), ylim=c(min(y_lab), max(y_lab)*1.05))
observations <- DataInput()[,1]
text(x_lab, y_lab, labels=observations, pos=3)
})
output$plot1 <- renderPlot({
print(plotInput())
})
output$downloadPlot <- downloadHandler(
filename = "Shinyplot.png",
content = function(file) {
png(file)
print(plotInput())
dev.off()
})
})
Das ist fantastisch. Habe die letzten paar Stunden damit verbracht, eine Sammlung von reaktiven Plots herunterzuladen und konnte nicht herausfinden, was falsch lief. Mein Gehirn ist zu frittiert, um zu verstehen, ob es negative Auswirkungen auf den Wechsel von "reaktiv" zu "funktional" gibt, aber es scheint gut zu funktionieren – Adrian