2015-08-14 20 views
5

Benim parlak uygulamasında bir indirme düğmesine tıklayabilmek istiyorum, bir pdf oluşturan bir işlevi (bir pakette) yürütmek zorunda a/results klasöründe, bu oluşturulan dosyayı parlak uygulama kullanıcısına bir indirme olarak sunun. Şu andaki download_portfolio indirme düğmesinin kodunu aşağıdaki sunucudan yapıştırdım (o kadar çok parça tekrar üretilebileceğinden emin değil). Herkesin neyin yanlış gittiğine dair bir fikri olup olmadığını görmek istedim, aşağıdaki hata mesajını alıyorum, ancak FUNCTION_TO_GENERATE_PDF_IN_/results() işlevi çalışmakta ve PDF oluşturuyor, ancak uygulama yeniden yüklüyor ve kullanıcı hiçbir zaman bir indirme için yönlendirilmiyor.Bir pdf oluşturmak için parlak uygulama kodu çalıştırın o zaman kullanıcı için bu pdf indir

Hata alıyorum (ancak pdf işlevimden yine de doğru şekilde üretildi, yalnızca uygulama yeniden yüklendi ve pdf'yi indirmek için bir teklif yok). Ben hatalı downloadHandler kullanıyorsunuz indirmek

observe({ 
    output$download_portfolio <- downloadHandler({ 
    FUNCTION_TO_GENERATE_PDF_IN_/results() 
    filename = function() { paste(input$pdfname) } 
    content = function(file) { 
     file.copy(paste("results/",input$pdfname, file, overwrite = TRUE) 
    } 
    }) 
    }) 

cevap

4

üzerinde çalışıyorum

Error in self$downloads$set(name, list(filename = filename, contentType = contentType, : 
     argument "content" is missing, with no default 

app.R kodu. "reactivity" parlak olduğundan observe() işlevine gereksiniminiz yoktur; bu, nesneyi nesne değiştiğinde güncelleştirilen ui öğelerine bağlayabileceğiniz anlamına gelir. Bu nedenle, sadece indirme-düğmesine bir downloadHandler atamak ve nasıl sağlamak istiyoruz dosyası oluşturmak için söylemek gerekir:

library(shiny) 

ui <- fluidPage(# just the download-button and a textInput for the filename 
    textInput("pdfname", "Filename", "My.pdf"), 
    downloadButton("outputButton", "Download PDF") 
) 

server <- function(session, input, output) { 
    # No need for an observer! Just assign the downloadHandler to the button. 
    output$outputButton <- downloadHandler(input$pdfname, function(theFile) { 
    # The first parameter is the name given to the file provided for download to the user. 
    # The parameter in the function (theFile) is a placeholder for the name that is later 
    # assigned to the download-file. 

    # Now you can call your pdf-generating function... 
    makePdf() 

    # ... and use file.copy to provide the file "in" the save-button 
    file.copy(from = "/results/myGenerated.pdf", to = theFile) 
    }) 
} 

# Sample pdf-generating function: 
makePdf <- function(){ 
    pdf(file = "/results/myGenerated.pdf") 
    plot(cars) 
    dev.off() 
} 

shinyApp(ui = ui, server = server) 

Ancak, önce dosyalarınızı depolamak gerekmez, isteyebileceğiniz İndirme düğmesinin "doğrudan" içine kaydedilmesi için:

library(shiny) 

ui <- fluidPage(# As above 
    textInput("pdfname", "Filename", "My.pdf"), 
    downloadButton("outputButton", "Download PDF") 
) 

server <- function(input, output) { 
    output$outputButton <- downloadHandler(input$pdfname, function(theFile) { 
    # Here, your pdf-generator is provided with the "filename" that is used 
    # to provide the file for the user. 
    makePdf(theFile) 
    }) 
} 

# Sample pdf-generating function: 
makePdf <- function(filename){ 
    pdf(file = filename) 
    plot(cars) 
    dev.off() 
} 

shinyApp(ui = ui, server = server) 
İlgili konular