R安裝xml遇到的問題 - installation of package 'xml2' had non-zero exit status

系統(tǒng)版本

macOS Mojave Version 10.14.2

R版本

R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin18.2.0 (64-bit)

xml安裝版本:

2_1.2.0

安裝xml2包報錯如下:

During startup - Warning messages:
1: Setting LC_TIME failed, using "C"
2: Setting LC_MESSAGES failed, using "C"
3: Setting LC_MONETARY failed, using "C"
* installing *source* package ‘xml2’ ...
** package ‘xml2’ successfully unpacked and MD5 sums checked
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/include/libxml2
Using PKG_LIBS=-L/usr/lib -lxml2 -lz -lpthread -licucore -lm
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘xml2’
* removing ‘/usr/local/lib/R/3.5/site-library/xml2’

The downloaded source packages are in
    ‘/private/tmp/Rtmp9tYdXH/downloaded_packages’
Warning message:
In install.packages("xml2") :
  installation of package ‘xml2’ had non-zero exit status

在網上調研了半天得到的導致結論是pkg-config不能準確定位到libxml2的位置, 這樣說不是很準確, 但不糾結, 下邊我們來看如何解決.

解決辦法

brew安裝如下包
brew install pkg-config
brew install libxml2

注意, libxml2 需配置一下PATH

brew info libxml2
# ==============================================================
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"
下載xml2包, 解壓

地址: https://mirrors.#edu.cn/CRAN/src/contrib/xml2_1.2.0.tar.gz, 如果地址失效, 在R安裝這個包 - install.packages('xml2'), 就可以看到可用地址了
解壓之后在iterm2(終端)打開:

?  ~ cd Downloads/xml2
?  xml2 pwd
/Users/*/Downloads/xml2
?  xml2 ll
total 80
-rw-r--r--@  1 *  staff   1.5K Jan 24  2018 DESCRIPTION
-rw-r--r--@  1 *  staff   6.5K Jan 24  2018 MD5
-rw-r--r--@  1 *  staff   5.9K Jan  5  2018 NAMESPACE
-rw-r--r--@  1 *  staff   5.4K Jan  6  2018 NEWS.md
drwxr-xr-x@ 25 *  staff   800B Jan 23  2018 R
-rw-r--r--@  1 *  staff   1.9K Nov 23  2017 README.md
drwxr-xr-x@  4 *  staff   128B Jan 23  2018 build
-rwxr-xr-x@  1 *  staff    29B Jan 23  2018 cleanup
-rwxr-xr-x@  1 *  staff   3.1K Jan 22 17:35 configure
-rw-r--r--@  1 *  staff     0B Nov  5  2015 configure.win
drwxr-xr-x@  5 *  staff   160B Jan 23  2018 inst
drwxr-xr-x@ 31 *  staff   992B Jan  5  2018 man
drwxr-xr-x@ 27 *  staff   864B Jan 22 17:35 src
drwxr-xr-x@  4 *  staff   128B Jan  5  2018 tests
drwxr-xr-x@  3 *  staff    96B Jun 13  2016 tools
drwxr-xr-x@  4 *  staff   128B Jan 23  2018 vignettes

注意, 其中configure文件需要我們修改

修改configure文件

xml2-config --version >/dev/null 2>&1
if [ $? -eq 0 ]; then
  PKGCONFIG_CFLAGS=`xml2-config --cflags`
  PKGCONFIG_LIBS=`xml2-config --libs`

  # MacOS versions El Capitan and later ship a xml2-config which appends `xcrun
  # --show-sdk-path` to the xml2-config. So we remove it if it is present.
  # (https://stat.ethz.ch/pipermail/r-sig-mac/2016-September/012046.html)
  if echo $OSTYPE | grep -q '^darwin'; then
    PKGCONFIG_CFLAGS=`echo $PKGCONFIG_CFLAGS | perl -pe "s{\Q\`xcrun -show-sdk-path\`\E}{}"`
    PKGCONFIG_LIBS=`echo $PKGCONFIG_LIBS | perl -pe "s{\Q\`xcrun -show-sdk-path\`\E}{}"`
  fi
else
  pkg-config --version >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKGCONFIG_CFLAGS=`pkg-config --cflags $PKG_CONFIG_NAME`
    PKGCONFIG_LIBS=`pkg-config --libs $PKG_CONFIG_NAME`
  fi
fi

修改為

if [ $(command -v pkg-config) ]; then
  PKGCONFIG_CFLAGS=$(pkg-config --cflags $PKG_CONFIG_NAME)
  PKGCONFIG_LIBS=$(pkg-config --libs $PKG_CONFIG_NAME)
fi

或者

# 添加制定路徑 - libxml2 文件夾的路徑
PKG_CFLAGS='-I/usr/local/Cellar/libxml2/2.9.9_2/include/libxml2'
PKG_LIBS='-L/usr/local/Cellar/libxml2/2.9.9_2/lib -lxml2'
# #####
# Find compiler
CC=`${R_HOME}/bin/R CMD config CC`
CFLAGS=`${R_HOME}/bin/R CMD config CFLAGS`
CPPFLAGS=`${R_HOME}/bin/R CMD config CPPFLAGS`
iterm2安裝xml2
# 必須在xml2文件夾下
R CMD INSTALL .

執(zhí)行信息

* installing to library ‘/usr/local/lib/R/3.5/site-library’
* installing *source* package ‘xml2’ ...
file ‘configure’ has the wrong MD5 checksum
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2
Using PKG_LIBS=-L/usr/local/Cellar/libxml2/2.9.8_1/lib -lxml2
** libs
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c RcppExports.cpp -o RcppExports.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c connection.cpp -o connection.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_doc.cpp -o xml2_doc.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_init.cpp -o xml2_init.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_namespace.cpp -o xml2_namespace.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_node.cpp -o xml2_node.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_output.cpp -o xml2_output.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_schema.cpp -o xml2_schema.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_url.cpp -o xml2_url.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_xpath.cpp -o xml2_xpath.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o xml2.so RcppExports.o connection.o xml2_doc.o xml2_init.o xml2_namespace.o xml2_node.o xml2_output.o xml2_schema.o xml2_url.o xml2_xpath.o -L/usr/local/Cellar/libxml2/2.9.8_1/lib -lxml2 -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
installing to /usr/local/lib/R/3.5/site-library/xml2/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (xml2)
在R運行
R運行結果

大家若有疑問可以聯(lián)系我.

參考文章: https://www.perfectlyrandom.org/2015/12/13/install-xml2-r-package-on-macos/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末淮阐,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌失乾,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異笔时,居然都是意外死亡,警方通過查閱死者的電腦和手機仗岸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門允耿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來借笙,“玉大人,你說我怎么就攤上這事较锡∫导冢” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵蚂蕴,是天一觀的道長低散。 經常有香客問我,道長骡楼,這世上最難降的妖魔是什么熔号? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮鸟整,結果婚禮上引镊,老公的妹妹穿的比我還像新娘。我一直安慰自己吃嘿,他們只是感情好祠乃,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布梦重。 她就那樣靜靜地躺著兑燥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪琴拧。 梳的紋絲不亂的頭發(fā)上降瞳,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機與錄音蚓胸,去河邊找鬼挣饥。 笑死,一個胖子當著我的面吹牛沛膳,可吹牛的內容都是我干的扔枫。 我是一名探鬼主播,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼锹安,長吁一口氣:“原來是場噩夢啊……” “哼短荐!你這毒婦竟也來了?” 一聲冷哼從身側響起叹哭,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤忍宋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后风罩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糠排,經...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年超升,在試婚紗的時候發(fā)現(xiàn)自己被綠了入宦。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哺徊。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖云石,靈堂內的尸體忽然破棺而出唉工,到底是詐尸還是另有隱情,我是刑警寧澤汹忠,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布淋硝,位于F島的核電站,受9級特大地震影響宽菜,放射性物質發(fā)生泄漏谣膳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一铅乡、第九天 我趴在偏房一處隱蔽的房頂上張望继谚。 院中可真熱鬧,春花似錦阵幸、人聲如沸花履。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诡壁。三九已至,卻和暖如春荠割,著一層夾襖步出監(jiān)牢的瞬間妹卿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工蔑鹦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留夺克,地道東北人。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓嚎朽,卻偏偏與公主長得像铺纽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子哟忍,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內容