2015-06-16 60 views
3

我有一個閃亮的功能,其中用戶選擇下載圖像類型(.png,.tiff等),並點擊一個按鈕下載它的一部分。但所有的選項下載.png格式,我似乎無法找出什麼是錯的。R閃亮下載不同的圖像格式

請注意,預覽總是png。下載功能在點擊按鈕時創建不同的文件類型。另一點需要注意的是在downloadhandler中使用file.copy()而不是像 png(name) plot() dev.off() 這是因爲我的繪圖函數很複雜,file.copy()是更實用。 代碼如下。

#ui.R ---------------------------------------------------------- 
shinyUI(fluidPage(

    titlePanel("Download test"), 

    sidebarLayout(
    sidebarPanel(
     numericInput("fheight", "Height (cm)", min=2, max=15, step=1, value = 10), 
     numericInput("fwidth", "Width (cm)", min=2, max=15, step=1, value = 10), 
     selectInput("fres", "Res", choices=c("100","200","300"), selected = "100"), 
     selectInput("fformat", "File type", choices=c("png","tiff","jpeg","pdf"), selected = "png", multiple = FALSE, selectize = TRUE), 
     downloadButton('bn_download', 'Download Plot') 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     imageOutput("plotoutput") 
    ) 
) 
)) 


# server.R ---------------------------------------------------------- 
shinyServer(function(input, output) { 

    # store some values 
    store <- reactiveValues(dname="AwesomeDownload") 

    # data creation 
    fn_data <- reactive({ 
    df <- data.frame(x=rnorm(50),y=rnorm(50)) 
    }) 

    # create filename 
    fn_downloadname <- reactive({ 

    if(input$fformat=="png") filename <- paste0(store$dname,".png",sep="") 
    if(input$fformat=="tiff") filename <- paste0(store$dname,".tif",sep="") 
    if(input$fformat=="jpeg") filename <- paste0(store$dname,".jpg",sep="") 
    if(input$fformat=="pdf") filename <- paste0(store$dname,".pdf",sep="") 
    return(filename) 
    }) 

    # render png preview 
    output$plotoutput <- renderImage({ 

    df <- fn_data() 
    fheight <- input$fheight 
    fwidth <- input$fwidth 
    fres <- as.numeric(input$fres) 

    png(paste0(store$dname,".png",sep=""), height=fheight, width=fwidth, res=fres, units="cm") 
    plot(df) 
    dev.off() 

    return(list(src = paste0(store$dname,".png",sep=""), 
       contentType = "image/png", 
       width = round((input$fwidth*as.numeric(input$fres))/2.54, 0), 
       height = round((input$fheight*as.numeric(input$fres))/2.54, 0), 
       alt = "plot")) 
    },deleteFile=TRUE) 

    # download function 
    fn_download <- function() 
    { 

    df <- fn_data() 
    fheight <- input$fheight 
    fwidth <- input$fwidth 
    fres <- as.numeric(input$fres) 

    if(input$fformat=="pdf") fheight <- round(fheight*0.3937,2) 
    if(input$fformat=="pdf") fwidth <- round(fwidth*0.3937,2) 

    if(input$fformat=="png") png(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm") 
    if(input$fformat=="tiff") tiff(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm",compression="lzw") 
    if(input$fformat=="jpeg") jpeg(fn_downloadname(), height=fheight, width=fwidth, res=fres, units="cm",quality=100) 
    if(input$fformat=="pdf") pdf(fn_downloadname(), height=fheight, width=fwidth) 
    plot(df) 
    dev.off() 
    } 

    # download handler 
    output$bn_download <- downloadHandler(
    filename = fn_downloadname(), 
    content = function(file) { 
     fn_download() 
     file.copy(fn_downloadname(), file, overwrite=T) 
    } 
) 
}) 
+0

這裏https://groups.google.com/forum/#一起來看看! topic/shiny-discuss/u7gwXc8_vyY –

回答

1

刪除下載文件名中的括號可修復問題。是的,甚至不要問。我不知道爲什麼這麼做。但它的工作。

鄭元暢的回答是:

改變這一行:

filename = fn_downloadname(), 

這樣:

filename = fn_downloadname, 
+1

參數文件名需要一個函數或字符串。但是一個reactive()對象不會簡單地返回字符串,所以您需要傳遞反應函數本身,而不使用括號。不知道確切的細節,但這是這種令人困惑的行爲背後的一般想法。 –