Clean up “everything” in RStudio

This is a tip for how to clean up your RStudio windows.

For workspace:

You can use rm() to clean all objects in current environment

rm(list=ls())

Or if you only want to remove specific object or only a group of new generated objects, try the following:

rm(list='obj_name')
obj.list <- ls()  #Save the names of the existing objects
....
rm(list=setdiff(ls(), obj.list))  #Remove any new generated objects

 

For console:

You can press Ctrl – L manually. Of course, it would be nice to do this programmatically. So try this:

cat("14")  # or cat("f")

 

For plot windows:

Try to use dev.off(), it will clost all existing graphical device and only keep Null device (device 1). If you have other graphical devices open (e.g. pdf or png) and don’t want them to be closed, you can use dev.list() to figure out which graphical device is RStudio’s.

dev.off(dev.list()["RStudioGD"]

 

The correct way of hardcoding

Sometimes even after a good attempt by clinical data management at cleaning and coding the data, you may still find the data contain some undesired values. Therefore, you may need to use hardcoding to override the data before you have time to fix them in data management system.

However, hardcoding is dangerous and it is better to avoid hardcoding in any circumstance. One big reason is that data often change over time and the hardcoding writing today may not be appropriate in the future. A hardcode can be easily forgotten and the left code normally will lead to an unpredictable error when you analyze the data.

If hardcoding must be done, some programming skills may be helpful to reduce that risk. See the example below, the &sysdate was used to force the hardcoding to expire at some date point.

 

data test;
  set test;
  * Hardcode approved by Someone on 12/13/2012;
  if identity = "NEMISIS" and "&sysdate"d <= "13Dec12"d then do;
    ....;
    ....;
  end;
run;