-1

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.

Roland
  • 127,288
  • 10
  • 191
  • 288
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • 1
    Helpful [link1](http://stackoverflow.com/questions/18803354/ending-prompt-in-r) and another [link2](http://stackoverflow.com/questions/27709105/signs-appearing-in-console-in-r) – akrun Aug 04 '16 at 04:43
  • Akrun, Thanks for the link, but these two links don't help at all. One link talks about getting rid of "+" by pressing Esc, while the other simply states problems with the code (other than "+"). Can you you please help me with my questions? – watchtower Aug 04 '16 at 05:09
  • 1
    Also, I don't think these should be linked. The question is similar, but answers cover different topics. – watchtower Aug 04 '16 at 05:10
  • I see that the threads are still linked. I am not sure what you mean by "reopened it"... – watchtower Aug 04 '16 at 05:25
  • It is just a comment. If you don't want to click on it, don't do it. Previously, I thought it as dupe and dupe tagged it, which was reopened. – akrun Aug 04 '16 at 05:26
  • 2
    @watchtower, any link to other content on StackOverflow posted as part of a comment or an answer appears in the Linked section. – Hristo Iliev Aug 04 '16 at 07:51
  • @watchtower the second link that was linked by akrun is exactly the same question as you asking. How isn't it related? – David Arenburg Aug 04 '16 at 09:18
  • @David Arenburg--I believe I have clarified that the question is similar, but not exactly same because the original poster is concerned about opening a new object. Moreover, the response in that post talks about different issue..E.g. ."The + symbols are only a way to show that the current statement is conitnuation of the previous lines's statement. That's perfectly OK. There are a couple of other problems...." – watchtower Aug 04 '16 at 13:24

2 Answers2

9

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.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thanks Roland. You are spot on--I do use Ctrl + Enter in RStudio. If I understand this correctly, a) the linebreaks appear in the console with "+" because that's the continue operator. b) if I explicitly use "+" in my code, it would be treated as a function. c) ggplot2, I believe, uses "+" for piping. Am I correct? – watchtower Aug 04 '16 at 13:20
  • 2
    Please read my answer. a) It is not an "operator". It's an input prompt. – Roland Aug 04 '16 at 13:39
  • Sorry that was a typo. I did mean input "prompt". I do understand that "+" is not an operator in a). Thanks Roland. – watchtower Aug 04 '16 at 13:57
1

Usually in R, the + just means addition. Ggplot2 is a special case, though, as it has overridden the default behaviour, so that + means that you can add objects together, to build up layers of a plot. Behind the scenes, ggplot is taking each individual function call, putting them into a list, and executing them together.

http://docs.ggplot2.org/current/gg-add.html

So, now to explain your code issues.

The + operator generally means addition, as in 1 + 1 = 2. Secondly, single function calls can wrap over lines without needing any continuation operator. So with those to points in mind, it makes no sense whatsoever to put a plus sign in the middle of a function call.

R would take this code:

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

and turn it into this:

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

Secondly, your guess about it about a continuation code is incorrect. It's more of a continuation indicator, and is just a way for R to show that the code is all part of the same function call.

Nathan Webb
  • 457
  • 3
  • 10
  • Nathan thanks for your reply. I am still not understanding---if what you are saying about "+" is correct, then the "modified" version in Scenario 1 shouldn't work, but it does. Thoughts? – watchtower Aug 04 '16 at 05:24
  • 2
    Your code doesn't actually get modified - the console just displays it with a '+' prompt to indicate that the function goes over multiple lines. – Nathan Webb Aug 04 '16 at 13:33