如何使用downloadHandler将ggplot下载为png?目前,我遇到了多个错误

时间:2020-04-20 14:00:03

标签: r ggplot2 shiny download

我在将ggplot下载为png文件时遇到问题。我的ggplot具有单击功能,因此,当您单击图中的图时,图的顺序会更改,因此我的renderPlot输出中具有if和else条件。下面是服务器代码的一部分:

output$plot1 <- renderPlot({   

    e <- as.character(input$AB1)
    k <- TCGA(e) #function for cancer data
    g <- MEANSD(e) #function for healthy data
    test <- rbind(k,g) #combine healthy with cancer data 
    test$Tissue <- as.factor(test$Tissue) #row names of graph as factor -- important to order the graph according to alphabet or value of expression

    if (plot_data$trigger %% 2 == 0) {
      gg1 <- ggplot(test, aes(x= test$Origin , y= as.numeric(test$r), fill = Can))+geom_bar(position = position_dodge(), stat="identity", colour = "grey") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle(as.character(input$AB1)) + labs(y = "Expression level in log2(tpm+0.001) scale", x = "Tissue type (alphabetical order)")
    gg1
      } else {
      ggplot(test, aes(x= reorder(test$Origin, -test$r, sum) , y= as.numeric(test$r), fill = Can))+geom_bar(position = position_dodge(), stat="identity", colour = "grey") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle(as.character(input$AB1)) +  labs(y = "Expression level in log2(tpm+0.001) scale", x = "Tissue type (expression level order)")

      }
     })

output$tumordown <- downloadHandler(
    filename = function () {
      paste(input$AB1, "png", sep = ".")
    },
    content = function (file) {

      ggsave(file, plot = gg1)

      dev.off()
    }
  )

上面的代码给我错误 在图像中保存5.76 x 4 警告:ggsave错误:找不到对象'gg1' [没有可用的堆栈跟踪]

ggsave(file, plot = ggplot(test, aes(x= test$Origin , y= as.numeric(test$r), fill = Can))+geom_bar(position = position_dodge(), stat="identity", colour = "grey") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle(as.character(input$AB1)) + labs(y = "Expression level in log2(tpm+0.001) scale", x = "Tissue type (alphabetical order)")

,这段代码给我:在图像中保存6.67 x 6.67 警告:错误:您正在将函数作为全局数据传递。 您是否在data中拼写了ggplot()自变量 [没有可用的堆栈跟踪]

我的第一个问题:有人知道如何解决此错误吗?我已经在该站点上签出了许多示例,但是仍然给我错误。 我的第二个问题:如果下载能够正常进行,是否可以将if和else条件图保存在一个png中?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

将ggplot图形放在无功导体中:

gg1 <- reactive({
  e <- as.character(input$AB1)
  k <- TCGA(e) #function for cancer data
  g <- MEANSD(e) #function for healthy data
  test <- rbind(k,g) #combine healthy with cancer data 
  test$Tissue <- as.factor(test$Tissue) #row names of graph as factor -- important to order the graph according to alphabet or value of expression
  if (plot_data$trigger %% 2 == 0) {
    gg <- ggplot(test, aes(x = test$Origin , y = as.numeric(test$r), fill = Can)) + 
      geom_bar(position = position_dodge(), stat="identity", colour = "grey") + 
      theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      ggtitle(as.character(input$AB1)) + 
      labs(y = "Expression level in log2(tpm+0.001) scale", 
           x = "Tissue type (alphabetical order)")
  } else {
    gg <- ggplot(test, 
                 aes(x = reorder(test$Origin, -test$r, sum), 
                     y = as.numeric(test$r), 
                     fill = Can)) + 
      geom_bar(position = position_dodge(), stat="identity", colour = "grey") + 
      theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
      ggtitle(as.character(input$AB1)) +
      labs(y = "Expression level in log2(tpm+0.001) scale", 
           x = "Tissue type (expression level order)")
  }
  gg
})

然后

output$plot1 <- renderPlot({ gg1() })

downloadHandler中:

ggsave(file, gg1())

并删除dev.off()