Help needed downloading pdf of corrplot output

I am a novice and can't get my plot to save. I am using corrplot to generate the figure and it is rendered perfectly in the one panel - but I cannot work out how to then save it as a PDF. here is the server side code

server <- function(input, output) {
    
    output$shared <- renderTable({
        
        # input$file2 will be NULL initially. After the user selects
        # and uploads a file, head of that data file by default,
        # or all rows if selected, will be shown.
        
        req(input$file1)
        
        df <- read.csv(input$file1$datapath,
                       header = TRUE,
                       sep = ',' ,
                       quote = input$quote)
        
        if(input$disp == "head") {
            return(head(df))
        }
        else {
            return(df)
        }
        
    })
    

    output$corr_plot <- renderPlot({
        req(input$file1)
                
        # take CSV file of OTU data and make PCoA class for plotting
        rawdata <- read.csv(input$file1$datapath,header=T,row.names=1)
        mat <- as.data.frame(rawdata)
       
         # create correlation matrix
        corr_mat <- corr.test(mat, 
                              use = "pairwise",
                              method=input$dist,
                              adjust=input$adjust_method,
                              alpha=.05,
                              ci=TRUE,
                              minlength=5)
        
        corr_r_values <- corr_mat$r
        corr_p_values <- corr_mat$p
        
        # set colour palette for heatmap
        col=brewer.pal(n=11, name="RdYlBu")
        
        #plot heatmap of correlation matrix
           cp <- corrplot(corr_r_values, 
                      method = input$shape,
                      col=rev(col),
                      type = input$layout,
                      hclust.method = input$hclust_method,
                      order = input$reordered,
                      p.mat = corr_p_values, 
                      sig.level = 0.05,
                      insig = "blank",
                      addgrid.col = "#D3D3D3", # adjust for grid colour
                      tl.cex= input$text_size, # adjust for text size
                      number.cex = input$number_size, #adjust for number in plot size
                      pch.cex = "0.5", # input$text_size,
                      tl.col = "black", # adjust for text colour
                      diag = FALSE,
                      title=" ")
             
             print(cp)
             
    })

    
    output$downloadPlot <- downloadHandler(
        filename = function(){paste(" ", '.pdf', sep = '')},
        
        content = function(file){
            pdf(file, width = 5, height = 5)
            print(cp)
            dev.off()
        })

At its most basic I think something like this should work

pdf("myfile")
 corrplot(corr_r_values)
dev.off

many thanks, but please treat me as a novice.

Where would I write that in the context of what I posted?

I am not sure of what some of your code is doing. It looks like you have some shiny code and I have never used snivy.

However, if I understand what you are doing you can do this any time after you calculate corr_r_values.

Ideally I think what you want is:


pdf("myfile.pdf")
corrplot(corr_r_values, 
                 method = input$shape,
                 col=rev(col),
                 type = input$layout,
                 hclust.method = input$hclust_method,
                 order = input$reordered,
                 p.mat = corr_p_values, 
                 sig.level = 0.05,
                 insig = "blank",
                 addgrid.col = "#D3D3D3", # adjust for grid colour
                 tl.cex= input$text_size, # adjust for text size
                 number.cex = input$number_size, #adjust for number in plot size
                 pch.cex = "0.5", # input$text_size,
                 tl.col = "black", # adjust for text colour
                 diag = FALSE,
                 title=" ")
dev.off()

You will need to set the path name and parameters in pdf() to your circumstances.

I think one of your problems is trying to save corrplot into cp. This works for the ggplot family of graphics but not usually for a lot of other graphics.

thanks John, but I really need a solution that works in a Shiny app - I can solve this for normal R studio, but it is not working in my shiny app

Sorry, I have never used shiny. Let's hope a shiny expert comes along soon.

thanks and really do appreciate you trying to help - cheers Julian

I tested this and it worked for me

library(shiny)
library(corrplot)
ui <- fluidPage(
  plotOutput("mycorplot"),
  downloadButton("dbtn")
)

server <- function(input, output, session) {
  plot_obj <- reactive({
    corrplot(cor(mtcars), method = "circle")
  })

  output$mycorplot <- renderPlot({
    req(plot_obj())
  })

  output$dbtn <- downloadHandler(
    filename = "my_corr_plot.pdf",
    content = function(file) {
      pdf(file)
      corrplot(plot_obj(), method = "circle")
      dev.off()
    }
  )
}

shinyApp(ui, server)

many thanks will give it try tomorrow - Julian

many thanks - that worked - with a few tweaks - but I really appreciate your time to help me - julian

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.