Change default prompt and output line prefix in R?
You might try:
options(prompt=" ", continue=" ")
Note the spaces in between the quotes.
The first option makes the prompt disappear. The second erases the "+" from appearing on long wrapping lines.
As for changing the prompt, the command you're looking for is options
, with argument prompt
, which I found here.
> options(prompt = "# Customized R Prompt!\n") # Customized R Prompt! 1 + 5 [1] 6 # Customized R Prompt!
Setting the prompt to an empty string results in:
> options(prompt="") Error in options(prompt = "") : invalid value for 'prompt'
Which is why I used a comment. As far as I can tell, R does not have block comments, which is why I made the prompt a line comment and put a newline at the end of it -- should be no problems if anyone copy-pastes your session that way.
I'm still looking on the output format for a bit here... There's some code at this mailling list post that seems to format the output without the [#] blocks, but it's sure not pretty.
So, I very much like Jake and Marek's solutions. Jake's is straightforward, but doesn't address the output formatting part of the problem. Marek's was a bit cumbersome, so I wrapped it up in a function resulting in
cleanCode <- function() {
if (.Platform$OS.type == "unix" && .Platform$pkgType == "mac.binary") {
to_edit <- readLines(pipe("pbpaste")) # Mac ONLY solution
} else {
to_edit <- readLines("clipboard") # Windows/Unix solution
}
opts <- options()
cmdPrompts <- paste("^", opts$prompt, "|^", opts$continue, sep="")
# can someone help me here? how to escape the + to \\+, as well as other special chars
id_commands <- grep("^> |^\\+ ", to_edit) # which are command or continuation lines
to_edit[id_commands] <- sub("^> |^\\+ ", "", to_edit[id_commands]) # remove prompts
to_edit[-id_commands] <- paste(" # ", to_edit[-id_commands]) # comment output
writeLines(to_edit)
}
which lets me highlight and copy a portion of the interactive session.
So, for example, I can use this to copy
> x <- rnorm(20)
> plot(x)
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.34000 -0.86010 -0.21940 -0.43340 0.04383 1.06400
> str(x)
num [1:20] -1.568 -0.219 -1.951 1.064 0.768 ...
> sd(x)
[1] 0.8932958
to the clipboard and with a simple call to
> cleanCode()
produces output such as
x <- rnorm(20)
plot(x)
summary(x)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -2.34000 -0.86010 -0.21940 -0.43340 0.04383 1.06400
str(x)
# num [1:20] -1.568 -0.219 -1.951 1.064 0.768 ...
sd(x)
# [1] 0.8932958
which someone could quickly highlight and copy & paste into an R session to execute the code and compare their output. Of course, in this case, they'll get different results, since I'm basing the example on random data.
Thank you Jake, Marek, and everyone else... all of the responses have been helpful!