-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathb_dev.Rmd
183 lines (124 loc) · 5.12 KB
/
b_dev.Rmd
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
---
title: "b_dev"
author: "Colin Fay"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{b_dev}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<script type="text/javascript">
$(document).ready(function () {
window.initializeCodeFolding("show" === "show");
});
</script>
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
## Day to day development with `{golem}`
Now that you're all set with your project init, time to move to development!
App development should happen through the `dev/02_dev.R` file, which contains common commands for developing.
## Launching the app
To run the app, go to the `dev/run_dev.R` file, and run the all thing.
## `dev/02_dev.R`
### Add dependencies
You can use `attachment::att_amend_desc()` to parse the code in your `.R` files, detect `@import` `@importFrom` and `::`, and fill your `DESCRIPTION` accordingly.
Note that the `{attachment}` package should be installed on your machine.
```{r}
attachment::att_amend_desc()
```
About [package dependencies](https://r-pkgs.org/dependencies-mindset-background.html).
### Add modules
The `golem::add_module()` functions creates a module in the `R` folder. The file and the modules will be named after the `name` parameter, by adding `mod_` to the R file, and `mod_*_ui` and `mod_*_server` to the UI and server functions.
```{r}
golem::add_module(name = "my_first_module") # Name of the module
```
The new file will contain:
```{r}
# mod_UI
mod_my_first_module_ui <- function(id) {
ns <- NS(id)
tagList()
}
mod_my_first_module_server <- function(input, output, session) {
ns <- session$ns
}
## To be copied in the UI
# mod_my_first_module_ui("my_first_module_1")
## To be copied in the server
# callModule(mod_my_first_module_server, "my_first_module_1")
```
At the end of the file, you will find a piece of code that has to be copied and pasted inside your UI and server functions.
## Add function files
```{r}
golem::add_fct("helpers")
golem::add_utils("helpers")
```
These two function create `R/fct_helpers.R` and `R/utils_helpers.R`, two file you can use to add business logic functions.
### Add external files
These functions create external dependencies (JavaScript, CSS and Sass). `add_js_file()` creates a simple JavaScript file, while `add_js_handler()` adds a file with a skeleton for shiny custom handlers.
```{r}
golem::add_js_file("script")
golem::add_js_handler("script")
golem::add_css_file("custom")
golem::add_sass_file("custom")
```
Note: While the general philosophy of `{golem}` is being based on the idea that you're building a package, these functions can be used outside of a `{golem}` project.
Note that you can also download external CSS and JavaScript files with:
```{r}
golem::use_external_css_file(url = "url", name = "your_provided_name")
golem::use_external_js_file(url = "url", name = "your_provided_name")
```
The above has the effect of downloading the file at the specified url and giving it a provided name. If the intent is to use a CDN hosted CSS/JS file then manually add the tag - `tags$script(src = "source_of_ur_css/js")` in the function `golem_add_external_resources` in file `app-ui.R`.
The tag can be added inside the `tags$head(...)` if the intent is to load the js/css file in the `<head>` of the document or outside it if it is intended in the `<body>`.
### Adding these external resources to your app
You can add any external resource into `inst/app/www`.
JavaScript and CSS are automatically linked in the `golem_add_external_resources()` function. If you add other resources (example images), you can link them in the app with the `www` prefix:
```{r}
tags$img(src = "www/my.png")
```
You can also list here the use of other packages, for example `useShinyalert()` from the `{shinyalert}` package.
### Add a data-raw folder
If you have data in your package:
```{r}
usethis::use_data_raw()
```
About [data in a package](https://r-pkgs.org/data.html).
### Add tests
Add more tests to your application:
```{r}
usethis::use_test("app")
```
About [testing a package](https://r-pkgs.org/testing-basics.html).
## Documentation
### Vignette
```{r}
usethis::use_vignette("shinyexample")
devtools::build_vignettes()
```
About [package Vignette](https://r-pkgs.org/vignettes.html).
### Code coverage
```{r}
usethis::use_coverage()
```
## Using `{golem}` dev functions
There's a series of tools to make your app behave differently whether it's in dev or prod mode. Notably, the `app_prod()` and `app_dev()` function tests for `options( "golem.app.prod")` (or return TRUE if this option doesn't exist).
Setting this options at the beginning of your dev process allows to make your app behave in a specific way when you are in dev mode. For example, printing message to the console with `cat_dev()`.
```{r eval = TRUE}
options("golem.app.prod" = TRUE)
golem::cat_dev("hey\n")
options("golem.app.prod" = FALSE)
golem::cat_dev("hey\n")
```
You can then make any function being "dev-dependent" with the `make_dev()` function:
```{r eval = TRUE}
log_dev <- golem::make_dev(log)
log_dev(10)
options("golem.app.prod" = TRUE)
log_dev(10)
```