在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

如果我将基本图像从rocker/r-ver:3.4.3更改为使用R 3.4.4和Debian testing/sid的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?

最佳答案

发生此问题是由于环境差异。当您通过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

在交互式shell中使用r -i从bash启动
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

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

关于r - 从R调用时,Libreoffice给出 "Application Error",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49318776/

10-15 13:55