1
2
3### Function declarations
4
5## 1
6{
7  fun <- function(argument1,
8                  argument2) {
9    body
10  }
11}
12
13## 2
14{
15  function(
16           argument1,
17           argument2
18           )
19  {
20    body
21  }
22}
23
24## 3
25function(argument_fun(sub_argument1,
26  sub_argument2),
27  argument) {}
28
29## 4
30function(argument1, parameter = fun_call(
31  sub_argument),
32  argument2) {}
33
34## 5
35function()
36
37  function() body
38
39## 6a
40object <- function()
41{
42  body
43}
44
45## 6b
46object <-
47  function()
48  {
49    body
50  }
51
52## 6c
53object =
54  function()
55  {
56    body
57  }
58
59## 6d
60fun_call(argument) <-
61  function()
62  {
63    body
64  }
65
66## 7
67{
68  object <- function()
69  {
70    body
71  }
72}
73
74## 8
75{
76  fun_call(parameter = function()
77  {
78    body
79  })
80}
81
82## 9
83{
84  fun_call(parameter = function() {
85    body
86  })
87}
88
89## 10
90fun_call(
91  function() {
92    stuff
93  }
94)
95
96## 11
97{
98  fun_call1(fun_call2(argument, function() {
99    stuff
100  })
101  )
102}
103
104## 12
105{
106  fun_call1(argument, fun_call2(function() {
107    stuff
108  })
109  )
110}
111
112## 13
113fun_call(object :=
114           function()
115           {
116             body
117           })
118
119## 14
120fun_call(argument,
121  function(x)
122    stuff
123)
124
125## 15a
126`object` <- function()
127{
128  body
129}
130
131## 15b
132"object" <- function()
133{
134  body
135}
136
137## 15c
138'object' <- function()
139{
140  body
141}
142
143
144### Function calls
145
146## 1
147fun_call(argument1,
148  argument2)
149
150## 2
151fun_call(
152  argument1,
153  argument2
154)
155
156## 3
157fun_call(parameter = (
158  stuff
159),
160argument)
161
162## 4
163fun_call(parameter = fun_argument(
164  argument1
165),
166argument2)
167
168## 5
169fun_call(parameter = fun_argument(argument1,
170  argument2
171)
172,
173argument3)
174
175## 6
176`fun_call`(argument1,
177  argument2)
178
179## 6b
180`:=`(argument1,
181  argument2)
182
183## 7
184`fun_call`(
185  argument1,
186  argument2
187)
188
189## 7b
190`:=`(
191  argument1,
192  argument2
193)
194
195## 8
196fun_call(argument1
197, argument2
198, argument3,
199  argument4, (
200    stuff1
201  ),
202  argument5, (
203    stuff2
204  )
205 ,
206  argument6
207)
208
209## 9
210fun_call(parameter =
211           fun_argument(
212             sub_argument
213           ),
214  argument
215)
216
217## 10
218fun_call(parameter = fun_argument(
219  sub_argument
220),
221argument
222)
223
224## 11
225{
226  fun_call1(
227    fun_call2 (argument1, argument2,
228      parameter = fun_call3(
229        argument3,
230        argument4
231      ), function(x) {
232        body
233      },
234      argument5,
235      fun_call4(
236        argument6
237      ),
238      argument7
239    ), {
240      stuff
241    },
242    argument8
243  )
244}
245
246## 12
247object <- fun_call(
248  arg1,
249  arg2
250)
251
252## 13
253fun_call1(fun_call2(
254  argument
255))
256
257## 14
258some_function <- fun_call1(fun_call2(
259  argument
260))
261
262## 15
263object[, fun_call(
264  argument
265)]
266
267## 16
268fun_call1(argument1, fun_call2(fun_call3(
269  argument2
270))
271)
272
273## 17
274fun_call({
275  stuff1
276  stuff2
277
278  stuff3
279})
280
281## 18
282fun_call(argument1 %>%
283           stuff,
284  argument2)
285
286## 19
287fun_call(argument,
288  )
289
290## 20
291fun_call(parameter1 = ,
292  parameter2 = argument)
293
294
295### Blocks
296
297## 1
298{
299  function()
300  {
301    body
302  }
303}
304
305## 2
306{
307  fun_call({
308    stuff1
309  },
310  {
311    stuff2
312  }
313  )
314}
315
316## 3
317fun_call({
318  stuff1
319}, {
320  stuff2
321})
322
323## 4
324fun_call(
325  parameter1 = {
326    stuff1
327  },
328  parameter2 = {
329    stuff2
330  }
331)
332
333## 5
334fun_call(parameter1 = {
335  stuff1
336},
337{
338  stuff2
339}, parameter2 = {
340  stuff3
341}, {
342  stuff4
343},
344parameter3 =
345  stuff5 ~
346    stuff6 +
347    stuff7,
348argument)
349
350## 6
351fun <- fun_call({
352  stuff1
353}, {
354  stuff2
355},
356{
357  stuff3
358}
359)
360
361## 7
362fun <- fun_call({
363  stuff
364},
365argument
366)
367
368## 8
369fun_call(function(x) {
370  body1
371},
372function(x) {
373  body2
374})
375
376## 9
377fun_call(
378{
379  stuff
380}, {
381  stuff
382}
383)
384
385## 10
386object <-
387  fun_call({
388    stuff
389  }, {
390    stuff
391  })
392
393## 11
394object <-
395  fun_call(     {
396    body
397  }
398  )
399
400## 12
401fun_call1(
402  fun_call2({
403    stuff
404  }
405  )
406)
407
408## 13
409{
410  stuff1
411
412  {
413    stuff2
414  }
415}
416
417## 14
418{{
419  stuff
420}
421}
422
423## 15
424({
425  stuff
426})
427
428## 16
429( {
430  stuff
431}
432)
433
434## 17
435fun_call(argument, function(argument1,
436                            argument2) {
437  body
438}
439)
440
441## 18
442fun_call(
443  argument,
444  function(argument1,
445           argument2) {
446    body
447  }
448)
449
450## 19
451fun_call1(
452  fun_call2(argument, function(x) {
453    body
454  })
455)
456
457## 20
458fun_call1({
459  object1 <- fun_call2(
460    argument)
461  object2
462})
463
464## 21
465fun_call(argument,
466  function() {
467
468    stuff
469  }
470}
471
472## 22
473function_call()
474stuff
475
476
477### Bracket indexing
478
479## 1
480object[
481  argument1,
482  argument2
483]
484
485## 2
486object[argument1,
487  argument2
488]
489
490## 3
491object[(
492  argument1
493)]
494
495## 4
496{
497  object[
498    fun_call(
499      body
500    ),
501    argument[
502    (
503      sub_argument
504    )
505    ]
506  ]
507}
508
509## 5
510{
511  object[
512    argument1,
513    argument2,
514    by = argument3
515  ][
516    argument4,
517    fun_call1(argument1,
518      argument2)
519    argument5
520  ][
521    argument6,
522    fun_call2(
523      argument1,
524      argument2
525    )
526  ]
527}
528
529
530### Control flow
531
532## 1
533{
534  if (condition) {
535    stuff1
536  } else {
537    stuff2
538  }
539}
540
541## 2
542{
543  if (condition) {
544    stuff1
545  }
546  else {
547    stuff2
548  }
549}
550
551## 3
552{
553  if (condition)
554  {
555    stuff1
556  } else
557  {
558    stuff2
559  }
560}
561
562## 4
563{
564  if (condition)
565  {
566    stuff1
567  }
568  else
569  {
570    stuff2
571  }
572}
573
574## 5
575{
576  for (sequence)
577  {
578    stuff
579  }
580}
581
582## 6
583{
584  for (sequence) {
585    stuff
586  }
587}
588
589## 7
590if (condition)
591  stuff
592
593## 8
594for (sequence)
595  stuff
596
597## 9
598object <-
599  if (condition) {
600    stuff1
601  } else {
602    stuff2
603  }
604
605## 10
606{
607  object <-
608    if (condition) stuff1
609  else stuff2
610}
611
612## 10
613{
614  object <-
615    if (condition) fun_call(
616      argument1,
617      argument2
618    )
619  else stuff
620}
621
622## 11
623{
624  fun_call(parameter =
625             if (condition)
626               stuff1
627    else
628      stuff2
629  )
630}
631
632## 12
633{
634  if (condition1) {
635    stuff1
636  }
637  else if (condition2)
638    stuff2
639  else if (condition3) {
640    stuff3
641  } else if (condition4)
642    stuff4
643  else
644    stuff5
645}
646
647## 13
648fun_call(
649  argument,
650  parameter = if (condition1) {
651    stuff1
652  } else if (condition2) {
653    stuff3
654  } else {
655    stuff2
656  }
657)
658
659## 14
660fun_call(
661  argument,
662  parameter =
663    if (condition1)
664      stuff1
665  else if (condition2)
666    stuff3
667  else
668    stuff2
669)
670
671## 15
672object <- fun_call(argument,
673  parameter = if (condition1) {
674    stuff1
675  } else if (condition2) {
676    stuff3
677  } else {
678    stuff2
679  }
680)
681
682## 16
683object <- fun_call(argument, if (condition)
684  stuff1
685  else if (condition2)
686    stuff2
687)
688
689## 17
690while(condition)
691  stuff
692
693## 18
694if (condition1)
695  stuff1
696else
697  if (condition2) {
698    stuff2
699  }
700
701## 19
702object <-
703  if (condition)
704    fun_call()[index]
705else
706  stuff
707
708## 20
709funcall({
710  if (test1)
711    stuff1
712  if (test2)
713    stuff2
714})
715
716## 21
717fun_call(argument,
718  function() {
719
720    if (cond) object1 <- object2
721    else object3 <- object4
722  })
723
724## 22
725if (cond1)
726  if (cond2)
727    if (cond3)
728      stuff1
729else if (cond4)
730  stuff2
731else
732  if (cond5)
733    stuff3
734else
735  stuff4
736else if (cond6)
737  stuff5
738else
739  if (cond7)
740    stuff6
741else
742  stuff7
743else if (cond8)
744  stuff8
745else
746  if (cond9)
747    stuff9
748else
749  stuff10
750
751## 23
752if (cond1)
753  if (cond2)
754    for (sequence1)
755      if (cond3)
756        stuff1
757else
758  stuff2
759else if (cond4)
760  for (sequence2)
761    stuff3
762else
763  if (cond5)
764    fun_call(
765      argument
766    )
767else
768  stuff5
769else
770  stuff6
771
772## 24
773object <- if(cond)
774            stuff1
775else
776  stuff2
777
778## 25
779if (condition) {
780  (stuff)
781}
782
783## 26
784{
785  if (condition1)
786    stuff1
787  else if (condition2) {
788    stuff2
789  }
790  else if (condition3)
791    stuff3
792}
793
794## 27
795object <- if (condition) {
796  stuff1
797}
798else {
799  stuff2
800}
801
802## 28
803if (condition)
804  object <-
805    stuff
806
807## 29
808if (condition1)
809  object <-
810    if (condition2)
811      stuff1
812else
813  stuff2
814else
815  stuff3
816
817
818### Continuation lines
819
820## 1
821stuff1 %>%
822  stuff2 %>%
823  stuff3
824
825## 2
826{
827  stuff1 %>%
828    stuff2 %>%
829    stuff3
830} %>%
831  stuff4 %>%
832  stuff5
833
834## 3
835(
836  stuff1 %>%
837    stuff2 %>%
838    stuff3
839) %>%
840  stuff4 %>%
841  stuff5
842
843## 4
844object[
845  stuff1 %>%
846    stuff2 %>%
847    stuff3
848] %>%
849  stuff4 %>%
850  stuff5
851
852## 5
853stuff1 %>%
854  stuff2 %>%
855  if (condition) {
856    stuff3 %>%
857      stuff4 %>%
858      stuff5
859  } else {
860    stuff6 %>%
861      stuff7 %>%
862      stuff8
863  } %>%
864  stuff9 %>%
865  stuff10
866
867## 6
868stuff[stuff1 %>%
869        stuff2 %>%
870        stuff3] %>%
871  stuff4 %>%
872  stuff5
873
874## 7
875ggplot() +
876  geom(lhs -
877         rhs
878  ) +
879  geom()
880
881## 8
882{
883  ggplot() +
884    geom1(argument1,
885      argument2 = (
886        stuff1
887      ) -
888        stuff2) +
889    geom2() +
890    geom3()
891}
892
893## 9
894stuff +
895  fun_call(parameter = argument1,
896    fun_call((stuff1 - stuff2 +
897                stuff3
898    ) /
899      stuff4)
900  ) /
901  stuff5
902
903fun_call(arg1 +
904           arg2, arg3 +
905                   arg4)
906
907## 10
908fun_call(argument1 %>%
909           stuff1, argument2 %>%
910                     stuff2, {
911                       stuff3 %>%
912                         stuff4
913                     } %>%
914                       stuff5,
915           argument3
916)
917
918## 11
919object1 <- object2 %>%
920  fun_call1() %>%
921  fun_call2()
922
923## 12
924object1 <-
925  object2%>%fun_call1() %>%
926  fun_call2()%>%
927  fun_call3()
928
929## 13
930{
931  (stuff) %>%
932    fun_call()
933
934  {stuff} %>%
935    fun_call()
936}
937
938## 14
939{
940  object + (
941    stuff
942  ) %>%
943    fun_call()
944
945  object + {
946    stuff
947  } %>%
948    fun_call()
949}
950
951## 15
952object <-
953  stuff1 +
954  stuff2 ~
955    stuff3 +
956    stuff4 :=
957      stuff5 +
958      stuff6 =
959        stuff7 +
960        stuff8
961
962## 16
963object <- stuff1 +
964  stuff2 + stuff3 +
965  stuff4 ~ stuff5 +
966    stuff6 + stuff7 +
967    stuff8 := stuff9 +
968      stuff10 + stuff11 +
969      stuff12 = stuff13 +
970        stuff14 + stuff15 +
971        stuff16
972
973## 17
974object %>%
975  {
976    stuff1
977  } %>% object[index] %>% {stuff2} %>% fun_call1() +
978  {if (condition1) stuff3 else stuff4} +
979  if (condition2) {
980    stuff5
981  } else if (condition3) {
982    stuff6
983  } else {
984    stuff7
985  } %>%
986  (fun_call2()) %>% fun_call3() %>%
987  fun_call3()
988
989## 18
990`object`$`elem` <- stuff1 +
991  stuff2
992`object`@`elem` <- stuff1 +
993  stuff2
994
995## 19
996{
997  ## comment
998  object1 <-
999    object2
1000}
1001
1002## 20
1003fun_call(stuff1 + stuff2 +
1004           stuff3 +
1005           (stuff4 + stuff5 +
1006              stuff6) +
1007           object[stuff7 +
1008                    stuff8] +
1009           {stuff9 +
1010              stuff10})
1011
1012
1013## 21
1014object %>% fun_call({
1015  stuff1
1016}) %>%
1017  stuff2
1018
1019## 22
1020"string1" %>%
1021  'string2' %>%
1022  `stuff1` %>%
1023  stuff2
1024
1025## 23
1026object[index] %>%
1027  fun_call1(
1028    argument1
1029  )[index2] %>%
1030  fun_call2(
1031    argument2
1032  )[[index3]] %>%
1033  stuff
1034
1035## 24
1036fun_call(argument) <-
1037  hop
1038
1039## 25
1040fun_call1(argument, fun_call2(
1041  stuff1
1042) +
1043  stuff2)
1044
1045## 26
1046object <-
1047  {
1048    stuff1
1049  } %>%
1050  (
1051    stuff2
1052  )
1053
1054## 27
1055fun_call1(fun_call2(fun_call3(
1056  argument
1057))) %>%
1058  fun_call2()
1059
1060## 28
1061fun_call(argument1 %>%
1062           stuff,
1063  argument2)
1064
1065## 29
1066fun_call(stuff1 :=
1067           (stuff2),
1068  argument)
1069
1070## 30
1071fun_call1(fun_call2(
1072  fun_call3()) %>%
1073    stuff)
1074
1075## 31
1076fun_call(object1 + object2 ~ object3 +
1077           object4 + object5 := object6 +
1078             object7,
1079           argument)
1080
1081## 32
1082fun_call(object ~
1083           )
1084
1085## 33
1086fun_call(object +
1087           )
1088
1089## 34
1090fun_call(object[index1]$element[index2][index3]@attribute +
1091           stuff)
1092
1093## 35a
1094fun_call(argument <-
1095           object)
1096
1097## 35b
1098fun_call(argument <<-
1099           object)
1100
1101## 36
1102funcall(!stuff1 ||
1103          stuff2)
1104
1105
1106### Comments
1107
1108## 1
1109# Side comment
1110
1111## 2
1112{
1113  ## Hanging comment 1
1114  fun_call(
1115  {
1116    ## Hanging comment 2
1117  }
1118  )
1119}
1120
1121## 3
1122{
1123  ### Section comment
1124}
1125
1126## 4
1127fun_call(
1128  ## Comment
1129  argument
1130)
1131
1132## 5
1133object %>%
1134  ## comment,
1135  ## comment
1136  stuff
1137
1138## 6a
1139object <-
1140  function()
1141  {
1142    stuff
1143    ## comment
1144  }
1145
1146## 6b
1147object <-
1148  function()
1149  {
1150    ## comment
1151  }
1152
1153## 7
1154{
1155  fun_call(lhs +
1156             ### Comment
1157             rhs
1158  )
1159}
1160
1161## 8
1162"#" + # comment
1163  NULL
1164
1165## 9
1166"* #"
1167x
1168
1169
1170### Logical operators
1171
1172## 1
1173stuff1 &&
1174  stuff2 ||
1175  stuff3
1176
1177## 2
1178(stuff1 &&
1179   stuff2 ||
1180   stuff3)
1181
1182## 3
1183if (condition1 &&
1184      condition2 ||
1185      (condition3 && condition4) ||
1186      (condition5 &&
1187         condition6 &&
1188         condition7) ||
1189      condition8) {
1190  stuff
1191} && condition8 ||
1192  condition9 ||
1193  condition10
1194
1195## 4
1196stuff1 == stuff2 ||
1197  condition
1198
1199## 5
1200(stuff1 == stuff2 ||
1201   condition
1202)
1203
1204## 6
1205(stuff1 != stuff2 ||
1206   condition
1207)
1208
1209## 7
1210object <-
1211  condition1 | condition2 |
1212  condition3 | condition4
1213
1214## 8
1215if (condition1 || object1 %op% object2 ||
1216      condition3) {
1217  stuff
1218}
1219
1220## 9
1221any(condition1 |
1222      condition2) &&
1223  all(condition3 &
1224        condition4)
1225
1226
1227### Specific situations and overrides
1228
1229## 1
1230fun_call(
1231  ifelse(condition1, argument1,
1232    ifelse(condition2, argument2,
1233      ifelse))
1234)
1235