-
-
Save EvaMaeRey/25447b91d99cbeb59ffbd465c63c3965 to your computer and use it in GitHub Desktop.
Get the previous commands as a text string in RStudio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # RStudio overrides `history()`, `loadhistory()` and `savehistory()`, | |
| # so they work somewhat differently than on the console version of R. | |
| # This saves the current history to a temporary file then reads the last | |
| # line of it (or more depending on the `n` argument) | |
| # | |
| # It does not handle multi-line commands, which would be more tricky to handle... | |
| previous_commands <- function(n = 1) { | |
| tf <- tempfile() | |
| on.exit(unlink(tf)) | |
| savehistory(tf) | |
| head(tail(readLines(tf), n = n + 1), n = n) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment