install.packages的用法

本文详细介绍了如何在R环境中安装语言包,包括设置个人库路径、版本要求、使用install.packages函数及其参数详解等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何安装R语言包详见: Linux安装R语言包

 

使用公共路径上的R软件,如何拥有自己的library:

R中用.libPaths()函数查看lib路径,如果有多个lib,install.packages()默认是安装在第一个目录下

修改.bashrc文件中R lib路径的环境变量
export R_LIBS=/home/.../R/lib64/R/library,就能设定自己lib的默认目录,即使使用的R是别人安装的或是集体共用的,也能拥有自己的library
需要注意的是,安装和执行R的包对R程序有版本要求,所以要求R版本要高一些。只允许高版本R使用低版本包,不允许低版本R使用高版本包

install.packages

Description

Download and install packages from CRAN-like repositories or from local files.

Usage

install.packages(pkgs, lib, repos = getOption("repos"),
contriburl = contrib.url(repos, type),
method, available = NULL, destdir = NULL, 
dependencies = NA, type = getOption("pkgType"),
configure.args = getOption("configure.args"), 
configure.vars = getOption("configure.vars"), 
clean = FALSE, Ncpus = getOption("Ncpus", 1L),
libs_only = FALSE, INSTALL_opts, ...) 

Arguments

pkgs

character vector of the names of packages whose current versions should be downloaded from the repositories.

If repos = NULL, a character vector of file paths of ‘.zip’ files containing binary builds of packages. Source directories or ‘.tar.gz’ archives may also be installed, but some packages need suitable tools installed (see the ‘Details’ section).

If this is missing or a zero-length character vector, a listbox of available packages is presented where possible in an interactive R session.

lib

character vector giving the library directories where to install the packages. Recycled as needed. If missing, defaults to the first element of .libPaths().

repos

character vector, the base URLs of the repositories to use, e.g., the URL of a CRAN mirror such as "http://cran.us.r-project.org".

Can be NULL to install from local files (with extension ‘.tar.gz’ for source packages).

contriburl

URL(s) of the contrib sections of the repositories. Use this argument only if your repository mirror is incomplete, e.g., because you burned only the ‘contrib’ section on a CD. Overrides argument repos. As with repos, can also be NULL to install from local files.

method

download method, see download.file.

available

an object as returned by available.packages listing packages available at the repositories, or NULL when the function makes an internal call to available.packages.

destdir

directory where downloaded packages are stored. If it is NULL (the default) a subdirectory downloaded_packages of the session temporary directory will be used (and the files will be deleted at the end of the session).

dependencies

logical indicating to also install uninstalled packages which these packages depend on/link to/import/suggest (and so on recursively). Not used if repos = NULL. Can also be a character vector, a subset of c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances").

Only supported if lib is of length one (or missing), so it is unambiguous where to install the dependent packages. If this is not the case it is ignored, with a warning.

The default, NA, means c("Depends", "Imports", "LinkingTo").

TRUE means (as from R 2.15.0) to use c("Depends", "Imports", "LinkingTo", "Suggests") for pkgs and c("Depends", "Imports", "LinkingTo") for added dependencies: this installs all the packages needed to run pkgs, their examples, tests and vignettes (if the package author specified them correctly).

type

character, indicating the type of package to download and install.

Possible values are "source", "mac.binary.leopard" and "win.binary": the binary types can be listed and downloaded but not installed on other platforms.

The default is the appropriate binary type on Windows and on the CRAN binary Mac OS X distribution, otherwise "source". For the platforms where binary packages are the default, an alternative is "both" which means ‘try binary if available, otherwise try source’. (This will only choose the binary package if its version number is no older than the source version.)

configure.args

(not used on Windows) a character vector or a named list. If a character vector with no names is supplied, the elements are concatenated into a single string (separated by a space) and used as the value for the --configure-args flag in the call to R CMD INSTALL. If the character vector has names these are assumed to identify values for --configure-args for individual packages. This allows one to specify settings for an entire collection of packages which will be used if any of those packages are to be installed. (These settings can therefore be re-used and act as default settings.)

A named list can be used also to the same effect, and that allows multi-element character strings for each package which are concatenated to a single string to be used as the value for --configure-args.

configure.vars

(not used on Windows) analogous to configure.args for flag --configure-vars, which is used to set environment variables for the configure run.

clean

a logical value indicating whether to specify to add the --clean flag to the call to R CMD INSTALL. This is sometimes used to perform additional operations at the end of the package installation in addition to removing intermediate files.

Ncpus

The number of parallel processes to use for a parallel install of more than one source package. Values greater than one are supported if the make command specified by Sys.getenv("MAKE", "make") accepts argument -k -j Ncpus.

libs_only

a logical value: should the --libs-only option be used to install only additional sub-architectures? (See also INSTALL_opts.) This can also be used on Windows to install just the DLL(s) from a binary package, e.g. to add 64-bit DLLs to a 32-bit install.

INSTALL_opts

an optional character vector of additional option(s) to be passed to R CMD INSTALL for a source package install. E.g. c("--html", "--no-multiarch").

...

Arguments to be passed to download.file, or to the functions for binary installs on Mac OS X and Windows (which accept an argument "lock": the the section on ‘Locking’).

Details

R packages are primarily distributed as source packages, but binary packages (a packaging up of the installed package) are also supported, and the type most commonly used on Windows and from the CRAN distribution for Mac OS X. This function can install either type where supported, either by downloading a file from a repository or from a local file. The default type is given by getOption("pkgType"): this defaults to "source" apart from under Windows or a CRAN binary distribution for Mac OS X.

This is the main function to install packages. It takes a vector of names and a destination library, downloads the packages from the repositories and installs them. (If the library is omitted it defaults to the first directory in .libPaths(), with a message if there is more than one.) If lib is omitted or is of length one and is not a (group) writable directory, the code offers to create a personal library tree (the first element of Sys.getenv("R_LIBS_USER")) and install there. Detection of a writable directory is problematic on Windows: see the ‘Note’ section.

For source packages from a repository an attempt is made to install the packages in an order that respects their dependencies. This does assume that all the entries in lib are on the default library path for installs (set by R_LIBS).

Using packages with type = "source" always works on Windows provided the package contains no C/C++/Fortran code that needs compilation. Otherwise you will need to have installed the Rtools collection as described in the ‘R for Windows FAQ’ and you must have the PATH environment variable set up as required by Rtools.

When installing a binary package, install.packages will abort the install if it detects that the package is already installed and is currently in use. In some circumstances (e.g. multiple instances of R running at the same time and sharing a library) it will not detect a problem, but the installation may fail.

You are advised to run update.packages before install.packages to ensure that any already installed dependencies have their latest versions.

Argument libs_only = TRUE is supported for source installs and for Windows binary installs.

Value

Invisible NULL.

Locking

There are various options for locking: these differ between source and binary installs.

By default for a source install, the library directory is ‘locked’ by creating a directory ‘00LOCK’ within it. This has two purposes: it prevents any other process installing into that library concurrently, and is used to store any previous version of the package to restore on error. A finer-grained locking is provided by the option --pkglock which creates a separate lock for each package: this allows enough freedom for parallel installation. Per-package locking is the default when installing a single package, and for multiple packages when Ncpus > 1L. Finally locking (and restoration on error) can be suppressed by --no-lock.

For a Mac OS X or Windows binary install, no locking is done by default. Setting argument lock to TRUE (it defaults to the value of getOption("install.lock", FALSE)) will use per-directory locking as described for source installs: if the value is "pkglock" per-package locking will be used.

If package locking is used on Windows with libs_only = TRUE and the installation fails, the package will be restored to its previous state.

Note that it is possible for the package installation to fail so badly that the lock directory is not removed: this inhibits any further installs to the library directory (or for --pkglock, of the package) until the lock directory is removed manually.

Parallel installs

Parallel installs are attempted if pkgs has length greater than one and Ncpus > 1. It makes use of a parallel make, so the make specified (default make) when R was built must be capable of supporting make -j n: GNU make and dmake do, but FreeBSD and Solaris make do not: if necessary environment variable MAKE can be set for the current session to select a suitable make.

install.packages needs to be able to compute all the dependencies of pkgs from available, including if one element of pkgs depends indirectly on another. This means that if for example you are installing CRAN packages which depend on Bioconductor packages which in turn depend on CRAN packages, available needs to cover both CRAN and Bioconductor packages.

Note

install.packages tries to detect if you have write permission on the library directories specified, but Windows reports unreliably. If there is only one library directory (the default), R tries to find out by creating a test directory, but even this need not be the whole story. Under Windows Vista and later you may have permission to write in a library directory but lack permission to write binary files (such as ‘.dll’ files) there. See the ‘R for Windows FAQ’ for workarounds.

See Also

update.packages, available.packages, download.packages, installed.packages, contrib.url.

See download.file for how to handle proxies and other options to monitor file transfers.

INSTALL, REMOVE, remove.packages, library, .packages, read.dcf

The ‘R Installation and Administration’ manual for how to set up a repository.

Examples

## Not run:  install.packages(     c("XML_0.99-5.tar.gz", 
"http://www.cnblogs.com/Interfaces/Perl/RSPerl_0.8-0.tar.gz"),     repos = NULL, 
configure.args = c(XML = '--with-xml-config=xml-config',
RSPerl = "--with-modules='IO Fcntl'"))  ## End(Not run) 

 

R版本没问题,是最新的,但是仍然报错如下:> warnings() Warning messages: 1: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘proxy’ had non-zero exit status 2: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘checkmate’ had non-zero exit status 3: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘KernSmooth’ had non-zero exit status 4: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘wk’ had non-zero exit status 5: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘farver’ had non-zero exit status 6: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘lattice’ had non-zero exit status 7: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘cluster’ had non-zero exit status 8: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘nnet’ had non-zero exit status 9: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘foreign’ had non-zero exit status 10: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘data.table’ had non-zero exit status 11: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘colorspace’ had non-zero exit status 12: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘mvtnorm’ had non-zero exit status 13: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘SparseM’ had non-zero exit status 14: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘units’ had non-zero exit status 15: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘isoband’ had non-zero exit status 16: In install.packages("ggplot2", dependencies = TRUE) : installation of package ‘MASS’ had non-zero exit status 17: In install.packages("ggplot2", dependencies = TRUE) :
03-09
jupyter中输入R语言的以下代码,并报告下列提示,是什么情况?这种情况应该怎么? 代码:install.packages("readxl") install.packages("ggplot2") install.packages("psych") install.packages("optimx") install.packages("sjPlot") install.packages("targazer") install.packages("lmtest") install.packages("lme4") install.packages("ggpubr") install.packages("tidyr") install.packages("dplyr") install.packages("plyr") 提示: package 'readxl' successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages package 'ggplot2' successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages package 'psych' successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages package 'optimx' successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages package 'sjPlot' successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages Warning message: "package 'targazer' is not available for this version of R A version of this package for your version of R might be available elsewhere, see the ideas at https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages" package 'lmtest' successfully unpacked and MD5 sums checked Warning message: "cannot remove prior installation of package 'lmtest'" Warning message in file.copy(savedcopy, lib, recursive = TRUE): "problem copying D:\R-4.4.3\R-4.4.3\library\00LOCK\lmtest\libs\x64\lmtest.dll to D:\R-4.4.3\R-4.4.3\library\lmtest\libs\x64\lmtest.dll: Permission denied" Warning message: "restored 'lmtest'" The downloaded binary packages are in C:\Users\zhaoxr\AppData\Local\Temp\Rtmp6bboKg\downloaded_packages There is a binary version available but the sou
03-27
<think>嗯,用户想在Jupyter Notebook里用R内核安装特定的R包,比如targazer和lmtest,但遇到了问题。首先,我需要确认用户已经正确设置了R内核的Jupyter环境。根据引用[1],用户可能需要先确保R的路径已经添加到系统变量中,或者通过Anaconda Prompt进入R的安装目录启动R。如果用户没有正确配置,可能会导致安装包时找不到路径的问题。 接下来,用户提到的“package is in use”警告,通常是因为某个R包已经被加载,无法重新安装。这时候应该建议用户先退出当前会话,或者在安装前使用detach()函数解除包的加载。例如,detach("package:包名", unload=TRUE)这样可以卸载包,避免冲突。 关于“not available for this version of R”的警告,这可能是因为包的版本与当前R版本不兼容,或者包不在默认的CRAN仓库中。用户需要检查R的版本是否过旧,可以通过update.packages()更新所有包,或者指定其他镜像源。比如在install.packages()中设置repos参数为其他镜像,如https://cloud.r-project.org/,有时候可以解决这个问题。 另外,用户可能在使用Jupyter Notebook时,没有正确切换到R内核。根据引用[3],Jupyter的文件路径设置可能影响环境,但这里更关键的是确保在代码块中使用R语法,并且内核选择正确。安装包的时候,应该在R代码块中执行install.packages()命令,而不是用Python的pip。 还有,某些包可能需要依赖其他系统库,比如在Linux上可能需要安装额外的开发工具。用户如果遇到编译错误,可能需要安装这些依赖。例如,在Ubuntu上,安装r-base-dev可能有助于编译源代码。 最后,建议用户尝试手动安装,从CRAN下载源码,或者使用devtools安装GitHub上的版本。如果问题依然存在,检查安装后的加载情况,看看是否有权限问题或者路径错误。确保所有步骤都在正确的R环境中执行,避免环境混淆。</think>### 解答:在Jupyter Notebook中安装R包的方法 #### 步骤1:确认Jupyter已正确配置R内核 - 在Jupyter Notebook中选择R内核(右上角或菜单栏切换)[^3] - 若未安装R内核,需先运行以下命令: ```R install.packages("IRkernel") IRkernel::installspec() ``` #### 步骤2:处理"package is in use"警告 当遇到`'package is in use'`时: ```R # 在安装前卸载已加载的包 detach("package:包名", unload=TRUE) # 强制安装(慎用) install.packages("包名", dependencies=TRUE, INSTALL_opts='--no-lock') ``` #### 步骤3:解决"not available"警告 对于`not available for this version of R`: ```R # 检查R版本 R.version.string # 更新所有已安装包 update.packages(ask=FALSE, checkBuilt=TRUE) # 尝试指定镜像源 install.packages("包名", repos="https://cloud.r-project.org/") ``` #### 步骤4:安装特定包(以targazer为例) ```R # 在Jupyter的R代码块中执行 if(!require("targazer")){ install.packages("targazer") library(targazer) } ``` #### 步骤5:处理依赖问题 若提示缺少系统依赖(如Windows环境): - 安装Rtools工具链(需与R版本匹配) - Linux/Mac需安装开发工具: ```bash # Ubuntu示例 sudo apt-get install r-base-dev ``` #### 验证安装 ```R library(targazer) library(lmtest) # 无报错即表示成功 ``` ### 常见问题处理 1. **权限问题**:以管理员身份运行Jupyter或使用`install.packages(..., lib="/usr/local/lib/R/site-library")`指定路径 2. **版本冲突**:通过`install_version()`安装特定版本: ```R library(devtools) install_version("包名", version="x.x.x") ``` 3. **源码编译安装**(针对已下架包): ```R install.packages("https://cran.r-project.org/src/contrib/Archive/包名/包名_x.x.x.tar.gz", repos=NULL) ``` ### 引用说明 关于环境配置问题,请确保已按标准流程设置R系统变量[^1]和Jupyter文件路径。若使用Anaconda环境,建议通过`conda install r-包名`优先尝试安装预编译版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值