In this short post, you find the user-specific R configuration file, where you can place more personal functions preferences, shortcuts, aliases, and hacks that are used regularly. You can also download the .RBase file from my Github repository, which is linked to this webpage.
I use the user-specific .Rprofile to source functions whenever I start RStudio.
To setup you RStudio start-up follow this 4 steps:
You may find the .Rprofile file in your Documents folder. Just replace it with my file at Github.
Download the Base.R file store it, wherever it is convenient for you, as for instance Google drive.
Change the path for Base.R accordingly in .Profile.
Now you may add functions, which you want to load automatically, in your Base.R.
Done
Usually, R searches for the .R Profile file in 3 directories:
Go to your Home Directory of Windows or Mac
Working Directory: The simplest way is to change the working directory, by going to “RStudio -> Tools -> GlobalOptions -> General -> Default Working Directory”. I changed it to my specific cloud where I placed both files: .Rprofile and Base.R
Change the Library folder to the cloud in the .Rprofile.
This is quite useful if you work on several computers and want to install a package only once. In order to sync so to say you R installed packages, just copy the Documents/win-Library to your cloud and change the .Rprofile accordingly. Afterwards you can delete the packages in Documents/library on the local machine.
The functions below and settings are loaded automatically in every start-up. For instance, the first two functions let you easily copy data from Excel to R and vice versa. Actually, it also works for any clipboard content, lets say a data table from a webpage, etc. Depending on the separator, the data is split. You can change this parameter, as well as the header and decimal point seperator.
#######Useful functions###############
#Copy Clipboard from Excel to R
read.excel <- function(header=FALSE, sep="\t", dec=".") {
read.table("clipboard",sep=sep,header=header, dec=dec)
}
#Paste Clipboard from R to Excel
write.excel <- function(x,row.names=FALSE,col.names=TRUE,seperator="\t",...) {
write.table(x,"clipboard",sep=seperator,row.names=row.names,col.names=col.names,...)
}
#Newtons method is an iterative root-finding algorithm where we start with an initial guess x0
#and each successive guess is refined using the iterative process:
#
# xn+1=xn-f(xn)/f'(xn)
#
#The algorithm will run until either the number of steps N has been reached, or
#the error tolerance |xn+1???xn|<??, where ?? is defined as the tolerance parameter tol.
#
newton <- function(f, tol=1E-12,x0=1,N=20, stepSize = 0.001) {
h <- stepSize
i <- 1; x1 <- x0
p <- numeric(N)
while (i<=N) {
df.dx <- (f(x0+h)-f(x0))/h
x1 <- (x0 - (f(x0)/df.dx))
p[i] <- x1
i <- i + 1
if (abs(x1-x0) < tol) break
x0 <- x1
}
return(p[1:(i-1)])
}
#get packages: install them if necessary and load them
getPackages <- function(x){
for( i in x ){
i = gsub(".*/", "", i)
# require returns TRUE invisibly if it was able to load package
if( ! require( i , character.only = TRUE )){
# If package was not able to be loaded then re-install
(install.packages( i , dependencies = TRUE))
# If package was not able to be installed then re-install with github
if( ! require( i , character.only = TRUE )){
(devtools::install_github( i , dependencies = TRUE ))
print("Github Installation!")
}
}
# Load package
suppressPackageStartupMessages(require( i , character.only = TRUE ))
}
}
###Update R, RStudio, Github,...
#suppressPackageStartupMessages(library(installr))
#updateR()
#check.for.updates.R()
#do not save after q()
# q <- function (save = "n", status = 0, runLast = TRUE)
# .Internal(quit(save, status, runLast))
local({r <- getOption("repos")
r["CRAN"] <- "https://cran.wu.ac.at/"
options(repos=r)})
options(stringsAsFactors=FALSE)
options(max.print=100)
options(scipen=10)
options(editor="vim")
# options(show.signif.stars=FALSE)
options(menu.graphics=FALSE)
options(prompt="> ")
options(continue="... ")
options(width = 80)
utils::rc.settings(ipck=TRUE)
.env <- new.env()
attach(.env)
.env$unrowname <- function(x) {
rownames(x) <- NULL
x
}
.env$unfactor <- function(df){
id <- sapply(df, is.factor)
df[id] <- lapply(df[id], as.character)
df
}
message("Successfully loaded .Rprofile")
## Successfully loaded .Rprofile