forked from mayank-procogia/shiny-dev-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrshiny_tips_slides.qmd
339 lines (240 loc) · 14.9 KB
/
rshiny_tips_slides.qmd
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
---
title: "<span style='color:white; background-color: #1fb8cc4a; font-size: 80px;'>Supercharge your R Shiny Development Work flow</span>"
subtitle: "<span style='color:white; background-color: #1fb8cc4a; font-size: 24px;'>Tips, Tricks and Techniques for boosting Shiny Development Productivity and Efficiency</span>"
author: "<span style='color:white;'>Mayank Agrawal<br/><span style='font-size: 22px;'>Senior R developer Consultant I<br/>ProCogia</span></span>"
title-slide-attributes:
data-background-image: "data/LinkedIn_Banner_01.png"
data-background-opacity: "0.75"
date: today
date-format: full
format:
revealjs:
footer: "R Ladies NYC - September 24, 2023"
smaller: true
slide-number: true
transition: slide
transition-speed: fast
theme: [default, custom.scss]
editor: visual
self-contained: true
---
## Introduction
- Background: Computer Science Engineer and MBA in Business Analytics.
- Using R and R Shiny for 6+ years.
- Architected, built and managed more than 40+ R Shiny applications and dashboards.
- Built and led teams of 10+ R developers.
- Trained 30+ colleagues on R from diverse backgrounds.
- Have extensive hands on experience on all flavors of R products - Shiny, R Markdowns, Quarto, and Package development.
- Curious to learn.
- Working with [**ProCogia**](https://procogia.com/){target="_blank"}; an Official [full service partner](https://posit.co/certified-partners/){target="_blank"} with Posit.
------------------------------------------------------------------------
## Overview
Hello There!
In this lightning talk, we will touch base on the following high level topics to boost your R development experience.
- Streamlining Workflow Essentials
- Shiny Development Tips
- Productivity Hacks
- Mastering Shortcuts
![](data/logo_route.png){.absolute bottom="5rem" right="0" height="200"}
------------------------------------------------------------------------
## Streamlining Workflow Essentials
- Use RStudio Projects
- Why Should I use it?
- Organize your project structure.
- Use/Create a standard project template.
- Leverage package management tools
- Utilize `renv` or `packrat` to manage package dependencies.
- Ensure reproducibility and minimize version conflicts.
- Version Control with Git
- Track changes, collaborate with team members, and manage project history effectively using Git.
- Use Issues, Pull Requests and Connected Commits for efficient and effortless parallel development.
------------------------------------------------------------------------
### Using RStudio Projects
RStudio projects make it straightforward to divide your work into multiple contexts, each with their own working directory, workspace, history, and source documents.
::: fragment
#### Why should I use it?
- Isolates your development environment.
- Relative path referencing for your project files.
- Helps setup project package management via [`renv`](https://rstudio.github.io/renv/articles/renv.html){target="_blank"} or [`packrat`](https://github.com/rstudio/packrat){target="_blank"}
- Segregate development flow and context.
![](data/question.png){.absolute bottom="20rem" right="0" height="175"}
:::
::: fragment
#### How to create a new RStudio Project?
Use your RStudio IDE Toolbar:
- `File` --\> `New Project` --\> (Choose) `New Directory`/`Existing Directory`/`Version Control`
![](data/creativity.png){.absolute bottom="0" right="0" height="175"}
:::
------------------------------------------------------------------------
### Organize your project structure
::: columns
::: {.column width="50%"}
- Maintain a clear and logical directory structure.
- Group related files together for easy navigation.
- Create, Adopt and Enforce a standard development template for easier developer onboarding and faster development.
- Have a project template repository for initial cloning and usage.
- Please refer to the right image for an illustrative R project directory structure.
:::
::: {.column width="50%"}
![](data/ss_rstudio_dir_tree.png){fig-align="center"}
:::
:::
------------------------------------------------------------------------
## Shiny Development Tips
- Optimize Server logic
- Minimize unnecessary computations and avoid redundant calculations.
- Profile your code to identify bottlenecks with [`profvis`](http://rstudio.github.io/profvis/){target="_blank"}
- Use General Coding principles
- DRY (Don't Repeat Yourself)
- YAGNI (You aren't going to need it)
- KISS (Keep it Simple Silly)
- Document your code for humans.
- Clean Code at all costs
- Separation of Concerns
![](data/logo_idea.png){.absolute bottom="50" right="0" height="200"}
------------------------------------------------------------------------
## Shiny Development Tips
- Utilize Reactive programming
- Use **Waterfall** reactivity model.
- Avoid **Spaghetti Reactivity mess**.
- Leverage Shiny's reactivity to update UI elements dynamically based on user input.
- This enhances interactivity and responsiveness.
- Implement efficient UI components
- Prototype the UI layout using [draw.io](draw.io){target="_blank"} or similar tools.
- Use conditional panels, modules, and well-designed layouts to create a user-friendly interface.
- Iterate rapidly to test how different implementations enhance UI/UX
------------------------------------------------------------------------
## Productivity Hacks
- Debugging like a pro
- Debug via `browser()`, `debug(function_name)`, and debug points.
- Set breakpoints, step through code, and inspect variables to troubleshoot efficiently.
- Profiling and performance tuning
- Use tools like `profvis` to identify performance bottlenecks and Optimize critical sections of your code for speed.
- Use benchmarking techniques to compare computation speeds of various packages. Refer R function `rbenchmark::benchmark()`.
- Automate repetitive tasks with scripts
- Write scripts to automate routine tasks such as data pre-processing, cleaning, visualization and report generation.
- Refactor code into functions with parameters for versatile reusability across projects.
------------------------------------------------------------------------
## Mastering Shortcuts
- RStudio IDE shortcuts
- Learn key shortcuts for common actions like running code, navigating files, and managing tabs.
- Boost your coding speed and efficiency.
- Shiny-specific keyboard tricks
- Use keyboard shortcuts for navigating the app, toggling between UI and server code, and inserting common Shiny functions.
- Time-saving code snippets
- Store frequently used code snippets in RStudio's code snippets pane for quick access.
![](data/logo_energy.png){.absolute bottom="30" right="0" height="150"}
------------------------------------------------------------------------
### Keyboard Shortcuts at a glance
RStudio provides dozens of useful shortcuts that you can access through the menu at the top:
`Tools` \> `Keyboard Shortcuts Help`.
![](data/ss_rstudio_tips_position.png){fig-align="center"}
------------------------------------------------------------------------
## Thank you
- Quarto presentation code available on [GitHub](https://github.com/mayank-procogia/shiny-dev-tips){target="_blank"} at [http://bit.ly/3ZHZTNf](http://bit.ly/3ZHZTNf){target="_blank"}
- Slides available on [GitHub Pages](https://mayank-procogia.github.io/shiny-dev-tips/rshiny_tips_slides.html){target="_blank"} at [https://bit.ly/3RsdExn](https://bit.ly/3RsdExn){target="_blank"}
- Connect and/or send me a DM for a follow up question or catch up
- Linkedin: [mayank-agrawal-7jan](https://www.linkedin.com/in/mayank-agrawal-7jan/){target="_blank"}
- Twitter: [mayank7jan](https://twitter.com/mayank7jan){target="_blank"}
- Mastodon: [mayank7j](https://fosstodon.org/@mayank7j){target="_blank"}
![](data/logo_thank_you_signboard.png){.absolute bottom="5rem" right="0" height="200"}
------------------------------------------------------------------------
## References
- RStudio Shortcuts Cheat sheet:
- RStudio IDE Toolbar: `Help` --\> `Keyboard Shortcuts Help`
- Mac: `Option` + `Shift` + `K`
- Windows: `Alt` + `Shift` + `K`
- Customizing the RStudio IDE: [Link 1](https://support.posit.co/hc/en-us/articles/200549016-Customizing-the-RStudio-IDE) [Link 2](https://support.posit.co/hc/en-us/articles/200711853-Keyboard-Shortcuts-in-the-RStudio-IDE)
- Comprehensive documentation and self help guides for using RStudio IDE [Link](https://support.posit.co/hc/en-us/sections/200107586-Using-the-RStudio-IDE)
- Using RStudio Projects. [Link](https://support.posit.co/hc/en-us/articles/200526207-Using-RStudio-Projects)
- Posit Cheat Sheets [Link](https://posit.co/resources/cheatsheets/)
- Project package management packages - [`renv`](https://rstudio.github.io/renv/articles/renv.html){target="_blank"} and [`packrat`](https://github.com/rstudio/packrat){target="_blank"}
- [`profvis`](http://rstudio.github.io/profvis/){target="_blank"} for code profiling and time estimation
# Appendix
My favourite Keyboard Shortcuts for R Development Workflow
------------------------------------------------------------------------
## Navigate between Window Panes
Knowing how to toggle between panes without touching your mouse to move your cursor will help save time and improve your workflow. Use the following shortcuts to switch between panes seamlessly (Mac/Windows):
- `Control/Ctrl + 1`: *Source editor* (your script)
- `Control/Ctrl + 2`: *Console*
- `Control/Ctrl + 3`: Help
- `Control/Ctrl + 4`: History
- `Control/Ctrl + 5`: *Files*
- `Control/Ctrl + 6`: *Plots*
- `Control/Ctrl + 7`: Packages
- `Control/Ctrl + 8`: Environment
- `Control/Ctrl + 9`: Viewer
------------------------------------------------------------------------
### Single Pane View
If you prefer to only have one pane in view at a time, add `Shift` to any of the previous commands to maximize the pane. Use the following shortcuts to bring a panes into primary focus seamlessly.
- `Control/Ctrl + Shift + 1`: Source editor (your script) pane only
- `Control/Ctrl + Shift + 2`: Console pane only
- `Control/Ctrl + Shift + 3`: Help pane only
- `Control/Ctrl + Shift + 4`: History pane only
- `Control/Ctrl + Shift + 5`: Files pane only
- `Control/Ctrl + Shift + 6`: Plots pane only
- `Control/Ctrl + Shift + 7`: Packages pane only
- `Control/Ctrl + Shift + 8`: Environment pane only
- `Control/Ctrl + Shift + 9`: Viewer pane only
------------------------------------------------------------------------
### The Classic Four Pane View
Wait! How do I go back to the classic four-pane view?
![](data/wait_meme.jpeg){fig-align="center" width="199"}
`Control/Ctrl + Shift + 0` (Mac/Windows): Four-pane RStudio IDE View
------------------------------------------------------------------------
## Console
| **Description** | Windows & Linux | Mac |
|---------------------------------|--------------------|-------------------|
| *Shift focus of cursor to Console* | `Ctrl + 2` | `Control + 2` |
| *Clear Console* | `Ctrl + L` | `Control + L` |
| Move Cursor to beginning of a line | `Home` | `Cmd + Left` |
| Move Cursor to end of a line | `End` | `Cmd + Right` |
| Move Cursor to start/end of a word | `Alt + Left/Rigth` | `Option + Left/Right` |
| Navigate command history | `Up/Down` | `Up/Down` |
| *Pop-up Command history* | `Ctrl + Up` | `Cmd + Up` |
------------------------------------------------------------------------
## Source Scripts
| **Description** | Windows & Linux | Mac |
|---------------------------------|--------------------|--------------------|
| *Toggle document outline* | `Ctrl + Shift + O` | `Cmd + Shift + O` |
| *Clear Console* | `Ctrl + L` | `Control + L` |
| New R Script | `Ctrl + Shift + N` | `Cmd + Shift + N` |
| Save active document | `Ctrl + S` | `Cmd + S` |
| Close active document | `Ctrl + W` | `Cmd + W` |
| Knit and Preview Document (knitr) | `Ctrl + Shift + K` | `Cmd + Shift + K` |
| Insert Chunk (Rmd) | `Ctrl + Alt + I` | `Cmd + Option + I` |
------------------------------------------------------------------------
## Find
| **Description** | Windows & Linux | Mac |
|------------------|--------------------|-------------------|
| Find and Replace | `Ctrl + F` | `Cmd + F` |
| Find in Files | `Ctrl + Shift + F` | `Cmd + Shift + F` |
------------------------------------------------------------------------
## Insert
| **Description** | Windows & Linux | Mac |
|------------------------|-----------------------|-------------------------|
| Insert Assignment Operator (`<-`) | `Alt + - (hyphen/minus)` | `Option + - (hyphen/minus)` |
| Insert Pipe Operator (`%>%`) | `Ctrl + Shift + M` | `Cmd + Shift + M` |
------------------------------------------------------------------------
## Code
| **Description** | Windows & Linux | Mac |
|-------------------------------|-------------------|----------------------|
| *Re-indent lines* | `Ctrl + I` | `Cmd + I` |
| *Comment/un-comment current line/selection* | `Ctrl + Shift + C` | `Cmd + Shift + C` |
| *Reformat Selection* | `Ctrl + Shift + A` | `Cmd + Shift + A` |
| Move Lines Up/Down | `Alt + Up/Down` | `Option + Up/Down` |
| Jump to Matching Brace/Parenthesis | `Ctrl + P` | `Control + P` |
| *Add Cursor via drag* | `Alt + drag mouse pointer` | `Option + drag mouse pointer` |
| *Delete Current Line* | `Ctrl + D` | `Cmd + D` |
| Insert Roxygen Skeleton | `Ctrl + Alt + Shift + R` | `Cmd + Option + Shift + R` |
------------------------------------------------------------------------
## Terminal
| **Description** | Windows & Linux | Mac |
|------------------------|-------------------|----------------------|
| Move Focus to Terminal | `Alt + Shift + M` | `Shift + Option + M` |
| New Terminal | `Alt + Shift + R` | `Shift + Option + R` |
------------------------------------------------------------------------
## Session
| **Description** | Windows & Linux | Mac |
|-------------------|--------------------|-------------------|
| Restart R session | `Ctrl + Shift + 0` | `Cmd + Shift + 0` |