4

I was wondering how I can execute some pre-defined functions when I open R or R-studio?

I know that it sounds silly, but I installed package praise and sort of want to try executing praise() automatically every time I open R or R studio, without actually typing in praise().

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
hippoppih
  • 41
  • 1

1 Answers1

4

For this you can use .First() and .Last() in the .Rprofile.

It's a typical R file, launched on startup and used primarly to export some stuff by default.

Example .Rprofile:

# .First() run at the start of every R session. 
# Use to load commonly used packages? 
.First <- function() {
    library(ggplot2)
    cat("\nSuccessfully loaded .Rprofile at", date(), "\n")
}

# .Last() run at the end of the session
.Last <- function() {
    cat("\nGoodbye at ", date(), "\n")
}

Related: Expert R users, what's in your .Rprofile?

Community
  • 1
  • 1
m0nhawk
  • 22,980
  • 9
  • 45
  • 73