基础知识:
像素数:图片上的最小成像单位的数目。评价一个设备的像素值,一般使用宽高像素的乘积表示,如1000px * 1500 px
尺寸:指图像打印后的物理尺寸,一般用厘米/米或者(英)寸表示,1英寸(inche)=2.54厘米
分辨率:单位尺寸下像素点的数目,越高图像越细腻。计算方式为 分辨率=像素数/尺寸,是真正衡量设备清晰度的指标,单位一般为像素/英寸或者像素/厘米。
容量:图像文件的存贮空间,也就是文件的大小,一般以Kb和Mb来表示。
ps:平时所说的1200万像素的手机,是指该手机的像素数。
在R绘图中也需要调整最后图片的上述参数。一般我们使用一下函数对图片进行导出:
png() #支持调整px或inche等
tiff() #支持调整px或inche等
pdf() #仅支持调整inche等
ggsave() #支持ggplot对象的输出,支持调整 px/inche/dpi
graph2pdf() #export系列,这里同pdf()
这里也写了一个shiny程序用于手动导出png或pdf
message(.libPaths())
.libPaths("/home/whq/my_R_package")
library(shinydashboard)
library(shiny)
library(ggplot2)
library(dplyr)
library(pheatmap)
library(grid)
library(export)
library(ggplotify)
library(bs4Dash)
plot_dir<-list.files("plot.file/",pattern = "rds|RDS")
ui<-dashboardPage(
header = dashboardHeader(title = "Plot Output"),
sidebar = dashboardSidebar(
sidebarMenu(
menuItem("Plot png",tabName = "plot_png"),
menuItem("Plot pdf",tabName = "plot_pdf")
)
),
body = dashboardBody(
tabItems(
tabItem(tabName = "plot_png",
fluidRow(
wellPanel(
uiOutput("filename_png"),
numericInput("plot_width",label = "Width OF Plot(px)",min = 400,max = 2500,step = 100,value = 1000),
numericInput("plot_height",label = "Height OF Plot(px)",min = 400,max = 2500,step = 100,value = 1000),
numericInput("plot_resolution",label = "Resolution OF Plot(dpi)",min = 100,max = 1000,step = 100,value = 100),
textInput("download_png_name",label = "Name of Downloaded File",value = "download png"),
downloadButton("download_png")
)%>%column(3,.),
uiOutput("plot_output_ui")%>%box(collapsible = T,width = 12,maximizable = T)%>%column(9,.)
)
),
tabItem(tabName = "plot_pdf",
fluidRow(
wellPanel(
uiOutput("filename_pdf"),
numericInput("plot_width_pdf",label = "Width OF Plot(px)",min = 400,max = 2500,step = 100,value = 1000),
numericInput("plot_height_pdf",label = "Height OF Plot(px)",min = 400,max = 2500,step = 100,value = 1000),
numericInput("plot_resolution_pdf",label = "Resolution OF Plot(dpi)",min = 100,max = 1000,step = 100,value = 100),
textInput("download_pdf_name",label = "Name of Downloaded File",value = "download pdf"),
downloadButton("download_pdf")
)%>%column(3,.),
uiOutput("plot_output_ui_pdf")%>%box(collapsible = T,width = 12,maximizable = T)%>%column(9,.)
)
)
)
)
)
server<-function(input,output){
#png
if(T){
output$filename_png<-renderUI(
{
selectInput("filename_input_png","Select a plot",choices = structure(paste("plot.file/",plot_dir,sep = ""),names=plot_dir))
}
)
plot_input_png=reactive({
a=readRDS(input$filename_input_png)
if(class(a)=="pheatmap"){
a=as.ggplot(a)
}
return(a)
})
output$plot_output_ui<-renderUI(
{
plotOutput("plot_output",width = input$plot_width,height = input$plot_height)
}
)
#细节操作,在renderplot中正常情况下无法使用reactive的值,因此想办法构建一个reactive将renderplot再包住
observeEvent(input$plot_resolution,{
output$plot_output<-renderPlot(
{
plot_input_png()
},res =input$plot_resolution
)
})
output$download_png<-downloadHandler(
filename = paste(input$download_png_name,".png",sep = ""),
content = function(file){
ggsave(file,plot_input_png(),width = input$plot_width,height = input$plot_height,units = "px",device = "png",dpi = 100)
}
)
}
#pdf
if(T){
output$filename_pdf<-renderUI(
{
selectInput("filename_input_pdf","Select a plot",choices = structure(paste("plot.file/",plot_dir,sep = ""),names=plot_dir))
}
)
plot_input_pdf=reactive({readRDS(input$filename_input_pdf)})
output$plot_output_ui_pdf<-renderUI(
{
plotOutput("plot_output_pdf",width = input$plot_width_pdf,height = input$plot_height_pdf)
}
)
#细节操作,在renderplot中正常情况下无法使用reactive的值,因此想办法构建一个reactive将renderplot再包住
observeEvent(input$plot_resolution_pdf,{
output$plot_output_pdf<-renderPlot(
{
plot_input_pdf()
},res =input$plot_resolution_pdf
)
})
#以300dpi换算,分辨率=像素/尺寸
output$download_pdf<-downloadHandler(
filename = function(){
paste(input$download_pdf_name,"-",round(input$plot_width_pdf,digits = 2),"X",round(input$plot_height_pdf,digits = 2),"_px",".pdf",sep = "")
},
content = function(file){
ggsave(file,plot_input_pdf(),width =input$plot_width_pdf,height = input$plot_height_pdf,device = "pdf",units = "px",dpi = 100 )
}
)
}
}
shinyApp(ui,server)

网友评论