#------------------------------------------------
# set options
options(editor=paste(getwd(),"/notepad2.exe",sep=""))
options(help_type="html")
options(show.signif.stars=FALSE)
options(stringsAsFactors=FALSE)
options("repos" = c(CRAN = "http://cran.rstudio.com/"))
#------------------------------------------------
# load libraries
library("ggplot2")
#------------------------------------------------
# create environment MyEnv and load various functions
.MyEnv <- new.env()
.MyEnv$h <- utils::head
.MyEnv$ht <- function(d) rbind(head(d,10),tail(d,10))
.MyEnv$hh <- function(d) if(class(d)=="matrix"|class(d)=="data.frame") d[1:5,1:5]
.MyEnv$sc <- function() source("clipboard")
.MyEnv$dp <- function(x) { dump(x,"clipboard") }
.MyEnv$ip <- function(x) { install.packages(x,lib="C:\\R\\library");library(x,character.only =T) }
.MyEnv$mcat <- function (a) {cat(paste(paste(a,collapse=" "),"\n"))}
.MyEnv$trw <- function(n,low=0,high=1e5){
require(random)
c(randomNumbers(n,low,high))
}
.MyEnv$gout <-
function(f,foldername,graphname,format="png") {
f()
if(nchar(foldername)==4) #graph for one of my courses
file=paste("C:/Users/Owner/Dropbox/teaching/",foldername,
"/web/graphs/",graphname,".",format,sep="")
else
file=paste(foldername,graphname,".",format,sep="")
mcat(file)
if(format=="png") png(file)
if(format=="pdf") pdf(file)
if(format=="ps") postscript(file, horizontal = F, pointsize = 17)
if(format=="eps") {
setEPS()
postscript(file, horizontal = F, pointsize = 17)
}
f()
dev.off()
}
.MyEnv$mh <-
function (x,f,par,n)
{
if(missing(n)) n <- 2*sqrt(length(x))
hist(x,n, freq=F, main="")
if(!missing(f)) {
df <- get(paste("d",f,sep=""))
x <- seq(min(x),max(x),length=100)
if(length(par)==1) y <- df(x,par)
else y <- df(x,par[1],par[2])
lines(x,y,lwd=2)
}
NULL
}
.MyEnv$eplot <- function () {plot(c(0,1),c(0,1),axes=F,xlab="",ylab="",type="n") }
.MyEnv$dpf <- function (f) {
dp(as.character(substitute(f)))
sp <- " "
g <- scan("clipboard", what = "character", sep = "\n")
n <- length(g)
for (i in 1:n) {
k <- 0
repeat {
k <- k + 1
if (substring(g[i], k, k) != " ")
break
}
if (k > 1) {
g[i] <- substring(g[i], first = k)
g[i] <- paste(paste(rep(sp, k - 1), collapse = ""),
g[i], collapse = "", sep = "")
}
g[i] <- gsub("<","<",g[i])
g[i] <- paste(g[i], "
", collapse = "", sep = "")
}
write(g, "clipboard", ncol = 1)
NULL
}
.MyEnv$r_tbls <- function (x,which="h",digits=2) {
n <- dim(x)[1]
m <- dim(x)[2]
for(i in 1:m) if(is.numeric(x[,i])) x[,i]<- round(x[,i],digits)
DoRowNames <- TRUE
if(length(rownames(x)[1])==0) DoRowNames <- FALSE
else if(rownames(x)[1] == "1") DoRowNames <- FALSE
if(which=="h") {
top <- "
") else nameline <- c(" | |
",colnames(x)[i]," | "),collapse="") nameline <- paste(nameline,"|
---|---|
",rownames(x)[j]," | "),collapse="") for(i in 1:m) middle[j] <- paste(middle[j],paste("",x[j,i]," | "),collapse="") middle[j] <- paste(middle[j],"