首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击操作按钮并将ggplot另存为pdf

单击操作按钮并将ggplot另存为pdf
EN

Stack Overflow用户
提问于 2019-06-20 23:44:26
回答 1查看 56关注 0票数 0

在推送downloadButton后,我尝试下载/保存动态输出的ggplot作为pdf。

我尝试了从多个来源在线尝试的各种方法,但每次都不成功。我附上了相关的代码,希望有人能帮助我创建适当的函数"downloadPlot“。

代码语言:javascript
复制
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")
   })
EN

回答 1

Stack Overflow用户

发布于 2019-06-21 00:04:05

代码语言:javascript
复制
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())
    }
  )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56689587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档