如何分離 R 中的程式包?


要分離 R 中的程式包,我們可以簡單地使用 detach 函式。但我們需要記住,一旦程式包分離,就沒有辦法使用該特定程式包的任何函式。如果忘記了分離命令,我們會出現這樣的錯誤。例如,如果我們使用 detach detach(package:ggplot2, unload=TRUE) 函式分離 ggplot2 程式包,然後再執行 ggplot 或者 qplot 函式,就會出現錯誤。

示例

考慮下面的資料框 -

現場演示

> x<-rnorm(10)
> y<-rnorm(10)
> df<-data.frame(x,y)
> df

輸出

x y
1 -0.09124881 0.8106691
2 -0.20521435 -1.0067072
3 -1.07904498 1.3867400
4 1.34461945 -1.4676405
5 -0.21731862 0.5801624
6 -0.54413731 0.1817524
7 1.05737101 0.7518694
8 -0.08566360 0.3428287
9 -0.40894417 -0.2992289
10 1.71507507 0.9979026

載入 ggplot2 包並建立散點圖 -

> library(ggplot2)
> ggplot(df,aes(x,y))+geom_point()

輸出

分離 ggplot2 包 -

> detach(package:ggplot2,unload=TRUE)

現在再次建立繪圖 -

> ggplot(df,aes(x,y))+geom_point()
Error in ggplot(df, aes(x, y)) : could not find function "ggplot"

更新時間: 2021 年 1 月 4 日

11K+ 瀏覽

開啟您的 職業生涯

完成課程後獲得認證

立即開始
廣告
© . All rights reserved.