解決Pycharm配置R語言環境報錯RWrapper terminated, exitcode: 127

kingwzun發表於2024-07-06

問題

解決Pycharm配置R語言環境報錯RWrapper terminated, exitcode: 127 error while loading shared libraries: libR.so: site:stackoverflow.com

解決方案

1. 開啟GetEnvVars.R檔案

開啟 C:\Users\UserName\AppData\Roaming\JetBrains\PyCharm版本號\plugins\r-plugin\R\目錄下的GetEnvVars.R檔案。

  • 注:上面的UserName和PyCharm版本號按實際情況查詢,如果沒有找到,看下PyCharm的安裝目錄有沒有...\plugins\r-plugin\R\GetEnvVars.R檔案,不同版本路徑可能有所區別

2.1 R版本大於3

如果R版本大於等於3,將內容替換成檔案GetEnvVars.R的內容替換成:

#  Rkernel is an execution kernel for R interpreter
#  Copyright (C) 2019 JetBrains s.r.o.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https:#www.gnu.org/licenses/>.

getLDLibraryPath <- function() {

  sysinf <- Sys.info()

  if (sysinf['sysname'] == "Darwin") Sys.getenv("DYLD_FALLBACK_LIBRARY_PATH")
  else if (tolower(sysinf['sysname']) == "linux") {
    if(Sys.getenv("LDLIBRARY_PATH") == "") {
      sessinf <- sessionInfo()
      interpreter_lib_dir <- dirname(sessinf$BLAS)
      paste(interpreter_lib_dir, paste0(interpreter_lib_dir, "/R/lib"), sep = ":")
    } else {
      Sys.getenv("LDLIBRARY_PATH")
    }
  }
  else ""
}

cat(">>>RPLUGIN>>>")
cat(R.home(), R.home('share'), R.home('include'), R.home('doc'), Sys.getenv("PATH"), getLDLibraryPath(), sep = '\n')
cat("<<<RPLUGIN<<<")

2.2 R版本低於3

如果R版本低於3,將內容替換成檔案GetEnvVars.R的內容替換成:

#  Rkernel is an execution kernel for R interpreter
#  Copyright (C) 2019 JetBrains s.r.o.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https:#www.gnu.org/licenses/>.

getLDLibraryPath <- function() {

  sysinf <- Sys.info()

  if (sysinf['sysname'] == "Darwin") Sys.getenv("DYLD_FALLBACK_LIBRARY_PATH")
  else if (tolower(sysinf['sysname']) == "linux") {
    if(Sys.getenv("LDLIBRARY_PATH") == "") {
      sessinf <- sessionInfo()
      interpreter_lib_dir <- dirname(sessinf$BLAS)
      if (R.version$major <= 3)
  	  	paste(dirname(dirname(interpreter_lib_dir)), interpreter_lib_dir, sep=':')
	  else if (R.version$major > 3)
      	paste(interpreter_lib_dir, paste0(interpreter_lib_dir, "/R/lib"), sep = ":")
	  else 
	  	""
    } else {
      Sys.getenv("LDLIBRARY_PATH")
    }
  }
  else ""
}

cat(">>>RPLUGIN>>>")
cat(R.home(), R.home('share'), R.home('include'), R.home('doc'), Sys.getenv("PATH"), getLDLibraryPath(), sep = '\n')
cat("<<<RPLUGIN<<<")

其他可能的原因

如果上面的方案沒有解決,可以嘗試下面的解決方案:

  1. 如果是從原始碼編譯安裝的,自編譯的時候未使用該--enable-R-shlib選項編譯 R。(源地址)
    安裝教程:Linux下R安裝配置以及工具包安裝方式

相關文章