linux下不能画图的问题解决

在Linux环境中遇到R语言无法画图的问题,主要是由于缺少PNG设备支持。通过安装Cairo包并配置相关依赖,可以解决这个问题。首先安装Cairo包,然后检查R的图形支持,确认已支持Cairo生成图片。最后,使用Cairo系列函数如CairoPNG进行绘图,确保图形正确生成。若遇到png包安装失败,需要安装libpng-devel并通过R重新安装png包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【报错】:
> hist(clf2$school)
[rsession-enn_james] ERROR r error 4 (R code execution error) [errormsg=Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width,  :
  无法打开PNG设备
]; OCCURRED AT: core::Error r::exec::evaluateString(const std::string&, SEXPREC**, r::sexp::Protect*) /root/rstudio/src/cpp/r/RExec.cpp:271; LOGGED FROM: DevDesc* r::session::graphics::handler::shadow::<unnamed>::shadowDevDesc(DevDesc*) /root/rstudio/src/cpp/r/session/graphics/RShadowPngGraphicsHandler.cpp:141
Error in RStudioGD() :
  Shadow graphics device error: r error 4 (R code execution error)
此外: Warning message:
In grDevices:::png("/tmp/RtmpNwBswU/36f8ee1bfb64447a9d4cb058599b7cff.png",  :
  无法打开链结到X11显示''
而且输入capabilities函数可以看到不支持png,jpeg这些画图
> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp     sockets      libxml        fifo      cledit
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE        TRUE        TRUE        TRUE        TRUE        TRUE
      iconv         NLS     profmem       cairo         ICU long.double     libcurl
       TRUE        TRUE       FALSE       FALSE       FALSE        TRUE       FALSE

【解决办法】:不使用X11生成,使用 图形渲染库Cairo。

【步骤1】:安装Cairo包
install.packages("Cairo")

【步骤2】:加载Cairo后,查看支持
    
> library(Cairo)
> Cairo.capabilities()
   png   jpeg   tiff    pdf    svg     ps    x11    win raster
  TRUE  FALSE  FALSE   TRUE   TRUE   TRUE   TRUE  FALSE   TRUE
说明已经支持用cairo生成图片了

Cairo使用起来非常简单,和基础包grDevices中的函数对应。
CairoPNG: 对应grDevices:png()
CairoJPEG: 对应grDevices:jpeg()
CairoTIFF: 对应grDevices:tiff()
CairoSVG: 对应grDevices:svg()
CairoPDF: 对应grDevices:pdf()


【步骤3】:生成图片
>CairoPNG(file="out.png",width=800,height=480)
> hist(clf2$school)
> getwd()
[1] "/home/enn_james"
在当前目录下可以看到生成了图片

在Rstudio窗口下的右下侧的Files里面,找到刚生成的png文件,点击打开即可看到新生成的图片。







画图之前,先用

options(bitmapType='cairo')


if you are using R 3.0, try options(bitmapType='cairo')it worked for me

I placed it before png() call and once is enough for all png() calls.



Warning in grDevices:::png("/tmp/RtmpWTwF68/a0ee957a346f405dbc06815f37fd0971.png",  :
  本R版本不支持png
01 Jun 2015 00:55:03 [rsession-enn_james] ERROR r error 4 (R code execution error) [errormsg=Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width,  : 
  无法打开PNG设备
]; OCCURRED AT: core::Error r::exec::evaluateString(const std::string&, SEXPREC**, r::sexp::Protect*) /root/rstudio/src/cpp/r/RExec.cpp:271; LOGGED FROM: DevDesc* r::session::graphics::handler::shadow::<unnamed>::shadowDevDesc(DevDesc*) /root/rstudio/src/cpp/r/session/graphics/RShadowPngGraphicsHandler.cpp:141
Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)


> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua    http/ftp     sockets      libxml 
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE        TRUE        TRUE        TRUE 
       fifo      cledit       iconv         NLS     profmem       cairo         ICU long.double     libcurl 
       TRUE        TRUE        TRUE        TRUE       FALSE       FALSE       FALSE        TRUE       FALSE 
R软件不支持png格式的图片,用capabilities()函数打印一下,你的环境支持的图片格式。

capabilities() 正是表示你的 R 是否支持相应的特性。这个不能设置,它是依靠安装来解决的。我知道从源码编译是有选项可以将该特性加上的,但rpm包是否有支持就不确定了,你可能得自己找一下。



首先,退出R,然后安装一堆相关的包
$ sudo yum install libpng libpng-devel libtiff libtiff-devel libjpeg-turbo libjpeg-turbo-devel
然后重新build R, 请根据自己的包修改 R_VERSION
$ tar -xf %R_VERSION%.tar.gz
$ cd %R_VERSION%
$ ./configure --enable-R-shlib --with-libpng --with-jpeglib --with-libtiff --with-x
编译之后,支持了png那些

R is now configured for x86_64-unknown-linux-gnu

  Source directory:          .
  Installation directory:    /usr/local

  C compiler:                gcc -std=gnu99  -g -O2
  Fortran 77 compiler:       gfortran  -g -O2

  C++ compiler:              g++  -g -O2
  C++ 11 compiler:           g++  -std=c++0x -g -O2
  Fortran 90/95 compiler:    gfortran -g -O2
  Obj-C compiler:          

  Interfaces supported:      X11
  External libraries:        readline
  Additional capabilities:   PNG, JPEG, TIFF, NLS
  Options enabled:           shared R library, shared BLAS, R profiling

  Capabilities skipped:      cairo, ICU
  Options not enabled:       memory profiling

  Recommended packages:      yes


$ make clean
$ make

JAVA_HOME        : /usr/local/jdk1.7.0_79/jre
Java library path: $(JAVA_HOME)/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
Updating Java configuration in /home/R/R-3.2.0


$ sudo make install


png包安装不上,报错如下

> install.packages("png")

gcc -std=gnu99 -I/usr/local/lib64/R/include -DNDEBUG  -I/usr/local/include    `libpng-config --cflags` -fpic  -g -O2  -c read.c -o read.o
/bin/sh: libpng-config: command not found
read.c:3:17: 错误:png.h:没有那个文件或目录
read.c:15: 错误:expected ‘)’ before ‘png_ptr’

..........................

原因,一些R的包的安装,需要安装相应的-devel包,

解答:

1、yum install libpng-devel

2、进入R,install.packages("png")

这就可以安装成功。


用源码编译 GNU 软件的同学,一定要学会并记住使用 ./configure –help


<span class="GDXA2EVBP5">Error in RStudioGD() : 
  Shadow graphics device error: r error 4 (R code execution error)</span>
<span class="GDXA2EVBP5">
</span>
<span class="GDXA2EVBP5">
</span>
原来时安装的时候:
./configure  --enable-R-shlib <span style="color:#ff0000;">--with-x=no </span>    
改为:<pre name="code" tabindex="0" class="GDXA2EVBEAB" style="margin-top:0px; margin-bottom:0px; font-family:'Ubuntu Mono'; font-size:10.4pt!important; outline:none; border-style:none; white-space:pre-wrap!important; line-height:1.2">./configure  --enable-R-shlib 就OK了



<pre name="code" tabindex="0" class="GDXA2EVBEAB" style="font-family:'Ubuntu Mono'; font-size:10.4pt!important; outline:none; border-style:none; white-space:pre-wrap!important; margin-top:0px; margin-bottom:0px; line-height:1.2">用这个编译,./configure  --enable-R-shlib <span style="color:#ff0000;">--with-x=no </span>
输出如下,还是有不能支持的
R is now configured for x86_64-unknown-linux-gnu

  Source directory:          .
  Installation directory:    /usr/local

  C compiler:                gcc -std=gnu99  -g -O2
  Fortran 77 compiler:       gfortran  -g -O2

  C++ compiler:              g++  -g -O2
  C++ 11 compiler:           g++  -std=c++0x -g -O2
  Fortran 90/95 compiler:    gfortran -g -O2
  Obj-C compiler:          

  Interfaces supported:      
  External libraries:        readline
  Additional capabilities:   PNG, NLS
  Options enabled:           shared R library, shared BLAS, R profiling

  Capabilities skipped:      JPEG, TIFF, cairo, ICU
  Options not enabled:       memory profiling

  Recommended packages:      yes

configure: WARNING: you cannot build info or HTML versions of the R manuals
configure: WARNING: you cannot build PDF versions of the R manuals
configure: WARNING: you cannot build PDF versions of vignettes and help pages

对交互式会话有影响
如果你以 R --no-readline 启动有 readline 支持的 R
最明显的就会发现方向键上下翻命令历史的功能废了

./configure 无需管理员权限 如果你有权限 还是装下 readline 库吧


./configure --help



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值