在 R 中安装 mgcv 包时出现段错误

Segfault when installing mgcv package in R

每当我尝试使用 ggplot2 的函数 geom_smooth() 时,我都会遇到段错误。我发现问题是由于 mcgv 包引起的,它是 R 推荐的包,只要包含它就会出现段错误。这是其安装的输出:

> install.packages('mgcv')
Installing package into ‘/home/<username>/R/x86_64-pc-linux-gnu-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/mgcv_1.8-17.tar.gz'
Content type 'unknown' length 849787 bytes (829 KB)
==================================================
downloaded 829 KB
* installing *source* package ‘mgcv’ ...
** package ‘mgcv’ successfully unpacked and MD5 sums checked
** libs
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c coxph.c -o coxph.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c discrete.c -o discrete.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c gdi.c -o gdi.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c init.c -o init.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c magic.c -o magic.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c mat.c -o mat.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c matrix.c -o matrix.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c mgcv.c -o mgcv.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c misc.c -o misc.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c mvn.c -o mvn.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c qp.c -o qp.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c soap.c -o soap.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c sparse-smooth.c -o sparse-smooth.o
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG  -I/usr/local/include   -fopenmp -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c tprs.c -o tprs.o
gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -Wl,-z,relro -o mgcv.so coxph.o discrete.o gdi.o init.o magic.o mat.o matrix.o mgcv.o misc.o mvn.o qp.o soap.o sparse-smooth.o tprs.o -L/usr/lib64/R/lib -lRlapack -L/usr/lib64/R/lib -lRblas -lgfortran -lm -lquadmath -fopenmp -L/usr/lib64/R/lib -lR
installing to /home/<username>/R/x86_64-pc-linux-gnu-library/3.4/mgcv/libs
** R
** data
** inst
** byte-compile and prepare package for lazy loading
Warning in fun(libname, pkgname) :
  bytecode version mismatch; using eval
Warning: package ‘nlme’ was built under R version 3.4.0
 *** caught segfault ***
address 0x6, cause 'memory not mapped'
Traceback:
 1: dyn.load(file, DLLpath = DLLpath, ...)
 2: library.dynam(lib, package, package.lib)
 3: loadNamespace(package, lib.loc)
 4: doTryCatch(return(expr), name, parentenv, handler)
 5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 6: tryCatchList(expr, classes, parentenv, handlers)
 7: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        msg <- conditionMessage(e)        sm <- strsplit(msg, "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = stderr())        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
 8: try({    attr(package, "LibPath") <- which.lib.loc    ns <- loadNamespace(package, lib.loc)    env <- attachNamespace(ns, pos = pos, deps)})
 9: library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc,     quietly = quietly)
10: .getRequiredPackages2(pkgInfo, quietly, lib.loc, useImports)
11: .getRequiredPackages(quietly = TRUE)
12: withCallingHandlers(expr, packageStartupMessage = function(c) invokeRestart("muffleMessage"))
13: suppressPackageStartupMessages(.getRequiredPackages(quietly = TRUE))
14: doTryCatch(return(expr), name, parentenv, handler)
15: tryCatchOne(expr, names, parentenv, handlers[[1L]])
16: tryCatchList(expr, classes, parentenv, handlers)
17: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        msg <- conditionMessage(e)        sm <- strsplit(msg, "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = stderr())        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
18: try({    suppressPackageStartupMessages(.getRequiredPackages(quietly = TRUE))    makeLazyLoading(pkg_name, lib, keep.source = keep.source)})
19: do_install_source(pkg_name, instdir, pkg, desc)
20: do_install(pkg)
21: tools:::.install_packages()
An irrecoverable exception occurred. R is aborting now ...
/usr/lib64/R/bin/INSTALL: line 34: 36723 Done                    echo 'tools:::.install_packages()'
     36724 Segmentation fault      | R_DEFAULT_PACKAGES= LC_COLLATE=C "${R_HOME}/bin/R" $myArgs --slave --args ${args}
Warning in install.packages :
  installation of package ‘mgcv’ had non-zero exit status

这是我的 R 版本:

> R.version
               _                           
platform       x86_64-redhat-linux-gnu     
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          3.3                         
year           2017                        
month          03                          
day            06                          
svn rev        72310                       
language       R                           
version.string R version 3.3.3 (2017-03-06)
nickname       Another Canoe  

对发生的事情有什么想法吗?

将您的 R 版本升级到 3.4.0。特别是 mgcv 的编译是成功的(因为你已经到了字节编译阶段)。问题是它的依赖包nlme,需要R版本3.4.0

sudo apt-get update
sudo apt-get upgrade

我想一旦你升级了 R,你不需要做任何其他事情。 mgcvnlme 也会自动升级,因为它们是 "recommended"。

I'll give it a shot--but unfortunately I don't have sudo on this machine, so it will take a while to try and get it upgraded. I wonder if there are any solutions I can try in the interim.

您是否必须安装其他版本的 mgcv?为什么不直接 library(mgcv) 并使用与您当前的 R 版本兼容的版本呢?


请允许我重新制定 The_Anomaly 的最终本地解决方案:

After I restarted R studio's R session properly (i.e. manually restarted, not just recover from a crash), I could no longer load any packages that I had installed. So I start looking at .libPaths() but found surprisingly that the folder "R/x86_64-redhat-linux-gnu-library/3.3" did not have any of my packages, but another folder ".../3.4" did. For some reason, prior to this restart, R version 3.3 was using packages built for R 3.4. Your solution would have likely worked because it would have solved this mismatch, so I marked it as correct -- but for me the restart actually worked.