当从R调用时,Libreoffice给出“应用程序错误”

时间:2018-03-16 10:42:13

标签: r docker libreoffice

在docker容器中我试图使用LibreOffice将XLSX文件转换为PDF。相关命令在命令行上工作,但在从R调用时失败并显示“Application Error”。我使用此Dockerfile添加一些(根据我的经验任意)XLSX文件:

FROM rocker/r-ver:3.4.3

RUN apt-get update \
 && apt-get install --yes --no-install-recommends \
    default-jre-headless libreoffice-calc \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/* \
 && echo /usr/lib/libreoffice/program > /etc/ld.so.conf.d/libreoffice.conf \
 && ldconfig

COPY foo.xlsx /tmp

ldconfig的诀窍来自 shared library issue with the system function in R。)

在命令行上,我可以将XLSX文件转换为PDF:

root@b395caeba33b:/# loffice --headless --convert-to pdf /tmp/foo.xlsx 
convert /tmp/foo.xlsx -> //foo.pdf using filter : calc_pdf_Export

但是,R:

失败了
> system("loffice --version")
LibreOffice 5.2.7.2 20m0(Build:2)

> system("loffice --headless --convert-to pdf /tmp/foo.xlsx")
convert /tmp/foo.xlsx -> //foo.pdf using filter : calc_pdf_Export
Application Error

如果我将基本图像从使用R 3.4.4和Debian testing / sid的rocker/r-ver:3.4.3更改为rocker/r-base,结果只会略有变化:

> system("loffice --version")
LibreOffice 6.0.2.1.0 00m0(Build:1)

> system("loffice --headless --convert-to pdf /tmp/foo.xlsx")
Application Error

从R调用时,如何让LibreOffice将XLSX文件转换为PDF?

2 个答案:

答案 0 :(得分:1)

我找到了一个解决方法,但我仍然对正确的解释感兴趣。这是我发现的:

  • 使用选项function evaluate() { const input = prompt("Please enter the array of integers in the form: 1,2,3,1") .split(',') .map(item => item.trim()); function pairs(items) { } if (input == "" || input == null) { document.writeln("Sorry, there is nothing that can be calculated."); } else { document.writeln("Your calculation is: "); document.writeln(pairs(input) + " with a starting input string of: " + input); } } evaluate()启动docker容器并安装--security-opt seccomp:unconfined
  • strace来电

    R
  • 生成的跟踪文件显示加载system("strace -f -o R.trace loffice --headless --convert-to pdf /tmp/foo.xlsx") 时出错。奇怪的是它在libsal_textenclo.so中搜索了库,即使/usr/lib/x86_64-linux-gnu知道在哪里找到它:

    ldconfig
  • 设置root@1519f52c05e0:/# grep libsal R.trace 257 open("/usr/lib/x86_64-linux-gnu/libsal_textenclo.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) root@1519f52c05e0:/# ldconfig -p | grep libsal libsal_textenclo.so (libc6,x86-64) => /usr/lib/libreoffice/program/libsal_textenclo.so 以包含LD_LIBRARY_PATH 解决问题。

    /usr/lib/libreoffice/program
  • 我当前的解决方法是在R会话中设置root@4a235dfa08e3:~# export LD_LIBRARY_PATH=/usr/lib/libreoffice/program root@4a235dfa08e3:~# Rscript -e 'system("loffice --headless --convert-to pdf /tmp/foo.xlsx")' Application Error

    LD_LIBRARY_PATH

答案 1 :(得分:1)

由于环境差异,问题就出现了。通过env

运行system命令时
> system('env')
R_UNZIPCMD=/usr/bin/unzip
HOSTNAME=da4d504ddcb1
LD_LIBRARY_PATH=/usr/local/lib/R/lib:/usr/local/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server
SHLVL=0
HOME=/root
R_LIBS_SITE=
R_BROWSER=xdg-open
PAGER=/usr/bin/pager
R_VERSION=3.4.3
BUILD_DATE=
R_SYSTEM_ABI=linux,gcc,gxx,gfortran,?
TAR=/bin/tar
R_LIBS_USER=/usr/local/lib/R/site-library
TERM=xterm
COLUMNS=200
R_ARCH=
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
R_BZIPCMD=/bin/bzip2
R_INCLUDE_DIR=/usr/local/lib/R/include
R_SESSION_TMPDIR=/tmp/RtmpJsaXba
LANG=en_US.UTF-8
R_GZIPCMD=/bin/gzip
SED=/bin/sed
LN_S=ln -s
R_PDFVIEWER=/usr/bin/xdg-open
R_TEXI2DVICMD=/usr/bin/texi2dvi
R_HOME=/usr/local/lib/R
R_PRINTCMD=/usr/bin/lpr
R_DOC_DIR=/usr/local/lib/R/doc
R_LIBS=/usr/local/lib/R/site-library:/usr/local/lib/R/library:/usr/lib/R/library
LC_ALL=en_US.UTF-8
R_SHARE_DIR=/usr/local/lib/R/share
PWD=/
R_ZIPCMD=/usr/bin/zip
R_PLATFORM=x86_64-pc-linux-gnu
R_PAPERSIZE=letter
LINES=50
MAKE=make
R_RD4PDF=times,inconsolata,hyper
EDITOR=vi

您可以看到默认R包含一组环境变量,其中一个是LD_LIBRARY_PATH

> system('loffice --headless --convert-to pdf /tmp/foo.xlsx')
Application Error
> system('LD_LIBRARY_PATH= loffice --headless --convert-to pdf /tmp/foo.xlsx')
convert /tmp/foo.xlsx -> //foo.pdf using filter : calc_pdf_Export

将其删除并且有效。它在bash中工作的原因是因为默认环境变量集很小

root@5c5bbcfcebf2:/# env
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
HOSTNAME=5c5bbcfcebf2
PWD=/
HOME=/root
R_VERSION=3.4.3
BUILD_DATE=
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

当您从bash

启动r而不是R
root@5c5bbcfcebf2:/# r -i
system('env')
R_UNZIPCMD=/usr/bin/unzip
HOSTNAME=5c5bbcfcebf2
SHLVL=1
R_INSTALL_PKG=littler
HOME=/root
R_ENVIRON=
R_LIBS_SITE=
R_BROWSER=xdg-open
PAGER=/usr/bin/pager
R_VERSION=3.4.3
BUILD_DATE=
R_SYSTEM_ABI=linux,gcc,gxx,gfortran,?
R_PROFILE_USER=
TAR=/bin/tar
_=/usr/local/bin/r
R_LIBS_USER=/usr/local/lib/R/site-library
TERM=xterm
R_ARCH=
R_PAPERSIZE_USER=letter
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
R_BZIPCMD=/bin/bzip2
R_INCLUDE_DIR=/usr/local/lib/R/include
R_SESSION_TMPDIR=/tmp
R_OSTYPE=unix
LANG=en_US.UTF-8
R_CMD=/usr/local/lib/R/bin/Rcmd
R_DEFAULT_PACKAGES=NULL
R_PACKAGE_NAME=littler
R_GZIPCMD=/bin/gzip
LN_S=ln -s
SED=/bin/sed
R_PDFVIEWER=/usr/bin/xdg-open
R_PROFILE=
R_ENVIRON_USER=
R_TEXI2DVICMD=/usr/bin/texi2dvi
R_HOME=/usr/local/lib/R
R_PRINTCMD=/usr/bin/lpr
R_DOC_DIR=/usr/local/lib/R/doc
R_LIBS=/usr/local/lib/R/site-library:/usr/local/lib/R/library:/usr/lib/R/library
LC_ALL=en_US.UTF-8
PWD=/
R_SHARE_DIR=/usr/local/lib/R/share
R_ZIPCMD=/usr/bin/zip
R_PAPERSIZE=letter
R_PLATFORM=x86_64-pc-linux-gnu
MAKE=make
R_RD4PDF=times,inconsolata,hyper
EDITOR=vi

使用r -i

从bash启动交互式shell
system('LD_LIBRARY_PATH= loffice --headless --convert-to pdf /tmp/foo.xlsx')
convert /tmp/foo.xlsx -> //foo.pdf using filter : calc_pdf_Export

system('loffice --headless --convert-to pdf /tmp/foo.xlsx')
convert /tmp/foo.xlsx -> //foo.pdf using filter : calc_pdf_Export
Overwriting: //foo.pdf

从父级继承的子进程环境会导致您的情况出现问题