Sunday, February 16, 2014

xtable in knitr

xtable in knitr
Here i am going to give an example of xtable package in knitr. We use cars data which comes default through the R
The summary data of cars:
summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
```{r table1, results='asis'} 
library(xtable)
data(cars)
print(xtable(summary(cars), caption = "Cars Data",
floating = FALSE, label = "tab:coef"), type = "html", include.rownames = F)
```
library(xtable)
data(cars)
print(xtable(summary(cars), caption = "Cars Data", floating = FALSE, label = "tab:coef"), 
    type = "html", include.rownames = F)
Cars Data
speed dist
Min. : 4.0 Min. : 2
1st Qu.:12.0 1st Qu.: 26
Median :15.0 Median : 36
Mean :15.4 Mean : 43
3rd Qu.:19.0 3rd Qu.: 56
Max. :25.0 Max. :120
The outcome in your html file is:
## <!-- html table generated in R 2.15.1 by xtable 1.7-1 package -->
## <!-- Sun Feb 16 23:55:02 2014 -->
## <TABLE border=1>
## <CAPTION ALIGN="bottom"> Cars Data </CAPTION>
## <TR> <TH>     speed </TH> <TH>      dist </TH>  </TR>
##   <TR> <TD> Min.   : 4.0   </TD> <TD> Min.   :  2   </TD> </TR>
##   <TR> <TD> 1st Qu.:12.0   </TD> <TD> 1st Qu.: 26   </TD> </TR>
##   <TR> <TD> Median :15.0   </TD> <TD> Median : 36   </TD> </TR>
##   <TR> <TD> Mean   :15.4   </TD> <TD> Mean   : 43   </TD> </TR>
##   <TR> <TD> 3rd Qu.:19.0   </TD> <TD> 3rd Qu.: 56   </TD> </TR>
##   <TR> <TD> Max.   :25.0   </TD> <TD> Max.   :120   </TD> </TR>
##    <A NAME=tab:coef></A>
## </TABLE>
The rest, you are able to add some style by using CSS…

Wednesday, February 12, 2014

How to upgrade R version on Ubuntu

There are lots of directives on the web how to upgrade R version or installing the latest version of R. Here I am going to tell the clearest and success way that I have done.

Open your terminal (Ctrl+Alt+T) 
1. Uninstall current R version on your system
sudo apt-get remove r-base-core
2. Add cran.rstudio deb to sources.list
sudo nano /etc/apt/sources.list    
Paste the below code in to your sources.list
deb http://cran.rstudio.com/bin/linux/ubuntu precise/
My version is "precise".  That's why the line ends with "precise". You can learn your Ubuntu version:
lsb_release -a
Codename is your version....

3. Add key to sign CRAN packages
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
4. Add PPA (for CRAN) to your system
sudo add-apt-repository ppa:marutter/rdev
sudo apt-get update
sudo apt-get upgrade
5. Install R again 
sudo apt-get install r-base
If you want to check the version of your R. Open terminal, enter R and type:
version
That's all.....