You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter中R环境无法安装tidyverse的问题求助

解决Jupyter R环境安装tidyverse的权限与依赖问题

问题背景

在Jupyter的R环境中安装tidyverse时遇到三类错误:

  1. 提示依赖包MASS不可用
  2. 安装二进制包时因权限不足无法替换旧版本(如htmltools、jsonlite、rlang),出现Permission denied报错
  3. 多个源包(tinytex、lifecycle、tidyverse等)安装失败,返回非零退出状态
    已尝试卸载R、R Studio并删除Anaconda文件夹后重装,问题仍存在。具体报错日志如下:
> Warning message:
> "dependency 'MASS' is not available"also installing the dependencies 'htmltools', 'processx', 'xfun', 'bslib', 'tinytex', 'lifecycle', 'glue', 'tidyselect', 'vctrs', 'gtable', 'scales', 'gargle', 'timechange', 'systemfonts', 'vroom', 'progress', 'callr', 'knitr', 'rmarkdown', 'broom', 'conflicted', 'cli', 'dbplyr', 'dplyr', 'dtplyr', 'forcats', 'ggplot2', 'googledrive', 'googlesheets4', 'haven', 'hms', 'jsonlite', 'lubridate', 'magrittr', 'modelr', 'pillar', 'purrr', 'ragg', 'readr', 'readxl', 'reprex', 'rlang', 'rvest', 'stringr', 'tibble', 'tidyr', 'xml2'

There are binary versions available but the source versions are later:
binary source needs_compilation
htmltools     0.5.1.1  0.5.7              TRUE
processx        3.5.2  3.8.3              TRUE
xfun             0.22   0.41              TRUE
# ... 省略部分包版本信息 ...
tidyverse       1.3.1  2.0.0             FALSE

Binaries will be installed
package 'htmltools' successfully unpacked and MD5 sums checked
Warning message:
"cannot remove prior installation of package 'htmltools'"Warning message in file.copy(savedcopy, lib, recursive = TRUE):
"problem copying C:\ProgramData\anaconda3\Lib\R\library\00LOCK\htmltools\libs\x64\htmltools.dll to C:\ProgramData\anaconda3\Lib\R\library\htmltools\libs\x64\htmltools.dll: Permission denied"Warning message:
"restored 'htmltools'"
# ... 省略其他权限报错 ...
installation of package 'tidyverse' had non-zero exit status

解决方案

1. 优先解决权限问题

系统目录(C:\ProgramData\anaconda3)的写入权限不足是核心阻碍,可通过两种方式解决:

  • 以管理员身份启动Jupyter:右键点击Jupyter Notebook/Lab快捷方式,选择"以管理员身份运行",再执行安装命令。
  • 切换R包安装路径到用户目录:
    在Jupyter的R单元格中运行:
    # 设置用户专属的包安装目录
    user_lib <- file.path(Sys.getenv("USERPROFILE"), "R", "library")
    dir.create(user_lib, recursive = TRUE, showWarnings = FALSE)
    .libPaths(c(user_lib, .libPaths()))
    # 验证路径是否生效
    print(.libPaths())
    
    之后执行安装:install.packages("tidyverse")

2. 修复MASS依赖缺失

MASS是R的核心推荐包,缺失通常是镜像源或基础包损坏导致:

  • 切换国内CRAN镜像:
    options(repos = c(CRAN = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
    
  • 单独安装MASS:install.packages("MASS")
  • 若仍无效,重新安装Anaconda时选择**"Just Me"(仅为当前用户安装)**,避免系统级权限问题。

3. 解决源包编译失败问题

多数源包安装失败是因为缺少Windows编译工具Rtools,或强制安装了未兼容的源码版本:

  • 安装Rtools:下载对应R版本的Rtools(需与Anaconda内置R版本匹配),安装时勾选"Add Rtools to system PATH"。
  • 强制安装二进制包:
    install.packages("tidyverse", type = "binary")
    
  • 安装兼容版本的包:先查看当前R版本version,再安装对应版本的tidyverse(需先安装devtools):
    install.packages("devtools")
    devtools::install_version("tidyverse", version = "1.3.1")
    

4. 清理残留冲突包

若重装后仍有旧包冲突,手动清理:

  • 手动删除C:\ProgramData\anaconda3\Lib\R\library下的冲突包(如htmltools、jsonlite、rlang)。
  • 或用R命令卸载:
    remove.packages(c("htmltools", "jsonlite", "rlang"))
    
    卸载后重新安装tidyverse。

内容的提问来源于stack exchange,提问作者Rovshan Hasanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:50:54