小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

R語(yǔ)言 | 關(guān)于包的加載

 微笑如酒 2019-02-08

我們知道,在使用R包前首先需要安裝

R語(yǔ)言 | 如果你再問(wèn)我怎么安裝R包

中我們重點(diǎn)講解了如何安裝R包,以最常見(jiàn)的 install.packages() ,安裝ggplot2包為例:

  1. install.packages('ggplot2')

這里必須給包名添加雙引號(hào)! 而使用pacman包中的 p_load(ggplot2) 則不需要加雙引號(hào)!

安裝之后,需要加載,最常用的方法是使用 library() 函數(shù)

If you will make a more intensive use of the package, then maybe is worth to load it into memory. The simplest way to do this is with the library() command.

  1. library(ggplot2)    # 或library('ggplot2')

一般不加雙引號(hào)!

Please note that the input of install.packages() is a character vector and requires the name to be in quotes, while library() accepts either character or name and makes it possible for you to write the name of the package without quotes.

除了 library() 函數(shù), require() 函數(shù)也用于加載包。其區(qū)別在于,對(duì)于加載未安裝的包,其返回的內(nèi)容是不同的,以'ggplot3'包(實(shí)際不存在這個(gè)包..., 只為本次測(cè)試用)為例, library(ggplot3) 會(huì)報(bào)錯(cuò):

Error in library(ggplot3) : 不存在叫‘ggplot3’這個(gè)名字的程輯包

require(ggplot3) 則只返回警告

Warning message:In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :不存在叫‘ggplot3’這個(gè)名字的程輯包

在R中報(bào)錯(cuò)會(huì)使得程序中斷,而警告一般不會(huì)影響程序進(jìn)行

‘require’ is designed for use inside other functions; it returns ‘FALSE’ and gives a warning (rather than an error as ‘library()’ does by default) if the package does not exist.

所以, require() 一般用在其他函數(shù)中,比如在 if 語(yǔ)句中判斷包是否存在,并依此做其他操作:

  1. if(require(ggplot3)){print('ggplot3已安裝!')}else{print('ggplot3未安裝!')}

這里,即便包不存在也不影響程序執(zhí)行~

package::function() 格式顯示調(diào)用某個(gè)包中的函數(shù),則不需要預(yù)先 library() 加載包,例如調(diào)用 devtools包中的 install_github() 函數(shù)安裝TCGAbiolinks包:

  1. devtools::install_github(repo = 'BioinformaticsFMRP/TCGAbiolinks')

而且,在面對(duì)多個(gè)包中存在相同名稱(chēng)的函數(shù)時(shí)能夠有效避免混淆。例如,psych 包和 Hmisc 包均提供了名為 describe() 的函數(shù),那在使用該函數(shù)的時(shí)候,程序如何確定所用的函數(shù)來(lái)自于哪個(gè)包呢?

1、如果已知加載了其中一個(gè)包,例如,程序中只加載了 Hmisc 包,則此時(shí)使用的 describe() 函數(shù)即為來(lái)源于 Hmisc 包的函數(shù)。

2、如果兩個(gè)包都被加載,則以最后加載的包優(yōu)先。如 psych 在 Hmisc 之后被加載,則會(huì)提示 Hmisc 包中的 describe() 函數(shù)被 psych 包中的同名函數(shù)所覆蓋,即函數(shù)來(lái)源于最后加載的R包!

3、如果不想引起不必要的混亂,可以用 package::function() 格式,例如 Hmisc::describe() 顯示調(diào)用!

需要注意的是, package::function() 不代表自動(dòng)將該包 library了,例如此時(shí)直接使用 devtools 包的 install_version 函數(shù),會(huì)報(bào)錯(cuò):

錯(cuò)誤: 找不到對(duì)象'install_version'

這一切,背后的原理就是NAMESPACE!

命名空間(NAMESPACE)


每個(gè)R包的根目錄都有一個(gè)NAMESPACE文件:

The package namespace (as recorded in the NAMESPACE file) is one of the more confusing parts of building a package.

其中記錄著哪些函數(shù)是提供(export)給用戶(hù)使用,需要哪些依賴(lài)包(import),哪些包的哪些函數(shù)(importFrom)

As the name suggests, namespaces provide “spaces” for “names”. They provide a context for looking up the value of an object associated with a name.

例如TCGAbiolinks的NAMESPACE文件

https://github.com/BioinformaticsFMRP/TCGAbiolinks/blob/master/NAMESPACE

export(GDCdownload)export(GDCprepare)...import(stringr)import(utils)importFrom(ComplexHeatmap,HeatmapAnnotation)importFrom(ComplexHeatmap,draw)...

所以,有時(shí)候你明明看到某個(gè)函數(shù)內(nèi)部調(diào)用了另一個(gè)函數(shù),但就是無(wú)法使用它(沒(méi)有export給用戶(hù))!

Generally, you want to export a minimal set of functions; the fewer you export, the smaller the chance of a conflict. While conflicts aren’t the end of the world because you can always use :: to disambiguate, they’re best avoided where possible because it makes the lives of your users easier.

search path


所以,程序一定是通過(guò)某種方式,檢索用戶(hù)需要使用的函數(shù)到底來(lái)源于哪里,才有可能使用它:

To call a function, R first has to find it. R does this by first looking in the global environment. If R doesn’t find it there, it looks in the search path, the list of all the packages you have attached.

  1. search()

上示是剛打開(kāi)的R,直接執(zhí)行 search() 得到的結(jié)果,顯示已經(jīng)加載的,處于檢索路徑下的R包。除了pacman 包是之前設(shè)置好的打開(kāi)R即自動(dòng)加載,其余則為默認(rèn)打開(kāi)R即加載的基礎(chǔ)包,也就是為什么有些函數(shù)是能直接使用的,比如求均值的 mean() 就來(lái)源于已默認(rèn)加載的base包!

我個(gè)人常說(shuō) library() 加載R包,但實(shí)際上這其中有兩個(gè)過(guò)程:

Loading:載入了R包,但是沒(méi)有將其置于檢索路徑下,所以只能通過(guò) :: 調(diào)用

:: will also load a package automatically if it isn’t already loaded.

Attaching:將R包置于檢索路徑下:

Attaching puts the package in the search path. You can’t attach a package without first loading it, so both library() or require() load then attach the package.

所以,為什么沒(méi)有 library(devtools) 的情況下可以使用 devtools::install_github()

因?yàn)椋?:: 會(huì)自動(dòng) load devtools 包,并使用其中的函數(shù)。

既然已經(jīng) load 了devtools 包,為什么仍不能使用 install_version 函數(shù)

因?yàn)?,load并沒(méi)有將包置于檢索路徑下,所以 search() 不到 devtools 包,也就沒(méi)法找到其中的函數(shù)!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多