1

Is there a shortcut to add a new line into the R (RStudio) code?

I would like to avoid something like gapminder%>%filter(continent=="Asia")%>%group_by(year)%>%summarise(mean_pop=mean(pop))%>%ggplot(aes(x=year,y=mean_pop))+geom_point()+geom_line()+theme_bw()

by using

gapminder %>%
   filter(continent == "Asia") %>%
   group_by(year) %>%
   summarise(mean_pop = mean(pop)) %>%
     ggplot(aes(x = year, y = mean_pop)) +
     geom_point() +
     geom_line() +
     theme_bw()

but without holding the space bar all the time.. and I googled now nearly half an hour and just didn't find a shortcut. Can't believe it?! So how all are doing this?

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
Ben
  • 1,432
  • 4
  • 20
  • 43

2 Answers2

2

RStudio has separate windows for editing code and executing commands:

enter image description here

In this case, the top left is the code editor (but the layout is modifiable). Below is the console.

To edit code, create a new file or open an existing file, and edit it in the code editor. RStudio will automatically attempt to format your code as appropriate when you hit Return.

To execute code you’ve written in the code editor there are several choices. To execute just the current statement, you can hit Cmd+Return. There are more options directly above the code editor (check out the menus ‹Run› and ‹Source›).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

In your case try to use ; and not %>%.

Example:

gapminder;filter(continent=="Asia");group_by(year);summarise(mean_pop=mean(pop))

Press CTRL + SHIFT + A or select all code and go to Code-> Reformat code

Result:

gapminder
filter(continent == "Asia")
group_by(year)
summarise(mean_pop = mean(pop))
seralouk
  • 30,938
  • 9
  • 118
  • 133
  • At first thanks! In "Code" I have only "Go To File/Function", "Show Diagnostics" and "Source File". Where is "Reformat code"? The keyboard shortcut does not work. – Ben Jul 06 '17 at 13:18
  • No, that’s almost certainly not what OP wants; not sure why it’s upvoted. – Konrad Rudolph Jul 06 '17 at 13:20
  • @Ben type: version. What version do you have? I have R version 3.2.2 (2015-08-14) – seralouk Jul 06 '17 at 13:20
  • @sera I have RStudio 1.0.143? – Ben Jul 06 '17 at 13:22
  • I think it's quite inefficient + wrong since the pipe operator have disapeared (so the code won't work now and the OP has to put the pipe for each line where it's missing). What the OP should do is to learn how to use properly how to indent his code with tab key and return. – Mbr Mbr Jul 06 '17 at 13:22
  • @MbrMbr Tab pop ups suggestions of which function I might want to use. And return executes the command line. I'm a little bit lost.. – Ben Jul 06 '17 at 13:27
  • 1
    @Ben Are you sure you're not running the lines in the console instead of the editor ? – Mbr Mbr Jul 06 '17 at 13:33