Dec 21, 2014

R - Install Packages

Do by learning. I think you have learned programming like C#, Java, or C++ so that the following article is setting a problem to learn by example.
How to install packages in my R environment? 
http://upload.wikimedia.org/wikipedia/commons/c/c0/MaPlot-edgeR.smear-wikipedia.png


Of course, the R GUI is easy to use so that I can find "Packages\install package(s) ..." to select desired package and install.
1. Select CRAN mirror site
I choose "Taiwan (Taipei)" mirror site.
2. Select desired packages (use Ctrl to select several packages at the same installation)
I think there are hundreds packages, it's really tedious if I want to select 20 packages among them. Thinking I want to build my "R environment" on several machines, it's a stupid work.
When you are doing stupid and tedious works using computer, there should be better solutions for you to make you convenient and become intelligent.
First, Google "auto install package for R" and get valuable information for installing R packages and set the library environment.
"rownames(installed.packages())" shows the default installed R packages.
R's default packages
To install some desired packages into R:

Define default repository
First, select one mirror site as the download repository. The example indicates "http://cran.r-project.org" as the repo.
## Default repolocal({r <- getOption("repos")       r["CRAN"] <- "http://cran.r-project.org"        options(repos=r)})
List of mirror sites for installing R packages. Users from Taiwan can specify "http://cran.csie.ntu.edu.tw/" for rapid downloads and installations.
## Default repolocal({r <- getOption("repos")       r["CRAN"] <- "http://cran.csie.ntu.edu.tw/       options(repos=r)})

The other method to define default repository is "chooseCRANmirror()". The "arg=0" shows all available world-wide mirrors. Index value is applied to use the mirror (in this example: 80: Taiwan (Taipei) ).
> chooseCRANmirror(0)
CRAN mirror 

  1: 0-Cloud                        2: Argentina (La Plata)           3: Argentina (Mendoza)          4: Australia (Canberra)           5: Australia (Melbourne)          6: Austria                      7: Belgium                        8: Brazil (BA)                    9: Brazil (PR)                 10: Brazil (RJ)                   11: Brazil (SP 1)                 12: Brazil (SP 2)               13: Canada (BC)                   14: Canada (NS)                   15: Canada (ON)                 16: Canada (QC 1)                 17: Canada (QC 2)                 18: Chile                       19: China (Beijing 1)             20: China (Beijing 2)             21: China (Hefei)               22: China (Lanzhou)               23: China (Xiamen)                24: Colombia (Bogota)           25: Colombia (Cali)               26: Czech Republic                27: Denmark                     28: Ecuador                       29: El Salvador                   30: Estonia                     31: France (Lyon 1)               32: France (Lyon 2)               33: France (Montpellier)        34: France (Paris 1)              35: France (Paris 2)              36: France (Strasbourg)         37: Germany (Berlin)              38: Germany (Bonn)                39: Germany (Goettingen)        40: Germany (Frankfurt)           41: Germany (M羹nster)            42: Greece                      43: Hungary                       44: Iceland                       45: India                       46: Indonesia (Jakarta)           47: Indonesia (Jember)            48: Iran                        49: Ireland                       50: Italy (Milano)                51: Italy (Padua)               52: Italy (Palermo)               53: Japan (Hyogo)                 54: Japan (Tokyo)               55: Japan (Tsukuba)               56: Korea (Seoul 1)               57: Korea (Seoul 2)             58: Korea (Ulsan)                 59: Lebanon                       60: Mexico (Mexico City)        61: Mexico (Texcoco)              62: Netherlands (Amsterdam)       63: Netherlands (Utrecht)       64: New Zealand                   65: Norway                        66: Philippines                 67: Poland                        68: Portugal                      69: Russia                      70: Singapore                     71: Slovakia                      72: South Africa (Cape Town)    73: South Africa (Johannesburg)   74: Spain (A Coru簽a)             75: Spain (Madrid)              76: Sweden                        77: Switzerland                   78: Taiwan (Chungli)            79: Taiwan (Taichung)             80: Taiwan (Taipei)               81: Thailand                    82: Turkey                        83: UK (Bristol)                  84: UK (Cambridge)              85: UK (London)                   86: UK (London)                   87: UK (St Andrews)             88: USA (CA 1)                    89: USA (CA 2)                    90: USA (IA)                    91: USA (IN)                      92: USA (KS)                      93: USA (MD)                    94: USA (MI)                      95: USA (MO)                      96: USA (OH)                    97: USA (OR)                      98: USA (PA 1)                    99: USA (PA 2)                 100: USA (TN)                     101: USA (TX 1)                   102: USA (WA 1)                 103: USA (WA 2)                   104: Venezuela                    105: Vietnam                    


Selection: 80

Use "chooseCRANmirror(80)" to directly set my nearest repository.

Then, you should know what packages that you want. For example, I ask me the question:
Can I use R to connect to database? 
Therefore, Google "R connect to sql server" to get valuable keywords, RODBC and RSQLServer, that may be R package names. Similar queries can be tried in Google search.
  • Google "r connect to db": DBI, RODBC, RMySQL, etc.
  • Google "r connect to SQL Server": RSQLServer
I also found the method "install.packages('RODBC')" that installs packages defined in a character vector. Then, test the RSQLServer package with sample codes. 
install.packages('RODBC')
install.packages('DBI')
install.packages('RSQLServer')

Set repository and install packages
The JDK must be installed, otherwise some problems will occur in following SQL connection codes.
http://download.oracle.com/otn-pub/java/jdk/8u25-b18/jdk-8u25-windows-x64.exe
To test SQL connection to MS SQL Server, following codes are used:
library(RODBC)dbhandle <- odbcDriverConnect('driver={SQL Server};server=localhost;database=master;trusted_connection=true')res <- sqlQuery(dbhandle, 'select * from information_schema.tables')

To connect to SQL with uid & pwd:
dbhandle <- odbcDriverConnect('driver={SQL Server};server=localhost;database=DBName;uid=xxx;pwd=****')
I found RSQLServer package is not needed to install. To uninstall packages, "remove.packages('RSQLServer')" is employed to remove useless packages.

Upgrade new version and recheck installed packages: when R GUI has new version, installed libraries can be checked and upgraded with following steps.
  • copy any installed packages to the library folder in the new installation
  • > update.packages(checkBuilt=TRUE, ask=FALSE)
For examples, copy (say) R\win-library\3.0 to R\win-library\3.1 before running update.packages(checkBuilt=TRUE, ask=FALSE)

Up to now, R is easy to use like Python.

No comments :

Post a Comment