-
Notifications
You must be signed in to change notification settings - Fork 755
/
r.py
362 lines (322 loc) · 9.44 KB
/
r.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from talon import Context, actions, settings
ctx = Context()
ctx.matches = r"""
code.language: r
"""
ctx.lists["user.code_common_function"] = {
# base R
"as character": "as.character",
"as data frame": "as.data.frame",
"as date": "as.Date",
"as double": "as.double",
"as factor": "as.factor",
"as integer": "as.integer",
"as numeric": "as.numeric",
"base read RDS": "readRDS",
"base save RDS": "saveRDS",
"cable": "kable",
"correlation": "cor",
"count": "count",
"covariance": "cov",
"describe": "describe",
"eigen": "eigen",
"ex table": "xtable",
"get working directory": "getwd",
"head": "head",
"if else": "ifelse",
"install packages": "install.packages",
"is NA": "is.na",
"is not NA": "!is.na",
"length": "length",
"library": "library",
"list files": "list.files",
"list": "list",
"lm": "lm",
"log": "log",
"make directory": "dir.create",
"margins": "margins",
"max": "max",
"mean": "mean",
"min": "min",
"names": "names",
"paste": "paste0",
"print": "print",
"reorder": "reorder",
"repeat": "rep",
"scale": "scale",
"sequence along": "seq_along",
"sequence length": "seq_len",
"sequence": "seq",
"set working directory": "setwd",
"sort": "sort",
"subset": "subset",
"sum": "sum",
"summary": "summary",
"tail": "tail",
"tidy": "tidy",
"trim white space": "trimws",
"type": "typeof",
"unique": "unique",
"vector": "c",
"vee table": "vtable",
"view": "View",
# dplyr
"anti join": "anti_join",
"arrange": "arrange",
"as tibble": "as_tibble",
"bind rows": "bind_rows",
"case when": "case_when",
"distinct": "distinct",
"everything": "everything",
"filter": "filter",
"full join": "full_join",
"glimpse": "glimpse",
"group by": "group_by",
"inner join": "inner_join",
"left join": "left_join",
"mutate": "mutate",
"pull": "pull",
"rename all": "rename_all",
"rename": "rename",
"right join": "right_join",
"select all": "select_all",
"select": "select",
"semi join": "semi_join",
"starts with": "starts_with",
"summarise": "summarise",
"tibble": "tibble",
"ungroup": "ungroup",
# ggplot2
"coord cartesian": "coor_cartesian",
"element text": "element_text",
"element blank": "element_blank",
"facet grid": "facet_grid",
"facet wrap": "facet_wrap",
"geom A B line": "geom_abline",
"geom area": "geom_area",
"geom bar": "geom_bar",
"geom boxplot": "geom_boxplot",
"geom histogram": "geom_histogram",
"geom horizontal line": "geom_hline",
"geom line": "geom_line",
"geom point": "geom_point",
"geom pointrange": "geom_pointrange",
"geom polygon": "geom_polygon",
"geom ribbon": "geom_ribbon",
"geom segment": "geom_segment",
"geom smooth": "geom_smooth",
"geom vertical line": "geom_vline",
"geom violin": "geom_violin",
"labs": "labs",
"scale colour manual": "scale_colour_manual",
"scale fill manual": "scale_fill_manual",
"scale fill viridis": "scale_fill_viridis_c",
"scale colour viridis": "scale_colour_viridis_c",
"theme set": "theme_set",
# purrr
"map character": "map_chr",
"map data frame": "map_dfr",
"map double": "map_dbl",
"map": "map",
"P map": "pmap",
# stringr
"string contains": "str_detect",
"string detect": "str_detect",
"string replace all": "str_replace_all",
"string replace": "str_replace",
# tidyr
"drop NA": "drop_na",
"gather": "gather",
"nest": "nest",
"pivot longer": "pivot_longer",
"pivot wider": "pivot_wider",
"spread": "spread",
"un nest": "unnest",
# readr, readxl, and other non-base R reading/writing
"read E views": "readEViews",
"read CSV": "read_csv",
"read RDS": "read_rds",
"read excel": "read_xlsx",
"write CSV": "write_csv",
"write RDS": "write_rds",
# Shiny
"shine ui": "shinyUI",
"title panel": "titlePanel",
"main panel": "mainPanel",
"tab panel": "tabPanel",
"navigation list panel": "navlistPanel",
"conditional panel": "conditionalPanel",
"input panel": "inputPanel",
"ui output": "uiOutput",
"text output": "textOutput",
"table output": "tableOutput",
"data table output": "dataTableOutput",
"select size input": "selectizeInput",
"action button": "actionButton",
"download button": "downloadButton",
"render ui": "renderUI",
"observe event": "observeEvent",
# Base
}
ctx.lists["user.code_libraries"] = {
"bayes plot": "bayesplot",
"BRMS": "brms",
"cable": "kable",
"car": "car",
"D plier": "dplyr",
"dev tools": "devtools",
"future": "future",
"furr": "furrr",
"gap minder": "gapminder",
"gee animate": "gganimate",
"gee highlight": "gghighlight",
"gee map": "ggmap",
"gee repel": "ggrepel",
"grid extra": "gridExtra",
"gee gee plot": "ggplot2",
"GLMM TMB": "glmmTMB",
"here": "here",
"knitter": "knitr",
"LME four": "lme4",
"LM test": "lmtest",
"lubridate": "lubridate",
"margins": "margins",
"inla": "INLA",
"NLME": "nlme",
"psych": "psych",
"purr": "purrr",
"R markdown": "rmarkdown",
"R stan": "rstan",
"R stan arm": "rstanarm",
"R color brewer": "RColorBrewer",
"read R": "readr",
"stargazer": "stargazer",
"tidy verse": "tidyverse",
"tidier": "tidyr",
"tidy bayes": "tidybayes",
"TMB": "TMB",
"vee table": "vtable",
"viridis": "viridis",
"viridis light": "viridisLite",
"shiny alert": "shinyalert",
}
ctx.lists["user.code_parameter_name"] = {
"alpha": "alpha",
"breaks": "breaks",
"colour": "colour",
"data": "data",
"fill": "fill",
"H just": "hjust",
"keep": ".keep",
"label": "label",
"labels": "labels",
"log": "log",
"main": "main",
"mapping": "mapping",
"method": "method",
"NA remove": "na.rm",
"path": "path",
"position": "position",
"plex label": "xlab",
"plex limit": "xlim",
"scales": "scales",
"size": "size",
"show legend": "show.legend",
"sort": "sort",
"title": "title",
"type": "type",
"vee just": "vjust",
"width": "width",
"with ties": "with_ties",
"why label": "ylab",
"why limit": "ylim",
"why max": "ymax",
"why min": "ymin",
}
@ctx.action_class("user")
class UserActions:
def code_operator_assignment():
actions.auto_insert(" <- ")
def code_operator_subtraction():
actions.auto_insert(" - ")
def code_operator_addition():
actions.auto_insert(" + ")
def code_operator_multiplication():
actions.auto_insert(" * ")
def code_operator_exponent():
actions.auto_insert(" ** ")
def code_operator_division():
actions.auto_insert(" / ")
def code_operator_modulo():
actions.auto_insert(" %% ")
def code_operator_equal():
actions.auto_insert(" == ")
def code_operator_not_equal():
actions.auto_insert(" != ")
def code_operator_greater_than():
actions.auto_insert(" > ")
def code_operator_greater_than_or_equal_to():
actions.auto_insert(" >= ")
def code_operator_less_than():
actions.auto_insert(" < ")
def code_operator_less_than_or_equal_to():
actions.auto_insert(" <= ")
def code_operator_and():
actions.auto_insert(" & ")
def code_operator_or():
actions.auto_insert(" | ")
def code_operator_bitwise_and():
actions.auto_insert(" & ")
def code_insert_null():
actions.auto_insert("NULL")
def code_state_if():
actions.insert("if () {}")
actions.key("left enter up end left:3")
def code_state_else_if():
actions.insert(" else if () {}")
actions.key("left enter up end left:3")
def code_state_else():
actions.insert(" else {}")
actions.key("left enter")
def code_state_for():
actions.insert("for ( in ) {}")
actions.key("left enter up end left:7")
def code_state_while():
actions.insert("while () {}")
actions.key("left enter up end left:3")
def code_import():
actions.user.insert_between("library(", ")")
def code_comment_line_prefix():
actions.auto_insert("#")
def code_state_return():
actions.user.insert_between("return(", ")")
def code_break():
actions.auto_insert("break")
def code_next():
actions.auto_insert("next")
def code_insert_true():
actions.auto_insert("TRUE")
def code_insert_false():
actions.auto_insert("FALSE")
def code_insert_function(text: str, selection: str):
text += f"({selection or ''})"
actions.user.paste(text)
actions.edit.left()
def code_private_function(text: str):
result = "{} <- function () {{\n\n}}".format(
actions.user.formatted_text(
text, settings.get("user.code_private_function_formatter")
)
)
actions.user.paste(result)
actions.edit.up()
actions.edit.up()
actions.edit.line_end()
actions.edit.left()
actions.edit.left()
actions.edit.left()
def code_insert_library(text: str, selection: str):
actions.user.insert_between("library(", ")")
actions.user.paste(text + selection)
def code_insert_named_argument(parameter_name: str):
actions.insert(f"{parameter_name} = ")