-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodular_code.qmd
More file actions
129 lines (121 loc) · 2.36 KB
/
modular_code.qmd
File metadata and controls
129 lines (121 loc) · 2.36 KB
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
```{r}
calculate_stats <- function(data) {
mean_value <- mean(data)
sd_value <- sd(data)
return(list(mean = mean_value, sd = sd_value))
}
```
```{r}
# data_loading.r
load_data <- function(file_path) {
# Code to load data from a file
}
# data_preprocessing.r
preprocess_data <- function(data) {
# Code to preprocess data
}
# data_visualization.r
visualize_data <- function(data) {
# Code to visualize data
}
```
```{r}
source(data_loading.r, local = TRUE)
source(data_preprocessing.r, local = TRUE)
source(data_visualization.r, local = TRUE)
```
```{r}
# name.csv
Dima # CSV file named "name.csv" with a single entry, "Dima".
```
```{r}
# load_data.r
# Read `name.csv` and get the name of the column (name is stored and
# column name).
my_name <- read.csv("name.csv", header = FALSE)
```
```{r}
# add_title.r
source("load_data.r")
# Defines a function called `add_title`, which concatenates a title
# and name to create a new string.
add_title <- function(name, title) {
return(paste(title, name))
}
```
```{r}
# say_hi.r
# Defines a function called `say_hi`, which concatenates a name and
# "Hi".
say_hi <- function(name) {
return(paste("Hi", name))
}
```
```{r}
# main.r
# Named "main.r," loads the `say_hi` and `add_title` modules from
# their respective files using the source function.
source("say_hi.r")
source("add_title.r")
title <- "Mr."
add_title(myName, title) |>
say_hi() |>
print()
```
```{r}
#| eval: true
#| echo: false
print("Hi Mr. Dima")
```
```{r}
# say_hi.r
#' @export
say_hi <- function(name) {
return(paste("Hi", name))
}
```
```{r}
library(ggplot2)
```
```{r}
box::use(ggplot2[...])
```
```{r}
# load_data.r
# Load `read.csv` function from `utils` and rename it to `read`
box::use(
utils[read = read.csv]
)
#' @export
my_name <- read("name.csv", header = FALSE)
```
```{r}
# add_title.r
#' @export
add_title <- function(name, title) {
return(paste(title, name))
}
```
```{r}
# main.r
box::use( # Load the files
./say_hi,
./add_title,
./load_data[my_name]
)
box::use(
stringr[str_split_1] # Load function from `stringr` package.
)
title <- "Mr."
# Access the function and the variable using `module$variable`.
add_title$add_title(my_name, title) |>
say_hi$say_hi() |>
# `str_split_1` splits a string by space and returns a character
# vector.
str_split_1(pattern = " ")
```
```{r}
#| eval: true
#| echo: false
"Hi Mr. Dima !" |> stringr::str_split_1(pattern = " ")
```