Here I'll drop ggplot2 tricks ...
Table of Contents:
- piechart.R: getting the order of the colors right is quite a task
- themes.R: ggthemes is really useful
| library(xtable) | |
| print_df <- function(df){ | |
| print(xtable(df), type = "html", include.rownames = FALSE) | |
| } | |
Here I'll drop ggplot2 tricks ...
Table of Contents:
Super fast group by, code from: http://stackoverflow.com/questions/3685492/r-speeding-up-group-by-operations.
Especially, the id() function could prove very useful in building indices.
| insert_row <- function(existingDF, newrow, r){ | |
| r <- as.numeric(r) | |
| if (length(newrow) != ncol(existingDF)){ | |
| stop("Length of row doesn't match.") | |
| } | |
| if (r == 1){ | |
| existingDF <- rbind(newrow,existingDF) | |
| } else{ |
| library(yaml) | |
| # loads the database shema description | |
| db_list <- yaml.load_file("sample3.yaml") | |
| # create the database | |
| con <- dbConnect(SQLite(), "new_db.sqlite") | |
| # create the tables one by one | |
| for (i in 1:length(db_list)){ |
| data.files <- dir('data') | |
| for (data.file in data.files) | |
| { | |
| filename <- file.path('data', data.file) | |
| variable.name <- sub('\\.csv$', '', data.file, ignore.case = TRUE, perl = TRUE) | |
| assign(variable.name, read.csv(filename, header = TRUE, sep = ',')) | |
| } |
| # looping through vector #### | |
| library(inline) | |
| sign <- signature(x="numeric", n="integer", d="numeric") | |
| code <- " | |
| for (int i=1; i < *n; i++) { | |
| x[i] = x[i-1]*d[0] + x[i]; | |
| }" | |
| c_fn <- cfunction(sign, |
| library(R2HTML) | |
| # setup a temporaty file to store the code | |
| fileName <- 'temp.html' | |
| .HTML.file = file.path(getwd(), fileName) | |
| # make a title | |
| HTML(as.title("Title of my report"), append = FALSE) | |
| # add space | |
| HTMLhr() |
| df$g <- factor(df$g, levels = letters[4:1]) |
| # tricks with regular expressions #### | |
| # insert character after a given pattern: use \\1 | |
| sub("([[:digit:]]{4})", "\\15", "12346789") |