Skip to main content

Posts

Showing posts from 2014

R and C++ integration: my first baby steps using inline

Hi, Really excited to try out stuff in Dirk's book. The first chapter of the book gives a gentle introduction to Rcpp and inline package. The problem setting is how to integrate a fibonacci recursive function in C++, compile it and execute it in R. Benchmarking results are also shown. The program code is all below in one R file. Its the same code in the book under listing 1.2 library(inline) library(rbenchmark) library(Rcpp) #inline function for fibonacci algorithm in C++ incltxt <- ' int fibonacci(const int x) {   if (x == 0) return(0); if (x == 1) return(1);   return fibonacci(x - 1) + fibonacci(x - 2); }' #wrapper function fibRcpp <- cxxfunction(signature(xs="int"),                        plugin="Rcpp",                        incl=incltxt,                         body=' ...

R benchmarking using proc.time() and rbenchmark package

I am the proud owner of Dirk's book on Seamless R and C++ Integration. Decided to dig right in and get into details on how to benchmark code from the first chapter on. Suppose you want to benchmark the following piece of R fibonacci code (1) fibR <- function(n) {      if(n == 0) return(0)      if(n == 1) return(1)      return (fibR(n - 1) + fibR(n - 2)) } You can use the good ol' proc.time() function like this (2) a <- proc.time() fibR(30) proc.time() - a Whose output will look something like this (3) user system elapsed 8.27 0.00 8.30 What this means is that the program took 8.27 user seconds to run the code and used 0 system seconds on resources in that time.  Ideally you need to add the user and system times to get the full picture of how long your code took to run. The elapsed time denotes the time it took for the day to go by between the first line of code in (2) to the 3rd line of code in (2). I...

Modify elements in a list using rapply

Using rapply one can write a function which runs over elements in a list returning a list. # Here I create a list of 10 normal random numbers testList <- list() testList[[1]] <- rnorm(10) testList[[2]] <- rnorm(10) # Here I replace all numbers which are < 0 by 0 newList <- rapply(testList, function(x) {ifelse(x<0,0,x)}, how = "list") Also if the data in the list is not a vector but a matrix newList <- function="" ifelse="" lapply="" p="" sapply="" testlist="" x="" y=""> # Here I add all elements using the Reduce function Reduce("+",newList)

Step by step instructions to introduce Latex equations in R markdown into Google Blogger

Here are step by step instructions to publish latex equations into google blooger account blogs using R. I was trying to figure out how to do this using copy and paste of the html generated from the knit2html function. Unfortunately this does not work. It turns out that there is a simple fix between the copy and paste phase where one has to modiy the script used for MathJax on the webpage for \(\LaTeX\) to work. Note that for my examples I have used examples from \(\LaTeX\) for Complete Novices by Nicola. L. C. Talbot. Suppose this is the Latex text that I want to publish on Google Blogger. This is created in RStudio as a simple Rmd file. ------------------------------------------- A linear equation in line $y = mx + c$ A linear equation with equation references Equation ~\ref{eqn:linear} is a linear function \begin{equation} \label{eqn:linear} f(x) = mx + c \end{equation} If you convert this to html using the knit2html function the resulting out...

Using R to publish to google blogger

I got frustrated with trying to get R html to show properly on my Wordpress.com blog as images dont show up at all in the blog. So I checked out Google Blogger and found no hassles there. A step by step guide to create the following webpage http://nevilleandrade.blogspot.de/2014/10/testing-r-using-google-blogger.html The steps I followed were (Windows) 1. Open RStudio and create a new Rmd file. 2. Knit to html using either the button "Knit HTML" or run knit2html("yourRMD.Rmd") on command line. 3. Open the html file created using notepad, copy the contents. 4. Create a new post on Google blogger. 5. Switch to html composition mode. 6. You will find the following content in the beginning <div dir="ltr" style="text-align: left;" trbidi="on"> </div> Do not remove this stuff other wise the link to the blog gets funny rather than the actual name of the blog. 7. Put a title for the blog. 8. Paste the conte...

Testing R using Google blogger

Title Title This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the Help toolbar button for more details on using R Markdown). When you click the Knit HTML button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: 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 You can also embed plots, for example: plot(cars)