当前位置: 首页 > 工具软件 > Caret > 使用案例 >

R语言学习记录之安装caret包与jupyter关联R内核

张玺
2023-12-01

1、安装caret包

#以下是caret包所依赖的所有包,可以用如下脚本进行安装预先依赖包
caret_needed_packages <- c("ps","processx","callr","prettyunits","backports","desc","pkgbuild","rprojroot","rstudioapi","numDeriv","SQUAREM","evaluate","pkgload","praise","colorspace","assertthat","utf8","lava","testthat","farver","labeling","munsell","R6","RColorBrewer","viridisLite","cli","crayon","ellipsis","fansi","pillar","pkgconfig","vctrs","stringi","prodlim","cpp11","digest","glue","gtable","isoband","rlang","scales","tibble","iterators","Rcpp","data.table","stringr","dplyr","generics","gower","ipred","lifecycle","lubridate","magrittr","purrr","tidyr","tidyselect","timeDate","ggplot2","foreach","plyr","ModelMetrics","reshape2","recipes","withr","pROC")
installed_pkgs <- installed.packages()
ind_flag<- caret_needed_packages %in% installed_pkgs
ind_needed_pkg<-caret_needed_packages[!ind_flag]
for(pkg in ind_needed_pkg){
  install.packages(pkg,repos = "https://mirror.lzu.edu.cn/CRAN/")
}

或者,直接安装——需要注意的是一定注意镜像地址的选择,否则可能出现奇怪的问题。

install.packages("caret",repos = "https://mirror.lzu.edu.cn/CRAN/")

2、两种方式设置镜像网站

第一种,临时设置,使用options()函数

old_options<-options()
old_options$repos
options(repos = "https://mirror.lzu.edu.cn/CRAN/")
getOption("repos")

使用setRepositories()函数,也可以在BioC等镜像上设置

setRepositories(addURLs = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/ ")

第二种,在Rstudio种Tools->Global Options ->Packages ->Management页设置,永久性的

3、在jupyter notebook中关联R语言

第一步,安装相关依赖包

install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))

第二步,按照网络资料说明,安装devtools,然后从Github上安装IRkernel包

devtools::install_github('IRkernel/IRkernel')

第三步,其实可以直接从CRAN上下载该IRkernel包的

install.packages("IRkernel")

第四步,R内核关联jupyter notebook

IRkernel::installspec()

或者,运行如下命令,直接安装在系统下

IRkernel::installspec(user = FALSE)

此时如果出现如下错误消息

Error in IRkernel::installspec() :
jupyter-client has to be installed but “jupyter kernelspec --version” exited with code 1.

此时有两种解决方法:

(1)在CMD中运行 IRkernel::installspec()命令,前提是:将R添加进环境变量,将anaconda添加进环境变量(主要是将Anaconda3\Scripts,Anaconda3,Anaconda3\Library等添加进环境变量);

(2)直接在Anaconda prompt,在里边运行R,确保R的环境变量添加了,然后运行上述命令;

更新于2020-11-11下午,新的问题

在window10台式机上进行配置时,发现依旧出现问题如上:

> IRkernel::installspec()
Error in IRkernel::installspec() : 
  jupyter-client has to be installed but “jupyter kernelspec --version” exited with code 1.

按照上述两个方法操作之后,报错结果依据。。。。
一番网上搜索,没有头绪。
执行命令查看jupyter内核,出现如下错误:

>jupyter kernelspec list
Error executing Jupyter command 'kernelspec': [WinError 5] 拒绝访问。

也就是,权限错误——考虑权限问题,进行了如下操作:
(1)对 C:\ProgramData\Anaconda3文件夹进行整体的权限设置:右键属性—>安全—>编辑,为当前用户设置完全权限;
执行结果:依旧失败
(2)在路径C:\ProgramData\Anaconda3\Scripts下找到执行文件 jupyter-kernelspec.exe ,查看发现权限都是完全控制,但以管理员权限依旧无法运行;尝试在 右键属性—>安全—>编辑—>高级“启用继承”
执行结果:

> IRkernel::installspec()
[InstallKernelSpec] Installed kernelspec ir in C:\Users\zhangzhuo3\AppData\Roaming\jupyter\kernels\ir

为啥?我也不知道为啥!!!实在不太懂windows!

 类似资料: