为什么函数downloadHandler不在Rshiny中下载文件?

时间:2018-05-06 14:27:02

标签: r shiny

我的ur.R文件如下:

library(shiny)


shinyUI(
  fluidPage(
    titlePanel(title = h4('Demonstraion of renderplot', align='center')),
    sidebarLayout(
      sidebarPanel(
        selectInput('var', "Select the Variable", choices = c('Sepal.Length' =1 , 'sepal width' = 2, 'Petal Length' = 3 , 'Petal Width' = 4), selected = 1),
        br(),
        sliderInput('bins', 'Select the number of bins', min = 5, max = 25, value = 15),
        br(),
        radioButtons('color', 'Color of the bins', choices = c('Green', 'Red', 'Blue'), selected = 'Green'),
        br(),
        radioButtons('type', 'Choose the type', choices = list('png', 'pdf'), selected = 'png')

      ),
    mainPanel(
      plotOutput("myhist"),
      downloadButton('down', 'Download the Plot')


    )
    )
  )
)

和我的server.R如下:

library(shiny)

shinyServer(function(input, output){

  colm = reactive({
    as.numeric(input$var)
  }) 
  output$myhist = renderPlot(
    {
      hist(iris[,colm()], breaks = seq(0,max(iris[,colm()], l= input$bins+1)),col =input$color, main ="Histogram of irish dataset", xlab = names(iris[colm()]))
    }
  )

  output$down <- downloadHandler(
    filename =  function() {
      paste("iris", input$var3, sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {
      if(input$var3 == "png")
        png(file) # open the png device
      else
        pdf(file) # open the pdf device
      hist(colm()) # draw the plot
      dev.off()  # turn the device off

    } 
  )



}) 

image

当我点击下载按钮时,它显示如下,而文件名假设为iris.png。为什么会这样? image

我还尝试按照Abinav在他对此视频的评论中所建议的那样包装函数downdloadHandler的参数。

output$down <- downloadHandler({
    filename =  function() {
      paste("iris", input$var3, sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file) {
      if(input$var3 == "png")
        png(file) # open the png device
      else
        pdf(file) # open the pdf device
      hist(colm()) # draw the plot
      dev.off()  # turn the device off

    } 
  })

但是这给出了如下错误信息。

Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) : 
  G:\R_workshop_related_NITTE\Shiny\downloadPlots/server.R:17:6: unexpected ','
16:       paste("iris", input$var3, sep=".")
17:     },
         ^
Warning: Error in sourceUTF8: Error sourcing C:\Users\Mahe\AppData\Local\Temp\RtmpIPEtpl\file29805c1e4eca
Stack trace (innermost first):
    1: runApp
Error in sourceUTF8(serverR, envir = new.env(parent = globalenv())) : 
  Error sourcing C:\Users\Mahe\AppData\Local\Temp\RtmpIPEtpl\file29805c1e4eca

我在Windows机器Rstudio版本1.1.442与R 3.4.4。

1 个答案:

答案 0 :(得分:1)

问题是您没有名为var3的输入元素,因此input$var3返回NULL因此文件名无效("iris."

您将输入命名为type,因此请使用input$type

output$down <- downloadHandler(
  filename =  function() {
    paste("iris", input$type, sep=".")
  },
  # content is a function with argument file. content writes the plot to the device
  content = function(file) {
    if(input$type == "png")
      png(file) # open the png device
    else
      pdf(file) # open the pdf device
    hist(colm()) # draw the plot
    dev.off()  # turn the device off

  }
)

(请注意,您正在保存hist(colm()),这与您渲染的情节不同)

  

当我点击下载按钮时,它显示如下,而文件名假设为iris.png。为什么会这样?

在RStudio Viewer中:

RStudio Viewer未使用filename=function()的结果来保存文件。这是很久以来的情况。如果你给它命名,你可以下载该文件,它会起作用。

在浏览器中:

filename=function()的结果可以正常运作。