在Shiny中保存基本R图时找不到404

时间:2018-10-19 06:46:20

标签: r shiny

我正在制作一个Shiny应用程序,该应用程序涉及使用基本R图形的生物信息学软件包中的函数绘制图。我正在RStudio服务器中构建Shiny应用程序。由于只能使用该软件包中的函数来绘制图,因此不能选择使用ggplot2或highcharter。保存ggplot2生成的图没有问题,但是尝试保存使用基本R图形生成的图时遇到了麻烦。我在Downloadhander (save plot) for basic plot in shiny中使用了答案,但是当我单击下载按钮时,我得到了“ 404 not found”并且下载未启动,即使该图在Shiny应用程序中正确显示了。这是RStudio默认的忠实间歇泉应用的修改版本,可以重现此问题:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         downloadButton("download", "Download plot")
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   p <- reactive({
     # generate bins based on input$bins from ui.R
     x    <- faithful[, 2] 
     bins <- seq(min(x), max(x), length.out = input$bins + 1)

     # draw the histogram with the specified number of bins
     hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
   output$distPlot <- renderPlot(p())
   output$download <- downloadHandler("foo.png", 
                                      content = function(file) {
                                        png(file = file)
                                        p()
                                        dev.off()
                                      })
}

# Run the application 
shinyApp(ui = ui, server = server)

问题似乎与

有关
png(file = file)
p()
dev.off()

在Shiny内不起作用,因为当我尝试以这种方式保存ggplot时,它也给出了“ 404 not found”,而ggsave可以很好地在Shiny应用程序中下载ggplot(尽管不是基于R的图)。在Shiny之外,用于保存情节的基本R方法可以正常工作。

1 个答案:

答案 0 :(得分:1)

p()从反应式更改为标准功能对我来说解决了这个问题。

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            downloadButton("download", "Download plot")
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    p <- function() {
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    }
    output$distPlot <- renderPlot(p())
    output$download <- downloadHandler(filename = "foo.png", 
                                       content = function(file) {
                                           png(filename = file)
                                           p()
                                           dev.off()
                                       },
                                       contentType = "image/png")
}

# Run the application 
shinyApp(ui = ui, server = server)