齐鲁风采群英会和加载R包的有效方法

Antoine Soetewey 2020-01-31 6 minute read

什么是R包裹以及如何使用它?

与其他程序不同,默认情况下只有基本功能才能使用R来实现。因此,您将经常需要齐鲁风采群英会一些“扩展”以执行所需的分析。这些扩展是R用户开发和发布的函数和数据集集合的扩展 。它们通过添加新的基础R功能来扩展现有基础R功能。 R是开源的,所以每个人都可以编写代码并将其发布为包,每个人都可以齐鲁风采群英会包并开始使用包内部内置的功能或数据集,所有这些都是免费的。

In order to use a package, it needs to be installed on your computer by running install.packages("name_of_package") (do not forget "" around the name of the package, otherwise R will look for an object saved under that name!). Once the package is installed, you must load the package and only after it has been loaded you can use all the functions and datasets it contains. To load a package, run library(name_of_package) (this time "" around the name of the package are optional, but can still be used if you wish).

齐鲁风采群英会和加载R包的低效方法

根据您使用的r多长时间,您可以使用有限的包装或相反,大量的包装。当您使用越来越多的软件包时,您将很快开始拥有(太多代码行,用于齐鲁风采群英会和加载它们。

以下是来自我的博士学位论文的代码预览,展示了R包的齐鲁风采群英会和加载在r(仅显示它们的一小部分时,缩短代码):1

# Installation of required packages
install.packages("tidyverse")
install.packages("ggplot2")
install.packages("readxl")
install.packages("dplyr")
install.packages("tidyr")
install.packages("ggfortify")
install.packages("DT")
install.packages("reshape2")
install.packages("knitr")
install.packages("lubridate")

# Load packages
library("tidyverse")
library("ggplot2")
library("readxl")
library("dplyr")
library("tidyr")
library("ggfortify")
library("DT")
library("reshape2")
library("knitr")
library("lubridate")

如您所能猜测代码变得越来越长,因为我需要越来越多的包装来分析。此外,我倾向于重新齐鲁风采群英会所有包裹,因为我在4种不同的计算机上工作,我不记得已经齐鲁风采群英会了哪台机器的软件包。每次打开脚本或r Markdown文件时,每次重新齐鲁风采群英会所有软件包都是浪费时间。

更高效的方式

然后,有一天,我的一位同事与我分享了一些代码。我很高兴他尽他所做的,因为他向我推出了更有效的方式来齐鲁风采群英会和加载r包。他给了我分享提示的许可,所以这里是我现在用来执行齐鲁风采群英会和加载R包的任务的代码:

# Package names
packages <- c("ggplot2", "readxl", "dplyr", "tidyr", "ggfortify", "DT", "reshape2", "knitr", "lubridate", "pwr", "psy", "car", "doBy", "imputeMissings", "RcmdrMisc", "questionr", "vcd", "multcomp", "KappaGUI", "rcompanion", "FactoMineR", "factoextra", "corrplot", "ltm", "goeveg", "corrplot", "FSA", "MASS", "scales", "nlme", "psych", "ordinal", "lmtest", "ggpubr", "dslabs", "stringr", "assist", "ggstatsplot", "forcats", "styler", "remedy", "snakecaser", "addinslist", "esquisse", "here", "summarytools", "magrittr", "tidyverse", "funModeling", "pander", "cluster", "abind")

# Install packages not yet installed
installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))

齐鲁风采群英会和加载R包的此代码更有效地提供:

  1. The function install.packages() accepts a vector as argument, so one line of code for each package in the past is now one line including all packages
  2. 在代码的第二部分中,它会检查是否已齐鲁风采群英会包是已齐鲁风采群英会的,然后仅齐鲁风采群英会丢失的包
  3. Regarding the packages loading (the last part of the code), the lapply() function is used to call the library() function on all packages at once, which makes the code more condense.
  4. The output when loading a package is rarely useful. The invisible() function removes this output.

From that day on, every time I need to use a new package, I simply add it to the vector at the top of the code, which is located at the top of my scripts and R Markdown documents. No matter on which computer I am working on, running the entire code will install only the missing packages and will load all of them. This greatly reduced the running time for the installation and loading of my R packages.

最有效的方式

{pacman} package

After this article was published, a reader informed me about the {packman} package. After having read the documentation and try it out myself, I learned that the function p_load() from {pacman} checks to see if a package is installed, if not it attempts to install the package and then loads it. It can also be applied to several packages at once, all this in a very condensed way:

install.packages("pacman")

pacman::p_load(ggplot2, tidyr, dplyr)

查找有关此包的更多信息 cr.

{librarian} package

Like {pacman}, the shelf() function from the {librarian} 包裹 automatically installs, updates, and loads R packages that are not yet installed in a single function. The function accepts packages from CRAN, GitHub, and Bioconductor (only if Bioconductor’s Biobase 包裹 is installed). The function also accepts multiple package entries, provided as a comma-separated list of unquoted names (so no "" around package names).

Last but not least, the {librarian} 包裹 allows to load packages automatically at the start of every R session (thanks to the lib_startup() function) and search for new packages on CRAN by keywords or regular expressions (thanks to the browse_cran() function).

Here is an example of how to install missing packages and load them with the shelf() function:

# From CRAN:
install.packages("librarian")

librarian::shelf(ggplot2, DesiQuintans / desiderata, pander)

For CRAN packages, provide the package name as normal without "" and for GitHub packages, provide the username and package name separated by / (i.e., UserName/RepoName as shown for the desiderata package).

查找有关此包的更多信息 cr.

谢谢阅读。我希望这篇文章有助于您以更有效的方式齐鲁风采群英会和加载R包。

一如既往,如果您有问题或与本文所涵盖的主题相关的建议,请将其添加为评论,以便其他读者可以从讨论中受益。

A special thanks Danilo and James for informing me about the {pacman} and {librarian} packages.


  1. 如图所示 帕特里克,这是一个很好的做法 评论 (with a # in front of the line of code) the installation of your packages after they have been installed on your computer. This avoids installing packages on someone else’s computer when you share your code. If they want to install them before running your code, they will need to do it by themselves.↩︎



喜欢这篇文章?

获取更新 每次发布新文章。
任何垃圾邮件都没有任何垃圾邮件。
分享: