Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default prompt and output line prefix in R?

Tags:

For the purposes of teaching and preparing written instructions about R, one of the things that's always frustrated me is that I can't simply copy commands and output from R and paste them into another R session. For example, if I do something trivial, such as

> x <- rnorm(10) > x  [1]  1.76975998  1.19722850 -0.39274507 -1.10979974  0.52320473 -0.08643833  [7]  0.94437690  0.08083207  0.62260363  1.89305469 

If I copy and paste that into a document or even here in this post, you (and my students) can not then just highlight it, copy it and paste it into an R session successfully

> > x <- rnorm(10) Error: syntax error > > x Error: syntax error >  [1]  1.76975998  1.19722850 -0.39274507 -1.10979974  0.52320473 -0.08643833 Error: syntax error >  [7]  0.94437690  0.08083207  0.62260363  1.89305469 Error: syntax error 

You might want to do this to test your installation of R, compare my output to yours, or simply to make use of a function I've offered.

So, what I'd like to be able to do is to change the default prompt from > to either an empty string or a blank space and also prefix all output lines with a hash mark #. That way, I could use R interactively to generate a session that looks like

x <- rnorm(10) x # [1]  1.76975998  1.19722850 -0.39274507 -1.10979974  0.52320473 -0.08643833 # [7]  0.94437690  0.08083207  0.62260363  1.89305469 

which could be copy/pasted into an R session successfully. It would make prepping R code for a journal article, students, lectures, etc. much easier for me (and maybe for others?)

I've poked around the documentation with no luck... any ideas? pointers?

Currently, I'm using R on a Mac either via the R.app GUI or from Terminal.

like image 738
William Doane Avatar asked Sep 19 '09 13:09

William Doane


People also ask

How do I change the default prompt in R?

From the standard R GUI, go File > New Script. Enter all your code in there, then to run it, just highlight code and press CTRL-R. Many other R GUI's have similar behavior. You can still work interactively in this mode; the key difference is that you highlight code and run it rather than hitting ENTER.

What is the default prompt in R?

The default prompt in the R console merely indicates it's awaiting input.


1 Answers

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.

like image 74
Jake Avatar answered Sep 20 '22 08:09

Jake