One R Function A Day.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
1 2 3 4 5 6 7 8 9 10 11 12 | URL <- “http://stat.ethz.ch/R-manual/R-devel/library/base/html/” xml<em>1 <- readLines(URL) ptn <- ‘<a href=\”.*?.html\”>(.*?.html)</a>’ xml</em>2 <- regmatches(xml<em>1, regexec(ptn, xml</em>1))</p> <p>html.list <- NULL for (i in 1:length(xml<em>2)) { if (length(xml</em>2[[i]]) == 2) { html.list <- rbind(html.list, xml_2[[i]][2]) } } html.list <- 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.
1 2 3 4 5 6 | i = 0 browseURL(paste(URL, html.list[i + 1], sep = “”)) x <- paste(“i”, i+1, sep = “ <- “) cur_code <- readLines(parent.frame(2)$ofile) cur_code[grep(“^i “, cur_code)] <- 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:
1 | R CMD BATCH C:\Packages\base_help.R |
Any kind of criticism or commet are welcome!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.