单击操作按钮并将ggplot另存为pdf
按下downloadButton后,我试图下载/保存动态输出ggplot作为pdf 我已经尝试了多种方法从网上的多个来源做到这一点,但每次都不成功。我附上了相关的代码,希望有人能帮助我创建适当的函数下载图单击操作按钮并将ggplot另存为pdf,pdf,ggplot2,shiny,download,save,Pdf,Ggplot2,Shiny,Download,Save,按下downloadButton后,我试图下载/保存动态输出ggplot作为pdf 我已经尝试了多种方法从网上的多个来源做到这一点,但每次都不成功。我附上了相关的代码,希望有人能帮助我创建适当的函数下载图 ui <- ...... . . downloadButton('downloadPlot','Download Image'), . . server <- function(input,output,session){
ui <- ......
.
.
downloadButton('downloadPlot','Download Image'),
.
.
server <- function(input,output,session){
.
.
# DP Box Plots
output$one <- renderPlot({
if(input$layer == "State Specific"){
validate(
need(input$ES, "Please select a state to access the box plots."))
}
else if(input$layer == "Niche Specific"){
validate(
need(input$niche, "Please select a niche to access the box plot.")
)
}
else if(input$layer == "State/Niche Specific"){
validate(
need(input$SoN, "Please select a state. ")
)
validate(
need(input$NoS, "Please select a niche. ")
)
}
else if(input$layer == "New/Renewal Specific"){
validate(
need(input$NR, "Please select new or renewal to view box plots.")
)
}
ggplot(Dataset(), aes(x= reorder(Niche_Groups,
Discretionary_Price_Relativity, FUN = median), y =
Discretionary_Price_Relativity)) + geom_boxplot() + coord_flip() +
ggtitle(paste("Discretionary Pricing - ",titlename())) + theme(plot.title =
element_text(hjust = 0.5)) +
ylab("Ratio to Technical") + xlab("Niche Groups")
})
非常感谢,成功了!当我还在研究它时,有没有办法控制它在同一个downloadHandler函数的pdf中的显示比例?@Monty PYTHON See?ggsave。你可以给出一个宽度和一个高度。除了改变高度,我还可以通过使用scale=让它工作。非常感谢。
server <- function(input,output,session){
.
.
theplot <- reactive({
if(input$layer == "State Specific"){
validate(
need(input$ES, "Please select a state to access the box plots."))
}
else if(input$layer == "Niche Specific"){
validate(
need(input$niche, "Please select a niche to access the box plot.")
)
}
else if(input$layer == "State/Niche Specific"){
validate(
need(input$SoN, "Please select a state. ")
)
validate(
need(input$NoS, "Please select a niche. ")
)
}
else if(input$layer == "New/Renewal Specific"){
validate(
need(input$NR, "Please select new or renewal to view box plots.")
)
}
ggplot(Dataset(), aes(x= reorder(Niche_Groups,
Discretionary_Price_Relativity, FUN = median), y =
Discretionary_Price_Relativity)) + geom_boxplot() + coord_flip() +
ggtitle(paste("Discretionary Pricing - ",titlename())) + theme(plot.title =
element_text(hjust = 0.5)) +
ylab("Ratio to Technical") + xlab("Niche Groups")
})
output$one <- renderPlot({
theplot()
})
output$downloadPlot <- downloadHandler(
filename = "theplot.pdf",
content = function(file){
ggsave(file, theplot())
}
)