domenica 6 maggio 2007

How to clear screen in R (Windows)

This tip (taken from this old thread) does work only under Windows:

cls <- function() {
       require(rcom)
       wsh <- comCreateObject("Wscript.Shell")
       comInvoke(wsh, "SendKeys", "\014")
       invisible(wsh)
}
cls()
or
# An R function to clear the screen on RGui:
cls <- function() {
if (.Platform$GUI[1] != "Rgui")
return(invisible(FALSE))
if (!require(rcom, quietly = TRUE)) # Not shown any way!
stop("Package rcom is required for 'cls()'")
wsh <- comCreateObject("Wscript.Shell")
if (is.null(wsh)) {
    return(invisible(FALSE))
} else {
comInvoke(wsh, "SendKeys", "\014")
    return(invisible(TRUE))
}
}
cls()

See this StackOverflow Question for other solutions.

9 commenti:

  1. great stuff! Extremely useful, thanksa lot !

    RispondiElimina
  2. why not simply press ctr+l? does the same job

    RispondiElimina
  3. In a Unix terminal CTRL+l cleans the screen. In the default GUI for Mac it doesn't work! The depicted solution does work more generally.

    RispondiElimina
  4. I'm a mac/pc user but I do all my r in pc so im not sure if this will work but try command l

    RispondiElimina
  5. Control+L works well under RGui and Windows.

    RispondiElimina
  6. command + alt + l should work on a Mac.

    RispondiElimina
  7. Hi,

    those function don't work anymore on Windows 7 with R 2.15
    do you have an updated version?

    RispondiElimina
  8. Dear Stephen, thanks for your feedback. Unfortunately I don't own a Windows 7 license and I currently use either Linux or Mac OS X. To make it short, I don't have an updated version of this tip. If you find a working solution to this task(maybe via StackOverflow) please let me know so I can update the 'recipe'. Thank you!

    RispondiElimina