在R Shiny应用程序中下载ggplot2和绘图对象 [英] Downloading a ggplot2 and a plotly object in an R Shiny app
问题描述
此问题是对这篇文章。
我有一个 R
闪亮的应用
同时使用 R
的 plotly
和 ggplot2
产生和显示图形。
I have an R
shiny app
that uses both R
's plotly
and ggplot2
to produce and display figures.
因为以闪亮
渲染 plotly
图形需要 plotly
的 renderPlotly
函数
我必须定义两个 render
函数,一个用于 plotly
,另一个用于 ggplot2
。
Because rendering plotly
figures in shiny
requires plotly
's renderPlotly
function
I have to define two render
functions, one for plotly
and the other for ggplot2
.
我的问题是如何定义 downloadHandler
来保存 plotly
使用 htmlwidgets :: saveWidget
作为html的对象,以及 ggplot2
使用 ggplot2作为pdf的对象: :ggsave
。
My question here is how to define a downloadHandler
that will save the plotly
objects as html using htmlwidgets::saveWidget
and the ggplot2
objects as pdf using ggplot2::ggsave
.
以下是示例数据:
set.seed(1)
meta.df <- data.frame(cell = c(paste0("c_",1:1000,"_1w"), paste0("c_",1:1000,"_2w"), paste0("c_",1:1000,"_3w")),
cluster = c(sample(c("cl1","cl2","cl3"),1000,replace=T)),
age = c(rep(1,1000),rep(2,1000),rep(3,1000)),
x = rnorm(3000), y = rnorm(3000))
expression.mat <- cbind(matrix(rnorm(20*1000,1,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[1:1000])),
matrix(rnorm(20*1000,2,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[1001:2000])),
matrix(rnorm(20*1000,3,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[2001:3000])))
这是到目前为止我拥有的应用代码:
Here's the app code I have so far:
server <- function(input, output, session)
{
output$gene <- renderUI({
selectInput("gene", "Select Gene to Display", choices = rownames(expression.mat))
})
output$group <- renderUI({
if(input$plotType == "Distribution Plot"){
selectInput("group", "Select Group", choices = c("cluster","age"))
}
})
scatter.plot <- reactive({
scatter.plot <- NULL
if(!is.null(input$gene)){
gene.idx <- which(rownames(expression.mat) == input$gene)
plot.df <- suppressWarnings(meta.df %>% dplyr::left_join(data.frame(cell=colnames(expression.mat),value=expression.mat[gene.idx,]),by=c("cell"="cell")))
scatter.plot <- suppressWarnings(plotly::plot_ly(marker=list(size=3),type='scatter',mode="markers",color=plot.df$value,x=plot.df$x,y=plot.df$y,showlegend=F,colors=colorRamp(c("lightgray","darkred"))) %>%
plotly::layout(title=input$gene,xaxis=list(zeroline=F,showticklabels=F,showgrid=F),yaxis=list(zeroline=F,showticklabels=F,showgrid=F)) %>%
plotly::colorbar(limits=c(min(plot.df$value,na.rm=T),max(plot.df$value,na.rm=T)),len=0.4,title="Scaled Expression"))
}
return(scatter.plot)
})
distribution.plot <- reactive({
distribution.plot <- NULL
if(!is.null(input$gene) & !is.null(input$group)){
gene.idx <- which(rownames(expression.mat) == input$gene)
plot.df <- suppressWarnings(meta.df %>% dplyr::left_join(data.frame(cell=colnames(expression.mat),value=expression.mat[gene.idx,]),by=c("cell"="cell")))
if(input$group == "cluster"){
distribution.plot <- suppressWarnings(plotly::plot_ly(x=plot.df$cluster,y=plot.df$value,split=plot.df$cluster,type='violin',box=list(visible=T),points=T,color=plot.df$cluster,showlegend=F) %>%
plotly::layout(title=input$gene,xaxis=list(title=input$group,zeroline=F),yaxis=list(title="Scaled Expression",zeroline=F)))
} else{
plot.df <- plot.df %>% dplyr::mutate(time=age) %>% dplyr::arrange(time)
plot.df$age <- factor(plot.df$age,levels=unique(plot.df$age))
distribution.plot <- suppressWarnings(ggplot(plot.df,aes(x=time,y=value)) +
geom_violin(aes(fill=age,color=age),alpha=0.3) +
geom_boxplot(width=0.1,aes(color=age),fill=NA) +
geom_smooth(mapping=aes(x=time,y=value,group=cluster),color="black",method='lm',size=1,se=T) +
stat_poly_eq(mapping=aes(x=time,y=value,group=cluster,label=stat(p.value.label)),formula=y~x,parse=T,npcx="center",npcy="bottom") +
scale_x_discrete(name=NULL,labels=levels(plot.df$cluster),breaks=unique(plot.df$time)) +
facet_wrap(~cluster) + theme_minimal() + ylab(paste0("#",input$gene," Scaled Expressioh"))+theme(legend.title=element_blank()))
}
}
return(distribution.plot)
})
output$out.plot_plotly <- plotly::renderPlotly({
if(input$plotType == "Scatter Plot"){
scatter.plot()
} else {
req(input$group)
if (input$plotType == "Distribution Plot" && input$group != "age"){
distribution.plot()
}
}
})
output$out.plot_plot <- renderPlot({
req(input$group)
if (input$plotType == "Distribution Plot" && input$group == "age") {
distribution.plot()
}
})
observeEvent(c(input$group, input$plotType), {
req(input$group)
if (input$group == "age" && input$plotType == "Distribution Plot") {
hide("out.plot_plotly")
show("out.plot_plot")
} else {
hide("out.plot_plot")
show("out.plot_plotly")
}
})
output$saveFigure <- downloadHandler(
if (input$group == "age" && input$plotType == "Distribution Plot") {
filename = function() {
paste0(input$plotType,".pdf")
}
} else{
filename = function() {
paste0(input$plotType,".html")
}
},
content = function(file) {
if(input$plotType == "Scatter Plot"){
htmlwidgets::saveWidget(scatter.plot(),file=file)
} else if(input$plotType == "Distribution Plot" && input$group != "age"){
htmlwidgets::saveWidget(distribution.plot(),file=file)
} else{
ggsave(distribution.plot(),filename=file)
}
}
)
}
ui <- fluidPage(
titlePanel("Explorer"),
useShinyjs(),
sidebarLayout(
sidebarPanel(
tags$head(
tags$style(HTML(".multicol {-webkit-column-count: 3; /* Chrome, Safari, Opera */-moz-column-count: 3; /* Firefox */column-count: 3;}")),
tags$style(type="text/css", "#loadmessage {position: fixed;top: 0px;left: 0px;width: 100%;padding: 5px 0px 5px 0px;text-align: center;font-weight: bold;font-size: 100%;color: #000000;background-color: #CCFF66;z-index: 105;}"),
tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }")),
conditionalPanel(condition="$('html').hasClass('shiny-busy')",tags$div("In Progress...",id="loadmessage")),
selectInput("plotType", "Plot Type", choices = c("Scatter Plot","Distribution Plot")),
uiOutput("gene"),
uiOutput("group"),
downloadButton('saveFigure', 'Save figure')
),
mainPanel(
plotly::plotlyOutput("out.plot_plotly"),
plotOutput("out.plot_plot")
)
)
)
shinyApp(ui = ui, server = server)
plotly
对象确实保存为html,但对于 input $ plotType ==分布图; && input $ group == age
选项,尽管 Save图形
按钮确实弹出了保存对话框,但图形未下载并保存。
The plotly
objects do get saved as html, but for the input$plotType == "Distribution Plot" && input$group == "age"
option, although the Save figure
button does pop up the save dialog box, the figure does not get downloaded and saved.
有什么想法吗?
推荐答案
我不得不更改2件事:
- 向
ggsave
通话中添加设备
(请参见@YBS链接的答案,谢谢!) - 将文件名的逻辑放入函数中,而不是根据图定义不同的函数
- add a
device
to theggsave
call (see the answers linked by @YBS, thanks!) - put the logic for the filename into the function instead of defining different functions based on the plot
library(shiny)
library(dplyr)
library(ggplot2)
library(ggpmisc)
library(shinyjs)
set.seed(1)
meta.df <- data.frame(cell = c(paste0("c_",1:1000,"_1w"), paste0("c_",1:1000,"_2w"), paste0("c_",1:1000,"_3w")),
cluster = c(sample(c("cl1","cl2","cl3"),1000,replace=T)),
age = c(rep(1,1000),rep(2,1000),rep(3,1000)),
x = rnorm(3000), y = rnorm(3000))
expression.mat <- cbind(matrix(rnorm(20*1000,1,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[1:1000])),
matrix(rnorm(20*1000,2,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[1001:2000])),
matrix(rnorm(20*1000,3,1), nrow=20, ncol=1000, dimnames=list(paste0("g",1:20),meta.df$cell[2001:3000])))
server <- function(input, output, session)
{
output$gene <- renderUI({
selectInput("gene", "Select Gene to Display", choices = rownames(expression.mat))
})
output$group <- renderUI({
if(input$plotType == "Distribution Plot"){
selectInput("group", "Select Group", choices = c("cluster","age"))
}
})
scatter.plot <- reactive({
scatter.plot <- NULL
if(!is.null(input$gene)){
gene.idx <- which(rownames(expression.mat) == input$gene)
plot.df <- suppressWarnings(meta.df %>% dplyr::left_join(data.frame(cell=colnames(expression.mat),value=expression.mat[gene.idx,]),by=c("cell"="cell")))
scatter.plot <- suppressWarnings(plotly::plot_ly(marker=list(size=3),type='scatter',mode="markers",color=plot.df$value,x=plot.df$x,y=plot.df$y,showlegend=F,colors=colorRamp(c("lightgray","darkred"))) %>%
plotly::layout(title=input$gene,xaxis=list(zeroline=F,showticklabels=F,showgrid=F),yaxis=list(zeroline=F,showticklabels=F,showgrid=F)) %>%
plotly::colorbar(limits=c(min(plot.df$value,na.rm=T),max(plot.df$value,na.rm=T)),len=0.4,title="Scaled Expression"))
}
return(scatter.plot)
})
distribution.plot <- reactive({
distribution.plot <- NULL
if(!is.null(input$gene) & !is.null(input$group)){
gene.idx <- which(rownames(expression.mat) == input$gene)
plot.df <- suppressWarnings(meta.df %>% dplyr::left_join(data.frame(cell=colnames(expression.mat),value=expression.mat[gene.idx,]),by=c("cell"="cell")))
if(input$group == "cluster"){
distribution.plot <- suppressWarnings(plotly::plot_ly(x=plot.df$cluster,y=plot.df$value,split=plot.df$cluster,type='violin',box=list(visible=T),points=T,color=plot.df$cluster,showlegend=F) %>%
plotly::layout(title=input$gene,xaxis=list(title=input$group,zeroline=F),yaxis=list(title="Scaled Expression",zeroline=F)))
} else{
plot.df <- plot.df %>% dplyr::mutate(time=age) %>% dplyr::arrange(time)
plot.df$age <- factor(plot.df$age,levels=unique(plot.df$age))
distribution.plot <- suppressWarnings(ggplot(plot.df,aes(x=time,y=value)) +
geom_violin(aes(fill=age,color=age),alpha=0.3) +
geom_boxplot(width=0.1,aes(color=age),fill=NA) +
geom_smooth(mapping=aes(x=time,y=value,group=cluster),color="black",method='lm',size=1,se=T) +
stat_poly_eq(mapping=aes(x=time,y=value,group=cluster,label=stat(p.value.label)),formula=y~x,parse=T,npcx="center",npcy="bottom") +
scale_x_discrete(name=NULL,labels=levels(plot.df$cluster),breaks=unique(plot.df$time)) +
facet_wrap(~cluster) + theme_minimal() + ylab(paste0("#",input$gene," Scaled Expressioh"))+theme(legend.title=element_blank()))
}
}
return(distribution.plot)
})
output$out.plot_plotly <- plotly::renderPlotly({
if(input$plotType == "Scatter Plot"){
scatter.plot()
} else {
req(input$group)
if (input$plotType == "Distribution Plot" && input$group != "age"){
distribution.plot()
}
}
})
output$out.plot_plot <- renderPlot({
req(input$group)
if (input$plotType == "Distribution Plot" && input$group == "age") {
distribution.plot()
}
})
observeEvent(c(input$group, input$plotType), {
req(input$group)
if (input$group == "age" && input$plotType == "Distribution Plot") {
hide("out.plot_plotly")
show("out.plot_plot")
} else {
hide("out.plot_plot")
show("out.plot_plotly")
}
})
output$saveFigure <- downloadHandler(
filename = function() {
if (input$group == "age" && input$plotType == "Distribution Plot") {
paste0(input$plotType,".pdf")
} else{
paste0(input$plotType,".html")
}
},
content = function(file) {
if(input$plotType == "Scatter Plot"){
htmlwidgets::saveWidget(scatter.plot(),file=file)
} else if(input$plotType == "Distribution Plot" && input$group != "age"){
htmlwidgets::saveWidget(distribution.plot(),file=file)
} else{
ggsave(filename = file,
plot = distribution.plot(),
device = "pdf")
}
}
)
}
ui <- fluidPage(
titlePanel("Explorer"),
useShinyjs(),
sidebarLayout(
sidebarPanel(
tags$head(
tags$style(HTML(".multicol {-webkit-column-count: 3; /* Chrome, Safari, Opera */-moz-column-count: 3; /* Firefox */column-count: 3;}")),
tags$style(type="text/css", "#loadmessage {position: fixed;top: 0px;left: 0px;width: 100%;padding: 5px 0px 5px 0px;text-align: center;font-weight: bold;font-size: 100%;color: #000000;background-color: #CCFF66;z-index: 105;}"),
tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }")),
conditionalPanel(condition="$('html').hasClass('shiny-busy')",tags$div("In Progress...",id="loadmessage")),
selectInput("plotType", "Plot Type", choices = c("Scatter Plot","Distribution Plot")),
uiOutput("gene"),
uiOutput("group"),
downloadButton('saveFigure', 'Save figure')
),
mainPanel(
plotly::plotlyOutput("out.plot_plotly"),
plotOutput("out.plot_plot")
)
)
)
shinyApp(ui = ui, server = server)
这篇关于在R Shiny应用程序中下载ggplot2和绘图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!