R: RStudio Plots not showing in the Plots Window

A. Symptom

The plots stopped showing in the pane when running the R codes. The temporary png file generated in the folder didn’t have the graphic output and the file was not able to be deleted.

B. R Code Caused problem

png(file='Construct.png') 
subgroup.analysis.mixed.effects(x = m.hksj, subgroups = madata$Construct, plot=TRUE)
dev.off

In R use png(file=) and dev.off to generate graphic output in the working directory. The subgroup.analysis.mixed.effect function doesn’t work with this method, hence the output graphic file got stuck.

C. Fix

dev.cur()   # to identify output device as "png 4"
dev.off()  # to identify "null device" so the png file in the folder can be deleted;
getOption("device") # to set option to "RStudioGD"