Configure RStudio Cloud
Dec 13, 2021, updated Feb 7, 2023
R
Configure Closing Behavior.
For historic reasons, when you quit R (or Close an RStudio Project), R will ask you if you want to save a workspace image.That is, it is asking if you want to save all the datasets and functions that you have in R’s working memory (the Global Environment) into a file called .RData. Most experts believe this is an extremely bad idea because you can end up with objects in that store which last forever and you can loose track of the code that made the objects. That leads to two kinds of nasty bugs. First, the obvious bug is you can easily save “broken” datasets and code. The second, and arguably worse problem, is that when somebody else tries to run your code, if they don’t get a copy of your .Rdata file and load the saved objects file, R will complain because the objects are missing. Therefore, you want to tell R to never save the workspace. To set tell R to never save the workspace and never prompt again go to the Tools menu and choose Global Options…:
Then choose uncheck the Respore .RData into workspace at startup checkbox and set Save workspace to .RData on exit: to be Never:
When you close an RStudio Project or quit R, it will continue to ask you if you want to save you code files. Always save your code files.
Packages
R uses “packages” to add new functionality. Typically packages contain functions to calculate new statistics and/or datasets but there are also packages that add new menu items to RStudio (like the rUM
package) or to automate tasks. If you are using RStudio Cloud for a class your instructor has probably configured it to give you access to analysis packages. I highly recommend that you run the code below to add in packages that contain tools to automate tasks.
To run this code in RStudio Cloud, open a project and paste this code into the Console windowpane and push the enter/return key on your keyboard.
if (!require('remotes')) install.packages('remotes')
remotes::install_cran("devtools")
The code above first checks to see if you have the remotes
package installed and if not it will download and install it. Once remotes
is installed, you can use it to efficiently install all other packages. In the code above the install_cran()
function, which comes in the remotes
package will install the devtools
package. remotes::install_cran
is smart enough to check to see if you have already installed “devtools”. So you will either see a message saying Skipping install of ‘devtools’ or R will install it and a pile of other useful packages, including the “usethat” package which is used below.
After installing you should see messages in the Console. The feedback notes are in red by default. So, don’t panic if/when your screen looks bloody after running the commands:
A Couple Thoughts On remotes::install_cran()
If you have been exposed to R before, you may have seen the install.packages()
function. I prefer remotes::install_cran()
because it checks to see if you already have installed a package and it stops if you already have the latest version. As you get more experience with R you may see devtools::install_cran()
or devtools::install_github()
on the web. That just means the example code is a bit old. Back in 2018 the “install_*” functions were moved out of devtools
into the remotes
package.
Introduce Yourself to git
git
is a language that is used to backup code and to do collaborative code development. Replace my details with your name and email address in the line below and then run the code using the Console windowpane. Double check that you included all the ” characters and don’t forget the ) character at the end.
usethis::use_git_config(
user.name = "Raymond R. Balise",
user.email = "balise@miami.edu"
)
If everything goes well, you will get no feedback:
If you go on to learn about www.GitHub.com you will need to do extra steps to link your RStudio Cloud account to your GitHub account. Talk with your instructor about data security before you use GitHub. Remember these lines but don’t use them until your instructor has checked your security settings. When it comes time to use GitHub this will help you setup the connection:
remotes::install_cran("gitcreds")
usethis::create_github_token()
library(gitcreds)
gitcreds_set()
Setup Your .Rprofile File
Run usethis::edit_r_profile()
in the Console windowpane to have R open your .Rprofile file. Any code that you add into your .Rprofile file is run every time RStudio starts.
Add a Couple Packages to the Profile File.
While you may be tempted to add a pile of packages, which you use every day, into your profile, don’t do it. If you rely on your profile file to automatically load packages your analysis code may not work when your main machine is in the shop and you try to run your analysis files on a new machine. The functions in the packages I mentioned above, devtools and usethis, are not normally saved into permanent programs. So, it is safe to add them to your configuration. Paste this code into your .Rprofile file and save it:
if (interactive()) {
suppressMessages(require(devtools))
suppressMessages(require(usethis))
suppressMessages(require(reprex))
}
Notice that there is a message at the bottom of the Console windowpane saying you need to restart your R:
You will restart the R session below.
Set Options to Tell R Who You Are
You can also add a few options to your profile. Replace my name, email and ORCID (if you have one) with your own information and paste this at the bottom of the profile file:
options(
usethis.full_name = "Raymond R. Balise",
usethis.description = list(
`Authors@R` = 'person(given = "Raymond",
family = "Balise",
role = c("aut", "cre"),
email = "balise@miami.edu",
comment = c(ORCID = "0000-0002-9856-5901"))'
)
)
If you are not a researcher you probably will not have an ORCID. You can erase the entire comment line but be sure to replace the comma at the end of the email line with the closing single quote ’ that is currently at the end of the comment line.
Use the Session menu to restart your R session:
After restarting it will look something like this:
You can close the profile file by clicking the small x next to .Rprofile in the title tab:
Final Thoughts
You may need to make these changes more than once if you are working in a Shared Space in RStudio.Cloud. That is, even if you have set options in one project these options may not be in place when you make new projects as part of a class.