Every R user knows the verbose of the R startup message. It contains essential information such as the R version and that R is an open-source platform. However, seeing this message every day … multiple times a day … can be a bit boring. What about seeing something different and nice every time you start RStudio?

Classic startup message for R version 4.0.3. Nothing bad at all but it could be more entertaining

I took a look at how to modify the startup message after a couple of tweets from Kara Woo here and here. After reading the community’s answers I had to do someting.

.Rprofile and RStudio Startup

.Rprofile is an empty file that can be used to automatically modify your blank new environement every time you start a R session. It can be used to load packagaes (i.e. {magrittr} in order to access the pipe), to set up options (i.e. scipen = 999 to deactivate the scientific format when printing numbers) or to create object containing specific data such as API keys. .Rprofile is generally located on the root of the R session and can be accessed by using file.edit("~/.Rprofile") but the package {usethis} has a specific function to open it so … let’s use it!

usethis::edit_r_profile()

As indicated, a blank file should open if you haven’t modified it yet.

Modifications of the .Rprofile file can display very useful information such as the Git branch name that you are currently working on (see this great post from Romain François). There is even a package on GitHub called {rprofile} created by Colin Gillespie to modify it with useful functions.

By scrapping the web, it is possible to find that the startup of RStudio sessions can be modified automatically (see this post about RStudio API). To do so, the rstudio.sessionInit hook has to be invoked in a function as follow:

setHook("rstudio.sessionInit", function(newSession) {
  
  # any code included here will be run at the start of each RStudio session
  
}, action = "append")

Customise your RStudio Startup Message

Inside this rstudio.sessionInit function, cat("\014") or cat('\f') will automatically clear the console and therefor the R startup message. This code is associated to the RStudio keyboard shortcut CRTL + L to clear the console.

Next step is to display something nice in the console. You could just print a message and it will be displayed everytime. However if you want to change your startup message you can also use one of the many R package that constains specific quotes and selecting a random one for every startup! Here is a non-exhaustive list of the quotes packages:

If you want to display your own random quotes, it is possible to query any API and to display its random quote. For example, let’s query the quote of the day from They Said So Quotes API using {httr} and {jsonlite}:

jsonlite::fromJSON(
  rawToChar(
    httr::GET("https://quotes.rest/qod")$content
  )
)$contents$quotes$quote

This They Said So Quotes API has several endpoint that can be queried to obtain different language or different categories of quotes.

For me the icing on the cake is the package {cowsay} available on CRAN by Scott Chamberlain and all 17 contributors. {cowsay} displays ASCII animals in the console with a bubble for which you can choose the content of the text.

cowsay::say("So why not choosing one of this animals to say a random quote in your startup message?", "cat")
## 
##  -------------- 
## So why not choosing one of this animals to say a random quote in your startup message? 
##  --------------
##     \
##       \
##         \
##             |\___/|
##           ==) ^Y^ (==
##             \  ^  /
##              )=*=(
##             /     \
##             |     |
##            /| | | |\
##            \| | |_|/\
##       jgs  //_// ___/
##                \_)
## 

The function say() from {cowsay} has two main arguments: the text to say and the type of animal (with respectively “Hello world!” and “cat” as default values). You could choose to have the same animal every time by picking one on the list displayed in cowsay::animals but it is also possible to pick a new one every time by using “random” as follow:

cowsay::say("I am completely randomly choosen!", by = "random")
## 
##  ------------- 
## I am completely randomly choosen! 
##  -------------- 
##               \   
##                \  
##                 \
##                .--.
##               /} p \             /}
##              `~)-) /           /` }
##               ( / /          /`}.' }
##                / / .-'""-.  / ' }-'}
##               / (.'       \/ '.'}_.}
##              |            `}   .}._}
##              |     .-=-';   } ' }_.}
##              \    `.-=-;'  } '.}.-}
##               '.   -=-'    ;,}._.}
##                 `-,_  __.'` '-._}
##               jgs   `|||
##                    .=='=,

In say() it is also possible to choose the color of the text and the color of the animal, let’s make it random as well with the package {randomcoloR} by Ron Ammar available on CRAN!

cowsay::say(
  what = "Random EVERYWHERE! but colors are not printed in .Rmd console output :(", 
  by = "random", 
  what_color = randomcoloR::randomColor(), 
  by_color = randomcoloR::randomColor()
)
## 
##  ----- 
## Random EVERYWHERE! but colors are not printed in .Rmd console output :( 
##  ------ 
##     \   
##      \
##                 \`*-.
##                  )  _`-.
##                 .  : `. .
##                 : _   '  \
##                 ; *` _.   `*-._
##                 `-.-'          `-.
##                   ;       `       `.
##                   :.       .       \
##                   .\  .   :   .-'   .
##                   '  `+.;  ;  '      :
##                   :  '  |    ;       ;-.
##                   ; '   : :`-:     _.`* ;
##                .*' /  .*' ; .*`- +'  `*'
##      [bug]     `*-*   `*-*  `*-*'
## 

Finally you just have to add the elements all together: rstudio.sessionInit hook function with, cat("\014") to erase the R message and {cowsay} using a random quote from a package or from an API:

setHook("rstudio.sessionInit", function(newSession) {
  cat("\014")
  cowsay::say(
    what = jsonlite::fromJSON(
      rawToChar(
        httr::GET("https://quotes.rest/qod")$content
        )
      )$contents$quotes$quote,
    by = "random",
    what_color = randomcoloR::randomColor(),
    by_color = randomcoloR::randomColor()
  )
}, action = "append")

Instead of the call to the Quote of the day API you can also use one of the R package presented above:

norris::get_random_joke()
sample(mitchhedberg::jokes, 1)
as.character(goodshirt::soul_squad())
as.character(dundermifflin::get_quote())
paste(as.character(statquotes::statquote())[4:5], collapse = "\n ~ By ")
paste(capture.output(dadjoke::dadjoke()), collapse = " ")
praise::praise()
paste(as.character(fortunes::fortune()), collapse = " ")
as.character(gaussfacts::gaussfact()) 

Even More Possibilities

My knowledge of art possibilities in R console is very limited but I’m sure there is even more to do. For example it would be possible to make it even more crazy, by using some video game sounds with {beepr} by Rasmus Bååth, by printing an image with the {ransid} by mikefc available on GitHub (see vignette here), by using a rayshader 3D plot or the matrix background.

Possibilities are limitless …

A joke or a philosophical quote is more entertaining but here you go!

Don’t hesitate to tell me if I forgot an amazing R package for quotes or for console display!

Edit: Print a Digital Rain in the console before the quote

I have found on StackExchange code golf an amazing code displaying a digital rain. Many thanks to the creators of the code Billywob and plannapus!

  f = function(w, t, n, m) {
    for (i in 1:t) {
      cat("\f", sample(rep(c("/", " "), c(n, 1920 - n))), sep = "")
      Sys.sleep(w)
    }
    cat("\f", g <- rep("\n", 12), rep(" ", (80 - nchar(m)) / 2), m, g, sep =
          "")
  }
  f(
    w = 0.2,
    t = 10,
    m = "Welcome to R",
    n = 100
  )

Edit: Use httr::content() instead of jsonlite::fromJSON(rawToChar())

I recently replied to a tweet about how to display Ted Lasso’s quote on RStudio startup and I realised that jsonlite::fromJSON(rawToChar()) could be replaced by a simple call to httr::content() which made the code much easier as shown below:

setHook("rstudio.sessionInit", function(newSession) {
  cat("\014")
  cowsay::say(
    what = paste(
      httr::content(httr::GET("https://tedlassoquotes.com/v1/quote"))[1:2],
      collapse = " "),
    by = "random",
    what_color = randomcoloR::randomColor(),
    by_color = randomcoloR::randomColor()
  )
}, action = "append")