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()
})
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.