无法安装R包,报错‘& was unexpected at this time.’求助
R包安装报错:
& was unexpected at this time.(Windows 11环境) 问题详情
- 系统:Windows 11
- 环境:R 4.2.2,降级后的旧版RStudio
- 问题:使用
install.packages()或devtools::install_github()安装包时触发& was unexpected at this time.错误,安装失败。此前同版本R可正常安装包,重装R并降级RStudio后出现该问题。
报错示例1(install.packages)
> install.packages('RSQLite') Installing package into ‘C:/Users/Ryan/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) There is a binary version available but the source version is later: binary source needs_compilation RSQLite 2.2.19 2.2.20 TRUE installing the source package ‘RSQLite’ trying URL 'https://cran.rstudio.com/src/contrib/RSQLite_2.2.20.tar.gz' Content type 'application/x-gzip' length 3745301 bytes (3.6 MB) downloaded 3.6 MB & was unexpected at this time. Warning in install.packages : installation of package ‘RSQLite’ had non-zero exit status The downloaded source packages are in ‘C:\Users\Ryan\AppData\Local\Temp\Rtmp4uP1BH\downloaded_packages’
报错示例2(devtools::install_github)
> devtools::install_github("r-dbi/RSQLite") Downloading GitHub repo r-dbi/RSQLite@HEAD ── R CMD build ─────────────────────────────────────────────────────────────────────── & was unexpected at this time. Error: Failed to install 'RSQLite' from GitHub: ! System command 'Rcmd.exe' failed
解决步骤
1. 优先安装二进制包
报错触发于源码包编译阶段,先尝试直接安装预编译的二进制版本:
install.packages('RSQLite', type = 'binary')
2. 检查路径是否含特殊字符
R的安装目录、库路径如果包含空格、&或中文等特殊字符,会触发终端解析错误:
- 执行以下命令查看当前库路径:
.libPaths() - 若路径有特殊字符,建议修改到无特殊字符的目录(比如
C:\R\library\4.2),可通过修改R_LIBS_USER环境变量实现。
3. 切换RStudio终端类型
将RStudio默认终端从PowerShell改为Command Prompt:
- 打开
Tools > Global Options > Terminal - 选择终端类型为
Command Prompt,重启RStudio
4. 检查并修复R编译环境
Windows下安装源码包需要对应版本的Rtools:
- 执行
devtools::find_rtools()检查是否检测到Rtools(需先安装devtools) - 若未安装,下载对应R 4.2.x的Rtools42,安装时勾选“Add Rtools to system PATH”
5. 重置RStudio配置
关闭RStudio后,删除配置文件夹C:\Users\Ryan\AppData\Roaming\RStudio,重新启动RStudio尝试安装。
内容的提问来源于stack exchange,提问作者Ryguy
相关产品推荐
相关产品推荐

