gpt4 book ai didi

r - 无法从 Shiny 下载 .png 文件

转载 作者:行者123 更新时间:2023-12-04 09:45:15 25 4
gpt4 key购买 nike

我正在尝试为我的直方图创建一个下载按钮。我可以看到下载按钮,但似乎无法正确下载。它总是下载 .htm 文件而不是 .png 文件。我在 server.R 中使用 ggsave 命令。我尝试使用更传统的方法,但这似乎也不起作用。

library(shiny)
library(plyr)
library(ggplot2)

shinyUI(fluidPage(
headerPanel(title = "Test"),
sidebarLayout(
sidebarPanel(
sliderInput("input.a", "A", min = 0, max = 100, value = 50),
sliderInput("input.b", "B", min = 0, max = 100, value = 50),
sliderInput("input.c", "C", min = 0, max = 100, value = 50)
),

mainPanel(

tabsetPanel( type = "tabs", #Open panel
tabPanel("Distributions",plotOutput("hist.plot")),
downloadButton('downloadhist', 'Download Plot'))

) # close mainPanel
) # close sidebarLayout
) # close fluidPage
) # close shinyUI

shinyServer(function(input,output){ # open shiny server

#output$hist.plot = renderPlot({ # open renderPlot
make.hist.plot = reactive({

a = runif(1000,1,(input$input.a))
b = runif(1000,1,(input$input.b))
c = runif(1000,1,(input$input.c))

amount = c(a,b,c)

cat = c(rep("a",1000), rep("b",1000), rep("c",1000))

hist.data = data.frame(amount,cat)
names(hist.data) = c("amount","cat")

hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount),
plus.stdev = mean(amount) + sd(amount))
pricedata = pricedata[order(pricedata$avg),]


ggplot(hist.data, aes(x=amount, fill = cat))+
geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
theme_test()+
geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+


geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


facet_grid(cat ~., scales = "free")+
scale_y_continuous(expand = c(0,0),name = "Count")+
scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))

}) #close renderPlot

output$hist.plot = renderPlot({
print(make.hist.plot())
})

output$downloadhist = downloadHandler(
filename = function() { "hist.png" },
content = function(file) {
ggsave(file, plot = make.hist.plot(), device = "png")}
)

}) # close shinyServer



最佳答案

这似乎是一个范围问题。如果您将下载按钮放在侧边栏面板中,如果您删除 tabsetPanel,您的代码确实有效。 ,或者如果你把下载按钮放在同一个 tabPanel .最后的解决方案如下所示:

mainPanel(
tabsetPanel( type = "tabs", #Open panel
tabPanel("Distributions",
plotOutput("hist.plot"),
downloadButton('downloadhist', 'Download Plot')
)
)
) # close mainPanel

关于r - 无法从 Shiny 下载 .png 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62154635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com