gpt4 book ai didi

r - 如何调用由 downloadHandler 中的 react 函数产生的图

转载 作者:行者123 更新时间:2023-12-04 02:40:45 24 4
gpt4 key购买 nike

如何调用在 downloadHandler 中使用 react 函数创建的图,而无需重新定义它?

非工作示例:

# Part of server.R

output$tgPlot <- renderPlot({
plot1 <-ggplot(iris[iris$Species==input$species,])+geom_point(aes(Sepal.Length ,Sepal.Width))
print(plot1)

} )



output$plotsave <- downloadHandler(
filename = 'plot.pdf',
content = function(file){
pdf(file = file, width=12, height=4)
tgPlot()
dev.off()
}
)

为什么不能在 downloadHandler 中调用 tgPlot()?还有别的办法吗?

最佳答案

tgPlot() 是在别处定义的函数吗?我看不到你曾经定义过它。

您可能希望在您从两个函数引用的常规(非 react 性)函数中定义您的绘图代码,a la:

tgPlot <- function(inputSpecies){
plot1 <-ggplot(iris[iris$Species==inputSpecies,])+geom_point(aes(Sepal.Length ,Sepal.Width))
print(plot1)
}

output$tgPlot <- renderPlot({
tgPlot(input$species)
})

output$plotsave <- downloadHandler(
filename = 'plot.pdf',
content = function(file){
pdf(file = file, width=12, height=4)
tgPlot(input$species)
dev.off()
}
)

这为您提供了一个可以生成绘图的函数。然后可以在响应式 renderPlot 上下文中引用此函数以生成响应式绘图或生成 PDF。

关于r - 如何调用由 downloadHandler 中的 react 函数产生的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318755/

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