What's in your .Rprofile?___

#1
Hi all. Here I am, just finished with finals for the semester and I'm nerding out on R. Being fairly new to R, I thought it would be nice to learn more about the ins and outs of the .Rprofile file. Please share your .Rprofile or give some hints, suggestions, or guidelines regarding good habits and syntax in this area. I'll start:

Code:
.First <- function() {
   library(reshape2)
   library(plyr)
   library(ggplot2)
   library(systemfit)
   library(xtable)
   library(graphics)
   library(tikzDevice)


   setwd("/Users/NaRoGo/Desktop/RFun/")

   options(error=recover)
   options(prompt="Arrgh!> ", digits=4, show.signif.stars=FALSE)
   options(showWarnCalls=T, showErrorCalls=T)
   options(stringsAsFactors=FALSE)
   options(repos=c("http://cran.cnr.Berkeley.edu","http://cran.stat.ucla.edu"))
   #For tikzDevice caching 
   options( tikzMetricsDictionary='/Users/NaRoGo/.tikzMetricsDictionary' )
}

  
amscols <- function(x){ 
	colnames(x) <- paste( "$" , colnames(x) , "$" , sep = "" )
	x}

   
cat("\nLet's get started!", date(), "\n")



.Last <- function() {
        if (!any(commandArgs()=='--no-readline') && interactive()){
                require(utils)
                try(savehistory(Sys.getenv("R_HISTFILE")))
        }
}

I'm not 100% all my options are optimal or even work correctly. But I put together some things I thought were useful.

btw i'm using a macbook pro with leopard, R version 2.15.2 and Rstudio version 0.95.265 - newer versions don't support leopard.

Thanks for any Rdvice, Rxpertice, and Rxperience in Rdvance!
 

Dason

Ambassador to the humans
#2
It's not a bad idea to wrap loading of packages in if(interactive()){blahblahblah}

Code:
.First <- function(){
    # Use main CRAN mirror
    options(repos = c(CRAN = "http://cran.r-project.org/"))
    
    if(interactive()){
        require(devtools)
    }
}
Other functions that I find useful that I've written tend to get thrown into a package.
 

bryangoodrich

Probably A Mammal
#3
I have two profiles I use at work because my workspace is organized like this

Code:
data sources (all global data or databases)
project folders
--| data (for project)
--| documents
--| results
--| scripts
--| topics (if there are more refined projects on this project
-----| data
-----| documents
-----| results
-----| scripts
So I have project profiles and topic profiles. The project profiles set an option I can access--e.g., getOption("DATA_SOURCES")--to access the global data sources, whereas the topic profile contains that and a PARENT_DATA option to access folder in the project folder that topic might depend upon. I also include some cat statements that specify that the project accessed is a parent or topic project and what options were set. That's the only way I've really made use of the .Rprofile. This schema has helped me stay organized anyway. I work on a lot of projects at work I'm finding! lol
 
#4
I've also heard of setting up environments.. But I don't even know where to start.. I'm definitely interested in learning how to create my own personal packages, and a better organizational system would be extremely helpful -- I keep working out of the same directory and it's starting to get difficult finding the right files I need.
 
#5
I've made a lot of progress.. Took Dason and bryangoodrich's advice and created my own package - a lot easier than I thought it would be - organized my workflows to reflect the type of work my current project regards, and streamlined the entire process to be more efficient without being so dependent on a .rprofile. Currently working on scripts to make work even easier and adaptable.

My goal.. Set up a complete system that automatically takes new data from an experiment (a computer already runs the experiment and outputs a csv file of the results), summarizes the findings from the current experiment, updates a running history of data, and outputs a report of the current findings and the updated history. All without lifting a finger. It's automation on this type of level that allows me to contribute to blogs such as this. ;)
 

bryangoodrich

Probably A Mammal
#7
Nice work Nathan. I just started making use of my site profile, so I get fortunes every time I start RStudio and the home directly ("~") is at the base of my workspace folder, making it consistent when accessing my global data sources. If I have other people access my workspace over the network, I can simply set their site profile up to link to it over that network and the code should all work consistently! My projects and data aren't uniform enough to warrant creating my own packages. I look forward to the day when they do because then it would mean my work has such a culture around data that we can start doing thinks in a consistent and uniform fashion that I can create R tools to work on them!
 
#8
Thanks Bryan. I'm ironing out the kinks as I go along.. And there's a lot more to be done.. The script as it stands is supposed to periodically check the folder where the csv files get loaded, open R, and run an appropriate r script. Occasionally the experiment headings are mislabeled though and I have to go in and manually find and change them. Some other errors pop up here and there as well.. It's a work in progress.

@trinker - yes I use knitr now when appropriate, but texmaker is still my go-to for long papers.
 

Dason

Ambassador to the humans
#9
It's been a while since this has been updated so I'll put my current .Rprofile in here. Hasn't change too much though

Code:
.First <- function(){
    # Use main CRAN mirror
    options(repos = c(CRAN = "http://cran.r-project.org/"))
    
    if(interactive()){
        require(utils)
        require(devtools)
        require(pacman)

        print(tsfortunes::tsfortune())
        #system(paste("cowsay", shQuote(paste(tsfortunes::tsfortune(), collapse = " *** "))))
        system("date +'%b %e, %l:%M %P: Day %j of the year'")

    }
}
 

trinker

ggplot2orBust
#10
This probably makes no sense to others but...

Code:
.First <- function(){
    Sys.setenv(R_USER = "C:/Users/trinker")
    Sys.setenv("R_LIBS_USER" = "C:/R/R-3.0.1/library")
    options(scipen=15)
    options(github.user = "trinker")
    options(base.git = path.expand("~/GitHub"))
    options(bib.loc = path.expand("~/Dropbox/MASTER.bib"))
    options(name.reports = "Tyler Rinker\\\\University at Buffalo\\\\Department of Learning and Instruction")
    options(temp.reports = "apa6.mod.qual_tex")
    options(revealjs.loc = "C:/Users/trinker/Desktop/Copy/reveal.js")
    options(speed.temp=list(`1`="wordpress_rmd", `2`="basic_rmd", `3`="apa6.mod.qual_tex"))
    options(repos="http://cran.rstudio.com/")
    options(pac_update=TRUE)
    options(download_path = path.expand("~/Downloads/"))
    options(base_git = path.expand("~/GitHub"))
    options(primary_repo="qdap")
    options(dropbox_path = "C:/Users/trinker/Dropbox/public")
    options(dropbox_key = 61803503)       
    
    if("ggplot2" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(ggplot2)))
    }
    if("acc.roxygen2" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(acc.roxygen2)))
    }
    if("scales" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(scales)))
    }
    if("pacman" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(pacman)))
    }
    if("reports" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(reports)))
    }
    if("qdap" %in% list.files(.libPaths())){
        suppressWarnings(suppressPackageStartupMessages(library(qdap)))
    }

}
PS I miss you Nathan. Did Dason get you??? :shakehead
 

Dason

Ambassador to the humans
#11
You could get rid of those checks to see if the package is installed by using require instead
Code:
# Compare
suppressWarnings(suppressPackageStartupMessages(library(gasdfasd)))
suppressWarnings(suppressPackageStartupMessages(require(gasdfasd)))
and it's a bit cleaner if you only load the packages when you're running an interactive session.
 

trinker

ggplot2orBust
#12
Dason are you saying replace:

Code:
if("ggplot2" %in% list.files(.libPaths())){
    suppressWarnings(suppressPackageStartupMessages(library(ggplot2)))
}
With:
Code:
suppressWarnings(suppressPackageStartupMessages(require(ggplot2)))
 

trinker

ggplot2orBust
#13
Could probably be made even better with:

Code:
packs <- c("ggplot2", "acc.roxygen2", "scales", "pacman", "reports", "qdap")
FUN <- function(x) suppressWarnings(suppressPackageStartupMessages(require(x, character.only = TRUE)))
invisible(lapply(packs, FUN))
 

Dason

Ambassador to the humans
#14
Could probably be made even better with:

Code:
packs <- c("ggplot2", "acc.roxygen2", "scales", "pacman", "reports", "qdap")
FUN <- function(x) suppressWarnings(suppressPackageStartupMessages(require(x, character.only = TRUE)))
invisible(lapply(packs, FUN))
There you go. I use to have something like that but then decided I didn't want to load packages on startup. But I use devtools and pacman enough nowadays that I figured I'd load them - but that's not enough to justify a use of lapply in my eyes.