保存dotchart与闪亮

时间:2017-11-13 22:38:55

标签: r shiny

你知道这里有什么错吗? ggsave有效,但png(file=file)没有 - 它会保存空白图像。

output$savePlotAmount <- downloadHandler(
    filename = "amount.png",
    content = function(file) {
      if(input$plotType == "dot"){
        png(file = file)
        plotAmount()
        dev.off()
      }else{
        ggsave(plotAmount(), filename = file)  
      }

    })

我花了#34小时&#34;试图修复它,但我不知道发生了什么。很抱歉,该示例不可重现,但很难重现所有应用。

编辑: 什么是plotAmount()

plotAmount <- reactive({
    if(input$plotType == "violin") {
      plotAmount <- ggplot(values$x, aes_string(x = input$groupedBy, y = input$yVariableContinous)) + 
        geom_violin() +
        ggtitle(paste0(input$yVariableContinous, " grouped by ", input$groupedBy)) + 
        scale_y_continuous(limits = c(0, quantile(values$x[,input$yVariableContinous] , 0.95, na.rm = T)))
    }
    if(input$plotType == "boxplot") {
      plotAmount <- ggplot(values$x, aes_string(x = input$groupedBy, y = input$yVariableContinous)) + 
        geom_boxplot(outlier.shape = NA) +
        ggtitle(paste0(input$yVariableContinous, " grouped by ", input$groupedBy)) + 
        scale_y_continuous(limits = c(0, quantile(values$x[,input$yVariableContinous] , 0.95, na.rm = T)))
    }
    if(input$plotType == "dot") {
      var <- ifelse(input$groupedBy == 1, input$xVariable, input$groupedBy) 
      agregat <- aggregate(x = values$x[,input$yVariableContinous], by = list(g = values$x[,var], xx = values$x[,input$xVariable]), FUN = input$valueAs)
      dotchart(agregat$x, labels = agregat$xx, 
               groups = as.factor(agregat$g), 
               color = brewer.pal(9,"Set1")[as.numeric(as.factor(agregat$g))],
               xlab = "salary",
               cex = .75,
               main = paste0(input$yVariableContinous, " for ", input$xVariable,
                             "\ngrouped by ", input$groupedBy),
               xlim = c(min(values$x[,input$yVariableContinous], na.rm = T), quantile(values$x[,input$yVariableContinous] , 0.95, na.rm = T)),
               pch = 16
      )
    }

1 个答案:

答案 0 :(得分:0)

ggplot2图需要print()才能呈现。