R语言中的defaults包是干什么用的

Python012

R语言中的defaults包是干什么用的,第1张

1. 列出包所在库的路径

.libPaths()

[1] "C:/Program Files/R/R-3.0.2/library"

2. 安装包,括号里面包的名称要加英文引号,在列出的CRAN镜像站点列表中选择一个进行下载,我一般选的是China(Hefei)

install.packages()

例如,install.packages("ggplot2")

3. 包的载入library()或require(),安装完包后,需要加载才能使用其中的函数,此时括号中不使用引号。两者的不同之处在于library()载入之后不返回任何信息,而require()载入后则会返回TRUE,因此require()适合用于程序的书写。

例如

library(ggplto2)

>require(foreign)

Loading required package: foreign

>is.logical(require(foreign))

[1] TRUE

4. 包的更新

update.packages()

5. 包的帮助信息 格式如下,可以查看包中的函数以及说明

help(package="ggplot2")

6. 查看本地的包

6.1 查看默认加载的包,忽略基本的包

getOption("defaultPackages")

>getOption("defaultPackages")

[1] "datasets" "utils" "grDevices" "graphics" "stats" "methods"

[7] "ggplot2"

6.2 查看当前已经加载过的包

(.packages())

[1] "ggplot2" "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"

6.3 要显示所有可用的包

(.packages(all.available=TRUE))

>(.packages(all.available=TRUE))

[1] "abind" "agricolae" "aplpack" "base" "bitops"

[6] "boot" "car" "caTools" "class" "cluster"

[11] "codetools" "colorRamps" "colorspace" "compiler" "datasets"

[16] "Defaults" "devtools" "dichromat" "digest" "doBy"

[21] "e1071" "effects" "ellipse" "evaluate" "foreign"

[26] "formatR" "Formula" "gdata" "ggplot2" "ggthemes"

[31] "gmodels" "gplots" "graphics" "grDevices" "grid"

[36] "gtable" "gtools" "highr" "Hmisc" "httr"

[41] "KernSmooth" "knitr" "labeling" "lattice" "latticeExtra"

[46] "leaps" "lme4" "lmtest" "LSD" "manipulate"

[51] "markdown" "MASS" "Matrix" "matrixcalc" "memoise"

[56] "methods" "mgcv" "minqa" "multcomp" "munsell"

[61] "mvtnorm" "nlme" "nnet" "nortest" "parallel"

[66] "pixmap" "plyr" "proto" "psych" "quantmod"

[71] "Rcmdr" "RColorBrewer" "Rcpp" "RcppEigen" "RCurl"

[76] "relimp" "reshape2" "rgl" "rJava" "RODBC"

[81] "rpart" "rstudio" "samplesize" "sandwich" "scales"

[86] "schoolmath" "sciplot" "sem" "spatial" "splines"

[91] "stats" "stats4" "stringr" "survival" "tcltk"

[96] "tcltk2" "TH.data" "tools" "TTR" "utils"

[101] "VennDiagram" "whisker" "XLConnect" "xts" "zoo"

7. 卸载包detach(),这是library()的反向操作,此操作主要是为了避免某些包中的函数名称相同,造成冲突,注意与library()的参数不同,detach()参数为detach(package:包的名称),library(包的名称)。

例如

>library(ggplot2) #加载包

>(.packages()) #列出当前已经加载的包

[1] "ggplot2" "stats" "graphics" "grDevices" "utils" "datasets"

[7] "methods" "base"

>detach(package:ggplot2) # 卸载ggplot2包

>(.packages()) #列出当前已经加载的包

[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"

[7] "base"

8. 自定义启动时候的加载包

如果需要长期使用某个包的话,每次开启都需要输入library(),比较麻烦,因此可以让R启动时自动加载某些包。在R的安装目录/etc/Rprofile.site加入下载语句:

例如让R启动时自动加载ggplot2包

local({old <- getOption("defaultPackages")

options(defaultPackages = c(old, "ggplot2"))})

9. 在文章中引用R软件包,例如引用ggplot2包:

citation(package="ggplot2")

To cite ggplot2 in publications, please use:

H. Wickham. ggplot2: elegant graphics for data analysis. Springer New

York, 2009.

A BibTeX entry for LaTeX users is

@Book{,

author = {Hadley Wickham},

title = {ggplot2: elegant graphics for data analysis},

publisher = {Springer New York},

year = {2009},

isbn = {978-0-387-98140-6},

url = {http://had.co.nz/ggplot2/book},

}

第一步:安装合适版本的R软件,安装对应版本的Rtools,安装Rstudio,安装Miketex软件(这些软件均可从官网下载)。 第二步:打开R软件,选择镜像,安装“devtools”,具体可从R——程序包——安装程序包上进行,也可以使用install.packages("devtools"...

在编译R之前,需要通过yum安装以下几个程序:

#yum install gcc-gfortran #否则报”configure: error: No F77 compiler found”错误

#yum install gcc gcc-c++ #否则报”configure: error: C++ preprocessor “/lib/cpp” fails sanity check”错误

#yum install readline-devel #否则报”–with-readline=yes (default) and headers/libs are not available”错误

#yum install libXt-devel #否则报”configure: error: –with-x=yes (default) and X11 headers/libs are not available”错误

然后下载源代码,编译

[root@192 ~]# wget http://ftp.ctex.org/mirrors/CRAN/src/base/R-3/R-3.1.1.tar.gz

--2014-08-29 23:45:18-- http://ftp.ctex.org/mirrors/CRAN/src/base/R-3/R-3.1.1.tar.gz

Resolving ftp.ctex.org... 159.226.47.4

Connecting to ftp.ctex.org|159.226.47.4|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 28606569 (27M) [application/x-gzip]

Saving to: “R-3.1.1.tar.gz”

100%[+++++++++++++++++++++++++++++++++++++=>] 28,606,569 9.86K/s in 1m 49s

2014-08-30 03:03:40 (6.78 KB/s) - “R-3.1.1.tar.gz” saved [28606569/28606569]

[root@192 ~]#tar zxvf R-3.1.1.tar.gz

R-3.1.1/doc/manual/images/QQ.png

R-3.1.1/doc/html/about.html

R-3.1.1/doc/html/favicon.ico

R-3.1.1/doc/html/index-default.html

R-3.1.1/doc/html/left.jpg

R-3.1.1/doc/html/logo.jpg

R-3.1.1/doc/html/logosm.jpg

R-3.1.1/doc/html/Makefile.in

R-3.1.1/doc/html/NEWS.2.html

R-3.1.1/doc/html/NEWS.html

R-3.1.1/doc/html/packages-head-utf8.html

R-3.1.1/doc/html/R-admin.html

R-3.1.1/doc/html/R.css

R-3.1.1/doc/html/resources.html

R-3.1.1/doc/html/right.jpg

R-3.1.1/doc/html/Search.html

R-3.1.1/doc/html/SearchOn.html

R-3.1.1/doc/html/up.jpg

[root@192 ~]# cd R-3.1.1

[root@192 ~]# ./configure

R is now configured for x86_64-unknown-linux-gnu

Source directory: .

Installation directory:/usr/local

C compiler:gcc -std=gnu99 -g -O2

Fortran 77 compiler: gfortran -g -O2

C++ compiler: g++ -g -O2

C++ 11 compiler: g++ -std=c++0x -g -O2

Fortran 90/95 compiler:gfortran -g -O2

Obj-C compiler:

Interfaces supported: X11

External libraries:readline

Additional capabilities: NLS

Options enabled: shared BLAS, R profiling

Recommended packages: yes

configure: WARNING: you cannot build info or HTML versions of the R manuals

configure: WARNING: you cannot build PDF versions of the R manuals

configure: WARNING: you cannot build PDF versions of vignettes and help pages

[root@192 R-3.1.1]# make

make[1]: Entering directory `/root/R-3.1.1/m4'

make[1]: Nothing to be done for `R'.

make[1]: Leaving directory `/root/R-3.1.1/m4'

make[1]: Entering directory `/root/R-3.1.1/tools'

make[1]: Nothing to be done for `R'.

make[1]: Leaving directory `/root/R-3.1.1/tools'

make[1]: Entering directory `/root/R-3.1.1/doc'

trying to compile and link a JNI progam

detected JNI cpp flags:

detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm

make[2]: Entering directory `/tmp/Rjavareconf.9RgDis'

gcc -std=gnu99 -I/root/R-3.1.1/include -DNDEBUG -I/usr/local/include-fpic -g -O2 -c conftest.c -o conftest.o

conftest.c:1:17: error: jni.h: No such file or directory

conftest.c: In function ‘main’:

conftest.c:4: warning: implicit declaration of function ‘JNI_CreateJavaVM’

make[2]: *** [conftest.o] Error 1

make[2]: Leaving directory `/tmp/Rjavareconf.9RgDis'

Unable to compile a JNI program

JAVA_HOME: /usr/lib/jvm/Java-1.7.0-openjdk-1.7.0.65.x86_64/jre

Java library path:

JNI cpp flags:

JNI linker flags :

Updating Java configuration in /root/R-3.1.1

Done.

make[1]: Leaving directory `/root/R-3.1.1'

[root@192 R-3.1.1]# make install

installing packages ...

building HTML index ...

make[2]: Leaving directory `/root/R-3.1.1/src/library'

make[1]: Leaving directory `/root/R-3.1.1/src'

make[1]: Entering directory `/root/R-3.1.1/tests'

make[1]: Nothing to be done for `install'.

make[1]: Leaving directory `/root/R-3.1.1/tests'

到此,安装完毕!

测试:

[root@192 R-3.1.1]# R

R version 3.1.1 (2014-07-10) -- "Sock it to Me"

Copyright (C) 2014 The R Foundation for Statistical Computing

Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

>head(iris)

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

1 5.1 3.5 1.4 0.2 setosa

2 4.9 3.0 1.4 0.2 setosa

3 4.7 3.2 1.3 0.2 setosa

4 4.6 3.1 1.5 0.2 setosa

5 5.0 3.6 1.4 0.2 setosa

6 5.4 3.9 1.7 0.4 setosa

尝试安装shiny包

>install.packages("shiny")

Selection: 20

also installing the dependencies ‘Rcpp’, ‘bitops’, ‘httpuv’, ‘caTools’, ‘RJSONIO’, ‘xtable’, ‘digest’, ‘htmltools’

trying URL 'http://mirror.bjtu.edu.cn/cran/src/contrib/Rcpp_0.11.2.tar.gz'

Content type 'application/octet-stream' length 2004313 bytes (1.9 Mb)

opened URL

==================================================

downloaded 8734 bytes

trying URL 'http://mirror.bjtu.edu.cn/cran/src/contrib/httpuv_1.3.0.tar.gz'

Content type 'application/octet-stream' length 423739 bytes (413 Kb)

opened URL

>library(shiny)

>runExample("01_hello")

Listening on http://127.0.0.1:7964

测试完毕!