1 Whtat is R?

1.1 History

  • 1980 from S (AT&T Bell Laboratories)
  • Created by Ross Ihaka and Robert Gentleman, University of Auckland, New Zealand
  • Developed currently by R Development Core Team

1.2 For What?

  • Statistical computing
  • Data analysis
  • Graphics

2 Why is R?

4 Simple examples

4.2 ls

## 
## Call:
## lm(formula = y ~ x - 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.12770 -0.77127 -0.01196  0.56726  2.78710 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x1  1.04006    0.07207  14.431   <2e-16 ***
## x2  2.03310    0.07661  26.537   <2e-16 ***
## x3  0.03523    0.07773   0.453    0.651    
## x4  3.01407    0.07668  39.309   <2e-16 ***
## x5  0.02877    0.07487   0.384    0.701    
## x6  0.03364    0.07682   0.438    0.662    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.063 on 194 degrees of freedom
## Multiple R-squared:  0.9289, Adjusted R-squared:  0.9267 
## F-statistic: 422.4 on 6 and 194 DF,  p-value: < 2.2e-16

## 7 x 1 sparse Matrix of class "dgCMatrix"
##                        1
## (Intercept) -0.006773416
## V1           0.865268570
## V2           1.859444315
## V3           .          
## V4           2.806069225
## V5           .          
## V6           .

4.3 glm

## 
## Call:  glm(formula = y ~ x, family = gaussian)
## 
## Coefficients:
## (Intercept)           x1           x2           x3           x4  
##    0.673970     0.075858     0.168674    -0.049978     0.270509  
##          x5           x6  
##   -0.038466    -0.007128  
## 
## Degrees of Freedom: 199 Total (i.e. Null);  193 Residual
## Null Deviance:       43.88 
## Residual Deviance: 24.35     AIC: 162.4

## 7 x 1 sparse Matrix of class "dgCMatrix"
##                      1
## (Intercept)  1.1018865
## V1           0.2996526
## V2           0.9561836
## V3          -0.1174316
## V4           1.6072234
## V5           .        
## V6           .

5 Visualization

5.3 ggvis plot

## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

5.6 More amazing plots:

5.8 What else?

  • Animation
  • SVM: e1071
  • Optimization: DEoptim, quadqrog, linprog, CVXR, RcppDE
  • Parallel: doParallel, Rmpi, snow, multicore, RcppParallel
  • Rmarkdown
  • Deep learning: tf & keras
  • Workflowr
  • Distributed computation: sparklyr, SuperR

6 R package

6.1 How many are they?

  • more than 15,000 (to Sep. 2018)

6.4 Can I unzip them?

  • Yes, you can unzip them and
  • repack them

7 Developing R packages

7.1 How to develop them?

  • Rstudio
  • R command-line:

7.2 How to pack them?

  • cmd (windows) or terminal (MacOS):

7.3 Where and How to share them?

8 Example: FactSum

8.1 Summary

R package "FactSum" calculates the factorial of a large integer, which may be much greater than the maximum memory of any data type. FactSum implements dramatically fast. It takes only 0.45 seconds to cumpute 10000! (it approximates 2.8E+35660), and 0.98 seconds to compute 10000! and sum=1!+2!+3!+...+10000! simultaneously. It takes only one minute to cumpute 100000! (it approximates 2.8E+456574), and less then two minutes to compute 10000! and sum=1!+2!+3!+...+100000! simultaneously.

8.3 Usage

8.4 Example

## $fact
## [1] "788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000"
## 
## $len_fact
## [1] 375
## 
## $fact_sum
## [1] "792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313"
## 
## $len_sum
## [1] 375
## 
## $nzeros
## [1] 49

8.5 Development

8.6 See also sqrtn

9 External languages

9.1 How to call C/C++ or FORTRAN

  • Rtools
  • Interface to compile code (For C/C++)
  • foo.dll (Windows) or foo.so (MacOS):
  • compile in Rpk

9.2 How to use Rcpp?