R语言程序包安装成功但

Python023

R语言程序包安装成功但,第1张

你是不是用R Gui安装的,提示已经说了缺少quadprog依赖包,

用R Gui安装的话需要先把依赖包一个个安装上,比较麻烦,

可以在Rstudio上安装,

install.packages('tseries')

also installing the dependencies ‘quadprog’, ‘zoo’

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/quadprog_1.5-5.zip'

Content type 'application/zip' length 52439 bytes (51 KB)

downloaded 51 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/zoo_1.7-13.zip'

Content type 'application/zip' length 899932 bytes (878 KB)

downloaded 878 KB

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/tseries_0.10-35.zip'

Content type 'application/zip' length 327381 bytes (319 KB)

downloaded 319 KB

package ‘quadprog’ successfully unpacked and MD5 sums checked

package ‘zoo’ successfully unpacked and MD5 sums checked

package ‘tseries’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in

C:\Users\yuexf\AppData\Local\Temp\RtmpqqGpKz\downloaded_packages

>library(tseries)

‘tseries’ version: 0.10-35

‘tseries’ is a package for time series analysis and computational finance.

See ‘library(help="tseries")’ for details.

它会自动把依赖包给你安装上

conn <- dbConnect(MySQL(), dbname = "data", username="root", password="*********", host="127.0.0.1", port=3306)

username一般是root,然后dbname是你需要读取的表所在数据库的名字。

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},

}