I created a snippet. Go to Tools> Global Options> Code> Snippets> Edit Snippets and paste this in the R section and click Save. There should be tabs after the first line or else the snippet won't work. I like to use the here
package which will find the file at the project level no matter where the current working directory is. You can change that part of the snippet if you wish. Change h
to whatever you want.
snippet h
`r eval(parse(text = '
write("", file=here::here(".Rhistory"))
loadhistory(here::here(".Rhistory"))
'))`
Now all you have to do is type h
then Shift+Tab to run the snippet and the code in it! Read more about snippets here
loadhistory('nohist.txt')
.rstudioapi::executeCommand("clearHistory", quiet = TRUE)