闪亮的下载图 - 只获取 htm 文件(没有 png)

Shiny Download Plot - only getting htm files (no png)

对于我的生活,我无法弄清楚发生了什么。我想在我的应用程序中包含一个用于绘图(png 文件)的下载按钮。我有以下代码,当我按下下载按钮时,会弹出以下内容 "download.htm"。任何人都可以看到错误在哪里:

ui.R 与下载按钮的部分:

tabPanel("BOOKINGS", 
br(), br(),
fluidRow(column(12, "BOOKINGS",
tabsetPanel(
tabPanel("Plot", plotOutput("mcsoPlot")),
tabPanel("Table", dataTableOutput("BOOKINGS")), 
br(),
downloadButton(outputId = "down", label = "Download the plot")

server.R 与图表和下载按钮相关的部分:

buildplot <- function(){
p <- ggplot(selectedData(), aes(x = MONTH, group = TYPE, color = TYPE)) +
 geom_line(stat = "count", size = 1.5)  
p
}

output$down <- downloadHandler(
filename =  "Shinyplot.png",

content = function(file) {
png(file) # open the png device
buildplot()
dev.off()  # turn the device off

} 
)

如果有人能看出我做错了什么,我将不胜感激。谢谢

我想通了并想 post 答案,因为我没有 运行 在其他 post 中解决这个问题,有人可能会犯同样的错误。

在 ui.R 脚本中,downloadbutton 脚本不能位于与绘图相同的 fluidRow 中。我基本上创建了另一个 fluidRow 并将下载按钮放在那里。最终 ui.R 示例为:

fluidRow(column(12, "BOOKINGS",
tabsetPanel(
tabPanel("Plot", plotOutput("mcsoPlot")),
tabPanel("Table", dataTableOutput("BOOKINGS")), 
br(),
fluidRow(column(12, "",
downloadButton(outputId = "down", label = "Download the plot")))
))))