One R Function A Day.

February 11, 2013
By

(This article was first published on Category: R | Huidong Tian's Blog, and kindly contributed to R-bloggers)

As a R user with 5 years experience, I still have some basic functions that never used or never thought they could exist. Many times I was suprised with some elegant R functions, such like strwidth, parent.frame, etc. No one can know all R functions (and it’s unnecessary), but it’s very convenient to know some basic functions, especially those in the basic packages: base, utils, stats, etc.

I used to have a learning plan: learn one R function a day. But it was terminated because I always fail to remember it. Today, once again I found a useful function of the basic package base, which impelled me to keep learning R as a daily work. How could one guy who said he has 5 years experience but still doesn’t know some basic functions?!

My plan is simple: everyday I log on my computer, one of the first tasks is reading one topic-help document. To avoid forgetting to do this, I need the help page appears automatically.

So, I need to build a topic list first, and I want to start with the base package. The following is my code:

Topic List
1
2
3
4
5
6
7
8
9
10
11
12
URL &lt;- “http://stat.ethz.ch/R-manual/R-devel/library/base/html/xml<em>1 &lt;- readLines(URL)
ptn &lt;-&lt;a href=\”.*?.html\”&gt;(.*?.html)&lt;/a&gt;xml</em>2 &lt;- regmatches(xml<em>1, regexec(ptn, xml</em>1))</p>

<p>html.list &lt;- NULL
for (i in 1:length(xml<em>2)) {
  if (length(xml</em>2[[i]]) == 2) {
    html.list &lt;- rbind(html.list, xml_2[[i]][2])
  }
}
html.list &lt;- sort(unique(html.list[-which(html.list ==00Index.html”)]))

Then I need to choose an order to load the topic one by one. The function parent.frame(2)$ofile is used to get current script’s absolute path.

One by One
1
2
3
4
5
6
i = 0
browseURL(paste(URL, html.list[i + 1], sep = “”))
x &lt;- paste(“i”, i+1, sep =&lt;-)
cur_code &lt;- readLines(parent.frame(2)$ofile)
cur_code[grep(^i “, cur_code)] &lt;- x
writeLines(cur_code, parent.frame(2)$ofile)

The last step is creating a .bat file to run the R code in a batch mode, and add that file to your Task Scheduler and set the trigger as “at log on”. The content of the .bat file looks like this:

Batch Mode
1
 R CMD BATCH C:\Packages\base_help.R

Any kind of criticism or commet are welcome!

To leave a comment for the author, please follow the link and comment on his blog: Category: R | Huidong Tian's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.