Write code faster
Beginners lose time on column names, on the arguments of a function, and on the layout of other people’s code. DataStep Studio for R does these jobs, so you can think about the logic. The keys are on the cheat sheet.
22.1 Autocomplete that knows your data
Section titled “22.1 Autocomplete that knows your data”When the session runs, the editor asks it what fits at the cursor: the functions and their arguments, the packages and their functions, the data frames, and the columns of the data frame that a call or a pipe works on, each with its label. Suggestions open as you type, or with Ctrl+Space.
Run programs/22_3_pct.R once. In line 12, put the cursor inside count( and press Ctrl+Space. The first suggestions are the columns of adsl. dsr complete shows the same list in a terminal (here the first 12 of 62):
$ dsr complete programs/22_3_pct.R:12:9 --n 12programs/22_3_pct.R:12:9: argument in count(), data: adsl (from the session) STUDYID column "Study Identifier" · character · adsl USUBJID column "Unique Subject Identifier" · character · adsl SUBJID column "Subject Identifier for the Study" · character · adsl SITEID column "Study Site Identifier" · character · adsl COUNTRY column "Country" · character · adsl RFSTDTC column "Subject Reference Start Date/Time" · character · adsl RFENDTC column "Subject Reference End Date/Time" · character · adsl RFXSTDTC column "Date/Time of First Study Treatment" · character · adsl RFXENDTC column "Date/Time of Last Study Treatment" · character · adsl RFPENDTC column "Date/Time of End of Participation" · character · adsl SCRFDT column "Screen Failure Date" · Date · adsl FRVDT column "Final Retrieval Visit Date" · Date · adsl ... 50 more (--n)Signature: count(x, ..., wt = NULL, sort = FALSE, name = NULL) (now: x: A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).)COMPLETE: 62 candidates (session)- Hover. Rest the mouse on a function: its help. On a column: its label and its missing values.
- Signature help. Type
if_else(and a hint shows each argument with its help. - Without a session, completion still knows the project’s functions and the packages’ functions.
22.2 Help next to the code
Section titled “22.2 Help next to the code”Put the cursor on a function and press Shift+F1 (DataStep R: Help for the Function at the Cursor). A side panel shows the help page of the installed version of the package: usage, arguments, value, examples. Run example sends the examples to the session. The Search box finds help pages and vignettes of every installed package; the long guides of admiral are under Vignettes.
F1 stays the command palette. dsr syntax gives the arguments of a function in a terminal, before you write the call:
$ dsr syntax dplyr::countdplyr::count (version 1.2.1)
count(x, ..., wt = NULL, sort = FALSE, name = NULL)
Count the observations in each group(The first lines; the rest of the help page follows.)
22.3 Snippets: a clinical program from a short prefix
Section titled “22.3 Snippets: a clinical program from a short prefix”A snippet is a piece of code that the editor writes for you. Type its prefix and press Tab. The snippets of DataStep Studio for R are tested on the pharmaverse data:
| Prefix | What it writes |
|---|---|
dsr-header |
a program header: purpose, inputs, outputs, specification, QC |
adsl-trt, adsl-saffl |
the treatment dates and the safety flag from EX (admiral) |
adsl-agegr |
age groups, with their numbers |
adae-dates, bds-chg, adlb-param, adtte-param |
ADAE dates and TRTEMFL, baseline and change, ADLB, a time-to-event parameter (admiral) |
t-incidence, t-summary, t-shell, t-tte |
table programs: subjects with an event, summary statistics, a table from its shell, Kaplan-Meier |
pry-when |
a stop for one subject (chapter 24) |
test-that |
a testthat test (chapter 26) |
22.4 Format R Program, with its proof
Section titled “22.4 Format R Program, with its proof”This program works, but nobody wants to read it:
# Chapter 22: a program that works, in a layout nobody wants to readlibrary(dplyr)adsl<-pharmaverseadam::adsl|>filter(SAFFL=="Y")adsl|>group_by(TRT01A)|>summarise(n=n(),mean_age=round(mean(AGE),1), over_80=sum(AGE>80))#> # A tibble: 3 x 4#> TRT01A n mean_age over_80#> <chr> <int> <dbl> <int>#> 1 Placebo 86 75.2 30#> 2 Xanomeline High Dose 72 73.8 12#> 3 Xanomeline Low Dose 96 76 35Press Ctrl+Shift+A (Mac: Cmd+Shift+A), Format R Program. With a selection, only those lines change. Shift+Alt+F, VS Code’s own Format Document, uses the same engine. The file then has the tidyverse layout:
# Chapter 22: a program that works, in a layout nobody wants to readlibrary(dplyr)adsl <- pharmaverseadam::adsl |> filter(SAFFL == "Y")adsl |> group_by(TRT01A) |> summarise( n = n(), mean_age = round(mean(AGE), 1), over_80 = sum(AGE > 80) )Before it writes, the formatter compares the program before and after, token by token. A token is one word of the language: a name, an operator, a number, a string. Only when every token and every comment are the same, the file is written. This is the proof that only the layout changed. dsr format --check gives the proof and writes nothing:
$ dsr format programs/22_1_messy.R --check would change: programs/22_1_messy.R (proof: only the layout changed: the 52 tokens and 1 comments are the same)FORMAT: WOULD CHANGE (1 files: 1 would be formatted, 0 already in the style, 0 refused; scope line_breaks: the layout only; indent 2 (styler's defaults), 1 would change; line limit 80: 0 breaks by the wrap step, 0 lines over it before, 0 would stay)dsr format programs/22_1_messy.R writes the file. Cmd/Ctrl+Z takes a format back in the editor. The setting dsr.format.scope says how much may change; the default line_breaks changes the layout only.
22.5 The style check
Section titled “22.5 The style check”Style is how code looks: spaces, names, line length. R’s usual standard is the tidyverse style guide. dsr style (or DataStep R: Check the Style of This File) lists where a file leaves it, each finding with its line and its fix. In the editor, the same findings are in the Problems panel as information.
$ dsr style programs/22_1_messy.Rprograms/22_1_messy.R:3: spacing: no space around <- | fix: dsr format (in the editor: Format Document) puts the spaces; it proves that only the layout changesprograms/22_1_messy.R:4: spacing: no space around |> | fix: as for programs/22_1_messy.R:3programs/22_1_messy.R:5: spacing: no space around = | fix: as for programs/22_1_messy.R:3programs/22_1_messy.R:4: lintr:pipe_continuation: Put a space before `|>` and a new line after it, unless the full pipeline fits on one line. | fix: see ?lintr::pipe_continuation_linterprograms/22_1_messy.R:5: lintr:indentation: Hanging indent should be 34 spaces but is 2 spaces. | fix: see ?lintr::indentation_linterSTYLE: FINDINGS (5 findings in 1 of 1 file: spacing 3, lintr:indentation 1, lintr:pipe_continuation 1; the tidyverse style guide, line length 80; with lintr's default linters; a style finding is not an error. dsr format fixes the layout, dsr style --fix the safe ones)A style finding is never an error. Format fixes the layout. A function without a roxygen header (the #' comment lines above a function, chapter 19) gets the quick fix Insert roxygen header: put the cursor on the function and press Ctrl+. (Mac: Cmd+.).
22.6 Lint: find the typos before you run
Section titled “22.6 Lint: find the typos before you run”Lint reads the code without running it and finds real problems: a function that does not exist, an argument a function does not have, a variable that nobody made. This program has two typos:
# Chapter 22: two typos that dsr lint finds before you run the programlibrary(dplyr)
adsl_saf <- pharmaverseadam::adsl |> filter(SAFFL == "Y")
adsl_saf |> group_by(TRT01A) |> summarise(subjects = n_distnct(USUBJID))#> Error in `summarise()`:#> i In argument: `subjects = n_distnct(USUBJID)`.#> i In group 1: `TRT01A = "Placebo"`.#> Caused by error in `n_distnct()`:#> ! could not find function "n_distnct"The run stopped at the first typo. The file has one more line, nrow(adsl_safe), which R never reached. dsr lint (or DataStep R: Lint the Open R Files) finds both typos at once, with a suggestion for each:
$ dsr lint programs/22_2_typo.Rprograms/22_2_typo.R:10: function: no visible global function definition for ‘n_distnct’ (did you mean n_distinct?)programs/22_2_typo.R:12: variable: no visible binding for global variable ‘adsl_safe’ (did you mean adsl_saf?)LINT: PROBLEMS (1 files, with lintr)On a long program, a typo near the end costs you a whole run. Lint costs a second. Lint each program before you run it, and always before you hand it to QC.
22.7 Move between files
Section titled “22.7 Move between files”A study has many files: functions in one file, a program per data set, a program per table. These keys move between them. They read the files; they need no run.
| Key (Mac: Cmd for Ctrl) | What it does |
|---|---|
| Ctrl+click, Ctrl+Alt+G | go to the definition of a function, a data frame or a column |
| Alt+F12 | peek: the definition in place, under the line |
| Shift+F12 | every use, in every R file of the project |
| Ctrl+Alt+U | Find Every Use: the definition, the uses and, apart, the possible uses |
| Shift+F2 | rename a function or a variable, with a proof |
| Ctrl+Alt+O | the functions, sections and data frames of this file |
| Ctrl+T | a function or a data frame of the whole project, by part of its name |
| Ctrl+Alt+J | back to the line you came from |
In VS Code, F12 is Go to Definition and F2 is Rename. In R files, DataStep Studio for R gives F12 to the data viewer and F2 to clearing the log, as in the SAS tool. So Rename is on Shift+F2 and Go to Definition on Ctrl+click.
Try them on this program:
# Chapter 22: a small function, used twice: go to its definition, find its uses, rename itlibrary(dplyr)
pct_subjects <- function(n, total) { round(100 * n / total, 1)}
adsl <- pharmaverseadam::adsl |> filter(SAFFL == "Y")
adsl |> count(TRT01A, SEX) |> mutate(pct = pct_subjects(n, sum(n)), .by = TRT01A)#> # A tibble: 6 x 4#> TRT01A SEX n pct#> <chr> <chr> <int> <dbl>#> 1 Placebo F 53 61.6#> 2 Placebo M 33 38.4#> 3 Xanomeline High Dose F 35 48.6#> 4 Xanomeline High Dose M 37 51.4#> 5 Xanomeline Low Dose F 55 57.3#> 6 Xanomeline Low Dose M 41 42.7
adsl |> count(AGEGR1) |> mutate(pct = pct_subjects(n, sum(n)))#> # A tibble: 2 x 3#> AGEGR1 n pct#> <chr> <int> <dbl>#> 1 18-64 33 13#> 2 >64 221 87Each move has a dsr command. Where is pct_subjects defined, and where is it used?
$ dsr def pct_subjectsprograms/22_3_pct.R:4 function pct_subjects(n, total) 4 pct_subjects <- function(n, total) { 5 round(100 * n / total, 1) 6 }DEF: FOUND | pct_subjects is a function: programs/22_3_pct.R:4$ dsr refs pct_subjectspct_subjects: function, the projectdefined programs/22_3_pct.R:4:1 pct_subjects <- function(n, total) {use programs/22_3_pct.R:13:16 mutate(pct = pct_subjects(n, sum(n)), .by = TRT01A)use programs/22_3_pct.R:17:16 mutate(pct = pct_subjects(n, sum(n)))REFS: 2 uses in 1 file, 1 definition, 0 possible (pct_subjects: function)A rename changes the name in every file where it means that function, and nothing else. --check shows the plan and writes nothing:
$ dsr rename pct_subjects pct_of_n --checkwould change programs/22_3_pct.R:4:1 pct_of_n <- function(n, total) {would change programs/22_3_pct.R:13:16 mutate(pct = pct_of_n(n, sum(n)), .by = TRT01A)would change programs/22_3_pct.R:17:16 mutate(pct = pct_of_n(n, sum(n)))RENAME: WOULD CHANGE | pct_subjects -> pct_of_n (function, the project): 3 tokens in 1 file (proof: 3 tokens changed (pct_subjects to pct_of_n) in 1 file; the other 82 tokens, every comment and every string are the same; R's parser confirms it)A rename of a column is refused: which data frame a column name belongs to is decided only when the code runs. Use Find Every Use to see the possible places, and change them yourself.
22.8 Enlarge and Optimize Function Call
Section titled “22.8 Enlarge and Optimize Function Call”Two keys show or hide the arguments of a call. They are in the Professional edition, free during Early Access until 17 February 2027.
- Ctrl+Shift+E, Enlarge Function Call: every argument on its own line, with its name, and the arguments you did not give with their defaults.
- Ctrl+Shift+O, Optimize Function Call: the arguments that have their default value out, then the call on one line when it fits.
Both are written only with a proof that the call means the same. In line 14 of 20_1_weight.R, enlarge the if_else() call:
$ dsr call enlarge programs/20_1_weight.R:14:20 --check mutate(WGTGR = if_else( condition = WEIGHTBL >= 70, true = ">=70 kg", false = "<70 kg", missing = NULL, ptype = NULL ))CALL: WOULD BE ENLARGED (programs/20_1_weight.R:14: if_else() of the package dplyr: 3 names written, 2 defaults added (missing, ptype); `...` is not added; size: not added (the default is computed inside the function, or the function asks missing()); proof: the matched call with the defaults filled in is the same before and after; the comments and the code around the call are the same; line limit 80)Now you see an argument you did not know: missing. It is the value for a row where the condition is NA. In chapter 20, the subject without a baseline weight got NA because of it. missing = "no weight" would give that subject a group of its own.
22.9 Summary
Section titled “22.9 Summary”- Ctrl+Space completes the columns of the data frame you work on, with their labels. Shift+F1 opens the help of the installed version.
- Snippets (
adsl-agegr,t-incidence,test-that, …) write tested clinical code from a prefix and Tab. - Ctrl+Shift+A formats a program to the tidyverse layout, and writes only with the proof that only the layout changed.
dsr stylelists where the code leaves the style guide.dsr lintfinds typos and missing functions before you run.- Ctrl+click, Shift+F12 and Shift+F2 move to a definition, list the uses and rename with a proof.
- Ctrl+Shift+E and Ctrl+Shift+O enlarge and optimize a call (Professional).
22.10 Exercises
Section titled “22.10 Exercises”Exercise 22-1. Format 22_1_messy.R with dsr format --check. How many tokens and comments does the proof compare?
Exercise 22-2. Fix the two typos of 22_2_typo.R. What does dsr lint say now, and how many subjects are in each arm?
Exercise 22-3. Rename pct_subjects to pct_of_n with --check. How many tokens would change, and how many stay the same?
Exercise 22-4. Optimize the if_else() call in line 14 of 20_1_weight.R with dsr call optimize ... --check. Does anything change? Why?
The answers are in Answers, Part VI.
Found an error or have a question? Write to datastep.studio@gmail.com· Every program runs in DataStep Studio for R, the R extension for VS Code and Positron.
Data: the CDISC pilot study (cdiscpilot01), from the R packagespharmaversesdtm andpharmaverseadam (Apache License 2.0).
R is a project of the R Foundation. SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. This book is not affiliated with or endorsed by SAS Institute Inc. or the R Foundation. CDISC, SDTM and ADaM are trademarks of the Clinical Data Interchange Standards Consortium.
© 2026 DataStep Labs. All rights reserved.