Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Wednesday, July 23, 2014

How to run R from Shell script ?

http://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script
http://stackoverflow.com/questions/5391124/in-r-select-rows-of-a-matrix-that-meet-a-condition
http://stackoverflow.com/questions/7201341/how-can-2-strings-be-concatenated-in-r

I made two files: exmpl.bat and exmpl.r.
  • exmpl.bat:
    set R_Script="C:\Program Files\R-3.0.2\bin\RScript.exe"
    %R_Script% exmpl.R 2010-01-28 example 100 > exmpl.batch 2>&1
    Alternatively using Rterm.exe:
    set R_TERM="C:\Program Files\R-3.0.2\bin\i386\Rterm.exe"
    %R_TERM% --no-restore --no-save --args 2010-01-28 example 100 < exmpl.R > exmpl.batch 2>&1
  • exmpl.r:
    options(echo=TRUE) # if you want see commands in output file
    args <- span=""> commandArgs(trailingOnly = TRUE)
    print(args)
    # trailingOnly=TRUE means that only your arguments are returned, check:
    # print(commandsArgs(trailingOnly=FALSE))
    
    start_date <- span=""> as.Date(args[1])
    name <- span=""> args[2]
    n <- span=""> as.integer(args[3])
    rm(args)
    
    # Some computations:
    x <- span=""> rnorm(n)
    png(paste(name,".png",sep=""))
    plot(start_date+(1L:n), x)
    dev.off()
    
    summary(x)
Save both files in the same directory and start exmpl.bat. In result you got:
  • example.png with some plot
  • exmpl.batch with all what was done
Just to add - you could add environment variable %R_Script%:
"C:\Program Files\R-3.0.2\bin\RScript.exe"
and use it in your batch scripts as %R_Script% .......
Differences between RScript and Rterm:

======================================================================
> colnames <- c="" col1="" col3="" div="" nbsp="">
> data[ , colnames] 
  col1 col3
1    1    4
2    2    5
3    3    6
> data[ , colnames] 
  col1 col3
1    1    4
2    2    5
3    3    6
> r <- colnames="" data="" div="" nbsp="">
> r
  col1 col3
1    1    4
2    2    5
3    3    6
> r[r$col1 == 2,]
  col1 col3
2    2    5
> # this is a comment line
> r[r$col1 == 2 || r$col1==3,]
[1] col1 col3
<0 rows=""> (or 0-length row.names)
> r[r$col1 == 2,]
  col1 col3
2    2    5
> r[r$col1 == 2 | r$col1==3,]
  col1 col3
2    2    5
3    3    6

Monday, May 5, 2014

Useful R graphics link


http://stackoverflow.com/questions/14698616/when-should-i-use-geom-map

# solution 1
colnames(pointData) < - c('long','lat') # makes consistent with county_map
pointData$group < - 1 # ggplot needs a group to work with
county_map$value < - sapply(1:nrow(county_map),
                           function(x) round(runif(1, 1, 8), 0)) # for colours

ggplot(county_map, aes(x = long, y = lat, group = group)) +
    geom_polygon(aes(fill = value)) +
    coord_map() +
    geom_point(data = pointData, aes(x = long, y = lat), shape = 21, fill = "red")




# solution 2

map1 < - ggplot(countyData) +
  geom_map( map = county_map, aes(map_id = id,fill = value), 
            colour = "black") + coord_map() +
  expand_limits(x = county_map$long, y = county_map$lat)
  map1 + geom_point(mapping = aes(xx, yy), data = pointData)
 
 

Tuesday, April 29, 2014

Reite = R + Delite

Delite - http://stanford-ppl.github.io/Delite/
Relite - https://github.com/TiarkRompf/Relite (based on the FastR)
FastR - https://github.com/allr/fastr

Delite is a compiler framework and runtime for parallel embedded DSLs.
Delite provides:
- Built-in parallel execution patterns
- Optimizers for parallel code
- Code generators for Scala, C++ and CUDA
- A heterogeneous runtime for executing DSLs

With Delite, parallelized using 8 threads, we get about a 10x speedup over GNU R (and about 4x over FastR).

 
http://sandeeptata.blogspot.com/2012/08/scala-dsls-and-big-data.html

Sunday, April 6, 2014

SparkR - Spark + R


# My test SparkR program - mySparkR.R

require(SparkR)

# this does not work since I don' have a cluster setup
# sc < - sparkR.init(master="spark://david-centos6:7077", sparkEnvir=list(spark.executor.memory="1g"))
sc < - sparkR.init(master="local[2]", sparkEnvir=list(spark.executor.memory="1g"))

lines < - textFile(sc, "hdfs://david-centos6:8020/user/david/data/result.txt")
       
words < - flatMap(lines,
   function(line) {
      strsplit(line, " ")[[1]]
})

wordCount < - lapply(words, function(word) { list(word, 1L) })

counts < - reduceByKey(wordCount, "+", 2L)
output < - collect(counts)

for (wordcount in output) {
  cat(wordcount[[1]], ": ", wordcount[[2]], "\n")
}
                                
# pur the input file into HDFS
> hadoop fs -put result.txt data

# Run SparkR to test it
> ./sparkR examples/mySparkR.R
- or To increase the memory used by the driver you can  -
> SPARK_MEM=1g ./sparkR examples/mySparkR.R


NOTE: SparkUI is at http://david-centos6:4040
reference: http://stackoverflow.com/questions/21677142/running-a-job-on-spark-0-9-0-throws-error

ERROR sometimes you will be experiencing:
"Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient memory"

A simple test you can run to test if you have memory or other problems using Spark Shell
Try to run MASTER="local[2]" spark-shell on the same machine you're trying to run the code. And the same code in spark console: sc.parallelize(1 to 100).count

If the sufficient memory problem persist then you might want to try to add SPARK_WORKER_MEMORY=2g to the file tools/spark-0.9.0-incubating-bin-hadoop2/conf/spark-env.sh (Not sure if this help yet ???)



Thursday, April 3, 2014

Sublime Text 3 for R


https://github.com/wuub/SublimeREPL
SublimeREPL
  1. Install Package Control. http://wbond.net/sublime_packages/package_control
  2. Install SublimeREPL
    1. Preferences | Package Control | Package Control: Install Package
    2. Choose SublimeREPL
  3. Restart SublimeText2
  4. Configure SublimeREPL (default settings in Preferences | Package Settings | SublimeREPL | Settings - Default should be modified in Preferences | Package Settings | SublimeREPL | Settings - User, this way they will survive package upgrades!

Enhanced-R package for Sublime Text 2/3

This package helps in writing R languages:
  • More comprehensive Indentation and Syntax
  • Send commands to different applications such as R GUI, Terminal and SublimeREPL.
  • Show function hint in status bar

You can search all the packages available for Sublime from: https://sublime.wbond.net/search/sublime


Wednesday, April 2, 2014

Embedding Scala in R vs embedding R in Scala

http://dahl.byu.edu/software/jvmr/dahl-payne-uppalapati-2013.pdf

1. Embedding Scala in R
[david@david-centos6 ~]$ R

Instantiating a Scala interpreter/compiler in R is accomplished as follows:
R> library("jvmr")
R> a < -  scalaInterpreter()

Multiple interpreters can be created and each maintains its own workspace and memory.
Scala code can be evaluated using the interpret function or its shorthand equivalent. The
following two lines of code are equivalent:
R> interpret(a,'val mu = 3')
R> a['val mu = 3']

Both the interpret function and its shorthand are capable of handling multi-line code:
R> a["val sigma = 2.5
val n = 10
"]

2. Embedding R in Scala
> JVMR_JAR=$(R --slave -e 'library("jvmr"); cat(.jvmr.jar)')
> scala -cp ".:$JVMR_JAR"

scala> import org.ddahl.jvmr.RInScala
import org.ddahl.jvmr.RInScala

scala> val R = RInScala()
R: org.ddahl.jvmr.RInScala = org.ddahl.jvmr.RInScala@40726e15

scala> R.eval("words <- his="" in="" made="" r="" span="" string="" was="">

scala> println(R.capture("words"))
[1] "This String was made in R"

3. Embedding Spark in R

Thursday, March 27, 2014

R Language Installation


http://en.wikipedia.org/wiki/R_%28programming_language%29

How to install R on CentOS 6:
> sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

> sudo yum install R
 
Useful IDE:
http://www.rstudio.com/ide/download/desktop 

How to install packages for all users:

Part 1/3: First, run R as root

Launch R with sudo R, and enter your password.

Part 2/3: To install everything, just paste this single long line in R:

(Note that these packages will still NOT be available to all users until we fix it in part 3/3, because the default root permissions forbid other users from reading the installed packages.)
source("http://www.bioconductor.org/biocLite.R") ; biocLite() ; biocLite("limma",dependencies=TRUE); biocLite("statmod"); biocLite("ADaCGH", dependencies=TRUE); biocLite("arrayQuality"); biocLite("affxparser"); biocLite("makecdfenv"); biocLite("affycomp"); biocLite("multtest",dependencies=TRUE); biocLite("Genominator",dependencies=TRUE); biocLite("affy"); biocLite("HTqPCR");
install.packages(c("Agi4x44PreProcess", "aroma.affymetrix", "bitops", "caTools", "cluster", "CNTools", "cgh", "DBI", "digest", "DESeq", "getopt", "genefilter", "geneplotter", "gplots", "gtools", "grid", "gridBase", "GLAD", "GO.db", "hopach", "Hmisc", "hexbin", "IRanges", "KernSmooth", "KEGG.db", "mgcv", "methods", "matrixStats", "Matrix", "MASS", "marray", "nnet", "nlme", "org.Hs.eg.db", "preprocessCore", "pixmap", "parser", "qvalue", "qtl", "rpart", "R.huge", "R.filesets", "RColorBrewer", "RSQLite", "sandwich", "sets", "simpleaffy", "snapCGH", "spatial", "splines", "stats4", "strucchange", "survival", "tilingArray", "tcltk", "topGO", "vsn", "xterm256"), dependencies=TRUE);

Part 3/3: Finally, fix the permissions (required step!)

Now, if you installed things for all users using the sudo R command up above, you have an easy-to-solve problem---the new scripts aren't readable! They are installed by default into/usr/local/lib/R/. So you want to make everything there world-readable, and the directories world-exectuable (exectuable for directories means "can be looked at using ls").
If you skip this step, things will still work for YOU, but they will break for all other users.
Solution:
sudo chmod -R a+r /usr/local/lib/R ; sudo find /usr/local/lib/R -type d | sudo xargs chmod a+x
Now you're done installing the R package!

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

 If you want to  build your own R with the source from http://cran.rstudio.com/ make sure you do the followint to install the  required font:
- download the current package from 
http://mirrors.ctan.org/systems/texlive/tlnet/archive/inconsolata.tar.xz

- locate your personal texmf directory by

kpsewhich -var-value=TEXMFHOME

usually ~/texmf (and it may need to be created) and cd there.

- install the current version by something like

tar xf inconsolata.tar.xz

or use untar('inconsolata.tar.xz') in R;

(if there is an ls-R file in that directory, run

mktexlsr .

) then

sudo updmap --enable Map=zi4.map