Ending "+" Prompt in R

ending + prompt in R

It depends on how you’re running R.

In the terminal, the “normal” way is Control+C – the “cancel” key combination.

In the graphical R application and in RStudio, it’s Escape.

+ Continue Option in R

You are confusing code with the prompts the R console shows. The + appearing automatically in the console after the linebreaks is a console prompt indicating that further input is expected. It is not part of the code.

To illustrate this, I check the option for the continue prompt and change it for an example:

options("continue")
options(continue = "% ")
data.frame(a = 1,
b = 2)

This is displayed in the console like this:

> options("continue")
$continue
[1] "+ "

> options(continue = "% ")
> data.frame(a = 1,
% b = 2)
a b
1 1 2

As you see, the continue prompt has been changed from + to %. Note that the prompts are always at the same position in the console. They are always the first character of any input line and by default they are always followed by a space character.

When part of the code the + operator is actually a function, which usually does arithmetic addition, but methods can be defined for different behavior. That's what ggplot2 does.

PS: If you are using RStudio I hope that you have created a new file and are writing a script from which you send the code to the console (e.g., by pressing Ctrl+Enter). Some beginners don't know how to use RStudio and simply write in the console.

Is there any command to exit R programming?

Yes. You can exit R with the quit() command. More succinctly, the quit command is aliased as q().

Normally when you start R, you'll be reminded of this command. For example, in my install I see:

R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

See docs here.

How do I stop/end/halt a script in R?

As far as I could find, there is no single command that really stops a script on every platform/version. There are several ways to handle this:

Put it in a function or curly brackets:

{
if (TRUE) {stop("The value is TRUE, so the script must end here")}

print("Script did NOT end!")
}

OR evaluate the error and handle it like in an if else construction:

if (TRUE) {stop("The value is TRUE, so the script must end here")    
} else { #continue the script
print("Script did NOT end!")
}

OR (EDIT):
Another possibility is to call the script from a seperate 'main' R-scipt, with source("MyScript.R"). Then the script terminates. This however, suppresses all output other then errors to the console.

OR for more complex operations, use tryCatch() as shown here



Related Topics



Leave a reply



Submit