Sono abbastanza nuovo per Shiny (e R) e ho difficoltà ad esportare la trama che faccio in Shiny su un file png.Download png da Shiny (R)
ho guardato questi due thread, ma non poteva capirlo:
Save plots made in a shiny app Shiny downloadHandler doesn't save PNG files
riesco a creare il pulsante di download nell'interfaccia utente e il server sembra fare tutto quello che voglio io fare anche questo Quando premo il pulsante di download nella finestra di anteprima, una finestra pop-up mi chiede di specificare il percorso e il nome del file ma nessun file viene salvato. Quando faccio lo stesso in una finestra del browser, viene creato un file png, ma è vuoto.
Qualsiasi intuizione è molto apprezzata!
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()
})
})
Questo è fantastico. Ho passato le ultime ore a cercare di scaricare una collezione di trame reattive e non ho potuto capire cosa stesse andando storto. Il mio cervello è troppo fritto per capire se ci sono dei negativi nel cambiare da 'reattivo' a' funzione', ma sembra funzionare bene – Adrian