You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何R语言png()函数在本地正常运行但在HPC上失效?

问题:HPC集群中R脚本无法生成PNG图片,本地正常运行

本地运行可正常保存PNG,但提交到HPC后报错:

Error in png(file_name, width = 75000, height = 6500, type = "cairo",  :
  unable to start device 'png'
In addition: Warning message:
In png(file_name, width = 75000, height = 6500, type = "cairo",  :
  cairo error 'invalid value (typically too big) for the size of the input (surface, pattern, etc.)'
Execution halted
srun: error: fat-0002: task 0: Exited with exit code 1

本地系统内存更少但可正常运行,排除内存问题。以下是使用的脚本:

R脚本

library(tidyverse)
library(data.table)
library(dplyr)
library(stringr)
library(tidypaleo)


setwd("~/path-to-dir")

refsbcf <- fread("file.tbl")

chr_list <- unique(refsbcf$V2)

plot_list = list()

for (i in 1:length(chr_list)) {
  current = which(refsbcf$V2 %in% chr_list[i])
  refsbcf_current = refsbcf[current,]
  p1 = ggplot(data = subset(refsbcf_current), aes(x=as.factor(V3), y=V5)) +
    geom_col_segs(position = "stack",size = 1)+    
    scale_x_discrete() +
    scale_fill_manual()+
    theme_bw() +
    facet_grid(vars(V1), scales = "free") +
    labs(x = "position", y = "coverage")+
    theme(strip.text.x = element_text(size = 10, angle = 90, hjust = 0.5,vjust = 0.5),
          axis.title = element_text(size = 10),
          #hjust = 0, vjust = 0), 
          strip.background = element_blank(), 
          axis.text.y = element_text(size = 10),
          axis.text.x = element_text(size = 3, angle = 90, hjust = 0),
          plot.margin = unit(c(0, 0.5, 0, 0), "inches"),
          panel.border = element_rect(fill = NA, colour = "black"))
  plot_list[[i]] = p1
}

for (i in 1:length(chr_list)) {
  name = chr_list[i]
  file_name = paste("cov_TEST_",i, ".png", sep="")
  png(file_name, width = 75000, height = 6500, units = "px", pointsize = 10, res = 300)
  print(plot_list[[i]])
  dev.off()
}

批处理脚本

#SBATCH --job-name=R1
#SBATCH --partition=fat
#SBATCH --time=10:00:00
#SBATCH --qos=normal
#SBATCH --mail-type=END
#SBATCH --mem=300G

module load bio/R/4.0.0

srun Rscript script_visualization.R

module unload bio/R/4.0.0

问题排查与解决方案

核心原因

报错里的cairo错误明确指出:画布尺寸超出了HPC上cairo库的最大限制。本地系统的cairo环境可能没有严格的尺寸限制,或者版本不同允许更大的画布,而HPC集群的cairo编译配置或系统限制了绘图表面(surface)的最大尺寸。

具体解决方法

  • 大幅缩小画布尺寸:当前设置的width=75000px换算成英寸是250英寸(75000/300),远超常规可视化需求。建议改用英寸为单位设置合理尺寸,比如:
    png(file_name, width=20, height=5, units="in", res=300)
    
    这样生成的图片分辨率足够高(6000×1500px),同时不会触发尺寸限制。
  • 更换PNG设备类型:在png()函数中去掉type="cairo"参数,或者尝试type="Xlib",让R自动适配HPC上可用的绘图设备:
    png(file_name, width=20, height=5, units="in", res=300)
    # 或者
    png(file_name, width=20, height=5, units="in", res=300, type="Xlib")
    
  • 优化脚本内存使用:当前脚本先把所有绘图对象存入列表再批量保存,可改为生成一个图就保存一个,避免不必要的内存占用:
    # 去掉plot_list相关代码,合并循环
    for (i in 1:length(chr_list)) {
      current = which(refsbcf$V2 %in% chr_list[i])
      refsbcf_current = refsbcf[current,]
      p1 = ggplot(data = subset(refsbcf_current), aes(x=as.factor(V3), y=V5)) +
        geom_col_segs(position = "stack",size = 1)+    
        scale_x_discrete() +
        scale_fill_manual()+
        theme_bw() +
        facet_grid(vars(V1), scales = "free") +
        labs(x = "position", y = "coverage")+
        theme(strip.text.x = element_text(size = 10, angle = 90, hjust = 0.5,vjust = 0.5),
              axis.title = element_text(size = 10),
              strip.background = element_blank(), 
              axis.text.y = element_text(size = 10),
              axis.text.x = element_text(size = 3, angle = 90, hjust = 0),
              plot.margin = unit(c(0, 0.5, 0, 0), "inches"),
              panel.border = element_rect(fill = NA, colour = "black"))
      file_name = paste("cov_TEST_",i, ".png", sep="")
      png(file_name, width=20, height=5, units="in", res=300)
      print(p1)
      dev.off()
    }
    
  • 检查HPC的R依赖:尝试在批处理脚本中先加载cairo模块再加载R:
    module load cairo
    module load bio/R/4.0.0
    
    如果问题仍存在,联系集群管理员确认R版本的cairo编译配置是否有尺寸限制。

内容的提问来源于stack exchange,提问作者Ugur Ç.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 13:57:18