-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_targets.R
More file actions
57 lines (48 loc) · 1.28 KB
/
Copy path_targets.R
File metadata and controls
57 lines (48 loc) · 1.28 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
library(targets)
# set options
tar_option_set(packages = c("readr", "dplyr", "sentimentr", "here"))
# functions to be used
# load starwars data
load_data = function(file = here::here('materials', 'data', 'starwars_text.csv')) {
read_csv(file,
show_col_types = F)
}
# prepare data
clean_data = function(data) {
data |>
mutate(episode = case_when(document == 'a new hope' ~ 'iv',
document == 'the empire strikes back' ~ 'v',
document == 'return of the jedi' ~ 'vi')) |>
mutate(character = case_when(character == 'BERU' ~ 'AUNT BERU',
character == 'LURE' ~ 'LUKE',
TRUE ~ character)) |>
select(episode, everything())
}
# calculate sentiment
calculate_sentiment = function(data,
by = c("document", "character", "line_number")) {
data |>
sentiment_by(by = by) |>
sentimentr::uncombine()
}
# define targets
list(
tar_target(
name = starwars,
command =
load_data() |>
clean_data()
),
tar_target(
name = sentences,
command =
starwars |>
get_sentences()
),
tar_target(
name = sentiment,
command =
sentences |>
calculate_sentiment()
)
)