2014-11-05 25 views
8

से पीएनजी डाउनलोड करना मैं चमकदार (और आर) के लिए काफी नया हूं और चमकदार रूप से एक पीएनजी फ़ाइल में जो साजिश बना रहा हूं उसे निर्यात करने के साथ संघर्ष कर रहा हूं।चमकदार (आर)

मैं इन दो धागे को देखा, लेकिन यह समझ नहीं सका:

Save plots made in a shiny app Shiny downloadHandler doesn't save PNG files

मैं ui में डाउनलोड बटन बनाने के लिए प्रबंधन और सर्वर सब कुछ मैं यह करना चाहते हैं कर किया जा रहा है इसे भी करो। जब मैं पूर्वावलोकन विंडो में डाउनलोड बटन दबाता हूं, तो एक पॉप अप विंडो मुझे फ़ाइल स्थान और नाम निर्दिष्ट करने के लिए कहती है लेकिन कोई फ़ाइल सहेजी नहीं जाती है। जब मैं ब्राउज़र विंडो में ऐसा करता हूं, तो एक पीएनजी फ़ाइल बनाई जाती है लेकिन यह खाली है।

कोई अंतर्दृष्टि बहुत सराहना की है!

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() 
     })  

    }) 

उत्तर

12

यह अजीब परिदृश्य का संभावित हल shiny-discuss google group पर चर्चा की गई। आप क्या कर सकते हैं बस एक सामान्य कार्य में अपने प्रतिक्रियाशील plotInput कथन को बदल सकते हैं। सुनिश्चित नहीं है कि क्यों downloadHandler प्रतिक्रियाशील वस्तुओं के साथ अच्छा नहीं खेलता है।

# change 
plotInput <- reactive({...}) 

# into this 
plotInput <- function(){...} 

तुम भी downloadHandler कॉल में प्रिंट बयान निकाल सकते हैं:

output$downloadPlot <- downloadHandler(
     filename = "Shinyplot.png", 
     content = function(file) { 
     png(file) 
     plotInput() 
     dev.off() 
     })  
+0

यह शानदार है। पिछले कुछ घंटों में प्रतिक्रियाशील भूखंडों का संग्रह डाउनलोड करने की कोशिश कर रहा था, और क्या गलत हो रहा था, यह काम नहीं कर सका। मेरे दिमाग को समझने के लिए बहुत तले हुए हैं कि क्या 'प्रतिक्रियाशील' से 'फ़ंक्शन' में बदलने के लिए कोई नकारात्मकता है, लेकिन ऐसा लगता है कि यह अच्छी तरह से काम करता है – Adrian

संबंधित मुद्दे