watchtower watchtower - 3 months ago 21
R Question

"+" Continue Option in R

I am a beginner and have recently started using R instead of Stata because of its open-source nature.

I am a little confused about "+" continue symbol.

Here's how my data looks like:

head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1


Let's consider four scenarios:

Scenario 1:
I have noticed that RStudio automatically appends "+" if I write the following block of code:

attach(mtcars)
cyl.f <- factor(cyl, levels= c(4,6,8),
labels = c("4 cylinder", "6 cylinder",
"8 cylinder"))


gets changed to :

cyl.f <- factor(cyl, levels= c(4,6,8),
+ labels = c("4 cylinder", "6 cylinder",
+ "8 cylinder"))


As you can see above, RStudio automatically appended "+"

Scenario 2:
If I add "+" at the end of the code, it doesn't work for some strange reasons:

cyl.f <- factor(cyl, levels= c(4,6,8), +
labels = c("4 cylinder", "6 cylinder", +
"8 cylinder"))


This throws an error.

Scenario 3:
If I add "+" at the beginning of the code, it doesn't work for some strange reasons:

cyl.f <- factor(cyl, levels= c(4,6,8),
+ labels = c("4 cylinder", "6 cylinder",
+ "8 cylinder"))


This throws an error. This is understandable because RStudio automatically adds another "+" continue operator.

Scenario 4:

windows()
ggplot(data=mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title="Automobile Data", x="Weight", y="Miles Per Gallon")


If I don't add "+", this doesn't work. Apparently, it seems "+" is a "piping" operator above.

Now, the challenge is that I can't google this or search on SO because both the search engines think that "+" is an OR operator.

Can someone please explain when "+" is an operator for "continue" and when it is an operator for "piping."

I'd appreciate any thoughts and detailed explanation. This is extremely confusing for me.

Answer

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.

Comments