-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
304 lines (269 loc) · 12.7 KB
/
index.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
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
---
title: ""
subtitle: " "
output: html_document
---
```{r, include = FALSE}
#need these maybe to create lists of attributes about the repos:
#r-on-github - to list functions within repos....etc.
#gh - to list repos
#maybe just search websites?
```
```{r, include = FALSE, eval = FALSE}
library(tidyverse)
library(magrittr)
#getting the webpage
#URL_OCS <- c("https://github.com/opencasestudies?tab=repositories") #old version
URL_OCS <- c("https://github.com/orgs/opencasestudies/repositories")
webpage <- URL_OCS %>%
xml2::read_html()
webpage
#selecting mostly just the repoinfo
webpage_element <- webpage %>%
rvest::html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "d-inline-block", " " ))]')
attrs_ocs <- webpage_element %>%
rvest::html_attrs()
text_ocs <-webpage_element %>%
rvest::html_text()
#getting the names of the repos from the GitHUB page
repo_text<-map_df(attrs_ocs, as_tibble)
repo_text %<>%
mutate(is_case_study = str_detect(value, "opencasestudies/ocs")) %>%
filter(is_case_study == "TRUE") %>%
mutate(is_hovercard = str_detect(value, "hover")) %>%
filter(is_hovercard == "FALSE") %>%
mutate(case_study_name = str_remove(value, "/opencasestudies/")) %>%
mutate(interactive = str_detect(value, "interactive")) %>%
filter(interactive == FALSE) %>%
select(repo_name = value, case_study_name)
repo_non_bp <- repo_text %>% filter(!str_detect(repo_name, "-bp"))
repo_bp <- repo_text %>% filter(str_detect(repo_name, "-bp")) %>%
filter(!str_detect(repo_name, "template|firearm|flex|v2"))
# run the fist chunk first!
repo_non_bp
repo_non_bp <- repo_non_bp %>% filter(!str_detect(repo_name, "firearm")) # get just the health expenditure case study for now - not firearm legislation
# add the health expenditure case study to the list
repo_bp <-rbind(repo_bp, repo_non_bp)
```
```{r, include = FALSE, eval = FALSE}
# create table about repo and html location and names for case studies
OCS_info <- repo_bp %>%
tibble(
repo_url = paste0("https://github.com/", repo_name),
case_study_url = paste0("https://www.opencasestudies.org/", case_study_name))
```
```{r, include = FALSE, eval = FALSE}
#Get Readmes - one example
URL_readme <-c('https://github.com/opencasestudies/ocs-bp-co2-emissions')
readme_ex <-xml2::read_html(URL_readme)
webpage_element <- readme_ex %>%
rvest::html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "container-lg", " " ))]')
text_ocs <- webpage_element %>%
rvest::html_text()
text_ocs %<>% str_split("\n") %>%
as_tibble(unlist(.), .name_repair = ~ c("Text"))
text_ocs %>% mutate(near_title = str_detect(string = Text, pattern = "Title"))
text_ocs
```
```{r, include = FALSE, eval = FALSE}
##in case we need something else from the readme
# get_readme <- function(URL){
# xml2::read_html(URL) %>%
# rvest::html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "container-lg", " " ))]') %>%
# rvest::html_text()}
#
# URL <- OCS_info$repo_url
# Readmes <-pmap(list(URL), get_readme)
```
```{r, include = FALSE, eval = FALSE}
get_title <-function(URL){
xml2::read_html(URL) %>%
rvest::html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "toc-ignore", " " ))]') %>%
rvest::html_text()
}
URL <- OCS_info$case_study_url
Titles <-pmap(list(URL), get_title)
Titles %<>% str_remove("Open Case Studies: ") %>%
str_remove("Open Case Studies : ") %>%
as_tibble()
```
```{r, include = FALSE, eval = FALSE}
URL_readme <-c('https://github.com/opencasestudies/ocs-bp-RTC-Wrangling')
Packages_used <- xml2::read_html(URL_readme) %>%
rvest::html_nodes(xpath='//td | //th') %>%
rvest::html_text() %>%
as_tibble(unlist(.), .name_repair = ~c("Packages")) %>%
mutate(is_package = !str_detect(Packages, " |Package")) %>% # if there is a space or the word package = FALSE
filter(is_package == "TRUE") %>%
select(Packages) %>%
filter(Packages != "Data")
Get_packages <-function(URL){
xml2::read_html(URL) %>%
rvest::html_nodes(xpath='//td | //th') %>%
rvest::html_text() %>%
as_tibble(unlist(.), .name_repair = ~c("Packages")) %>%
mutate(is_package = !str_detect(Packages, " |Package")) %>% # if there is a space or the word package = FALSE
filter(is_package == "TRUE") %>%
select(Packages) %>%
filter(Packages != "Data") %>%
filter(Packages != "Link") %>%
filter(Packages != "link") %>%
filter(Packages != "Use") %>%
arrange(Packages)
}
URL <- OCS_info$repo_url
Packages<-pmap(list(URL), Get_packages)
```
```{r, include = FALSE, eval = FALSE}
#get Data Science objectives
URL_readme <-c('https://github.com/opencasestudies/ocs-bp-co2-emissions')
Objectives<- xml2::read_html(URL_readme) %>%
rvest::html_nodes(xpath='//li') %>%
rvest::html_text()
Obj_and_quest <- Objectives %>% as_tibble() %>%
filter(!str_detect(value, "(?<= )\n{2,}|(\n(?=[:space:]))|→")) %>%
filter(str_detect(value, "[:space:]")) %>%
filter(!str_detect(value, "GitHub|Sales|plans|management|guides|Code review|forum|Lab"))
Questions <- Obj_and_quest %>% filter(str_detect(value, "\\?"))
Objectives <- Obj_and_quest %>% filter(!str_detect(value, "\\?"))
#create functions to do this:
get_questions <- function(URL){
xml2::read_html(URL) %>%
rvest::html_nodes(xpath='//li') %>%
rvest::html_text() %>%
as_tibble() %>%
filter(!str_detect(value, "(?<= )\n{2,}|(\n(?=[:space:]))|→")) %>%
filter(str_detect(value, "[:space:]")) %>%
filter(!str_detect(value, "GitHub|Sales|plans|management|guides|Code review|forum|Lab"))%>%
filter(str_detect(value, "\\?"))%>%
mutate(value = str_remove(value, "\n"))
}
get_objectives <- function(URL){
xml2::read_html(URL) %>%
rvest::html_nodes(xpath='//li') %>%
rvest::html_text() %>%
as_tibble() %>%
filter(!str_detect(value, "(?<= )\n{2,}|(\n(?=[:space:]))|→")) %>%
filter(str_detect(value, "[:space:]")) %>%
filter(!str_detect(value, "GitHub|Sales|plans|management|guides|Code review|forum|Lab"))%>%
filter(!str_detect(value, "\\?")) %>%
mutate(value = str_remove(value, "\n")) %>%
filter(!str_detect(value, "Donohue|Lott|Census|Kristen|themagick|compositionsfor|Bureau|A |guide|discussion|Greatvideo|More examples|consciousness|erratic|bluish|limp|snore|clammy|purplish|modifythese|acrossyears|unable to talk|Anexplanation|Alist|cheatsheets|Acourse|http|Health Insurance|Health Care"))
}
```
```{r, include = FALSE, eval = FALSE}
library(magrittr)
URL <- OCS_info$repo_url
Questions<-pmap(list(URL), get_questions)
Objectives <-pmap(list(URL), get_objectives)
#Collapse the individual tibbles into strings
make_string <- function(vector){
toString(pull(vector))
}
Packages_again <- pmap(list(Packages), make_string) %>% unlist()
Obj_again <- pmap(list(Objectives), make_string) %>% unlist()
Questions_again <- pmap(list(Questions), make_string) %>% unlist()
#Combine all the info and make the links with the appropriate names and open in a new tab (target="_blank")
OCS_info %<>%
mutate(Packages = Packages_again,
Title = Titles$value,
Questions = Questions_again,
Objectives = Obj_again)
OCS_info %<>%
# mutate(`GitHub Repository` = paste0('<a href="',repo_url,'"',' target="_blank"','>',repo_name,"</a>"), # older version... before just showing "static version as text for link"
mutate(`GitHub Repository` = paste0('<a href="', repo_url,'"',' target="_blank"','>',"Static Version Repository","</a>"),
`Case Study` = paste0('<a href="',case_study_url,'"',' target="_blank"','>',Title,"</a>"))
OCS_info %<>%
mutate(Category = case_when(str_detect(string = case_study_name, pattern = "bp") ~ 'Bloomberg American Health Initiative <br></br><img src="https://opencasestudies.github.io/img/icon-bahi.png" height="52"> </img>', TRUE ~ 'General <br></br><img src="https://opencasestudies.github.io/img/icon.png" height="52"> </img>' ))
save(OCS_info, file = here::here( "ocs_info2.rda"))
```
```{r table-display-prep, include = FALSE}
#Start here!
library(tidyverse)
library(widgetframe)
library(magrittr)
```
```{r, include = FALSE}
library(RCurl)
load(file = here::here("ocs_info2.rda"))
#First create url for interactive version
OCS_info %<>%
mutate(Interactive_url = paste0(str_replace( string = case_study_url, pattern = "https://www.opencasestudies.org/", replacement = "https://rsconnect.biostat.jhsph.edu/"), "-interactive"))
# Then add image for link
OCS_info %<>%
mutate(Interactive_version = paste0('<br></br>', '<a href="', Interactive_url,'"',' target="_blank"','>', 'Interactive Version:<br></br><img src=\"https://rsconnect.biostat.jhsph.edu/ocs-bp-opioid-rural-urban-interactive/_w_fa6f6915/www/img/interactive_logo.png" height=\"52\"> </img>', '</a>'))
# add bloomberg website link
OCS_info %<>% mutate(Category = paste0('<br></br> <a href=\"https://americanhealth.jhu.edu/open-case-studies" target=\"_blank\"> <br></br>', Category))
```
```{r, include = FALSE}
#Adding static version text
OCS_info %<>% mutate(Static_version = paste0('<br></br>', '<a href="', case_study_url,'"',' target="_blank"','>', 'Static Version:<br></br><img src=\"https://opencasestudies.github.io/img/icon-bahi.png" height=\"52\"></img></a>'))
OCS_info %<>% mutate('Case Study' =paste0(OCS_info$Title, OCS_info$Static_version))
#Only add interactive version if url exists
OCS_info %<>% mutate(`Case Study` = case_when(
url.exists(Interactive_url) == TRUE ~ paste0(`Case Study`, Interactive_version),
url.exists(Interactive_url) == FALSE ~ `Case Study`))
```
```{r, include = FALSE}
save(OCS_info, file = here::here( "ocs_info_int.rda"))
```
```{r, include = FALSE}
# adding interactive repo link if separate repo
load(file = here::here("ocs_info_int.rda"))
OCS_info %<>% mutate(int_repo_url = paste0(repo_url, "-interactive"))
OCS_info %<>% mutate(int_repo_version = paste0('<a href="', int_repo_url,'"',' target="_blank"','>',"<br></br>Interactive Version Repository","</a>"))
#Only add interactive version repo if url exists
OCS_info %<>% mutate(`GitHub Repository` = case_when(
url.exists(int_repo_url) == TRUE & str_detect(pattern = "Interactive", OCS_info$`Case Study`) == TRUE ~ paste0(`GitHub Repository`, int_repo_version),
TRUE ~ `GitHub Repository`))
```
```{r, include = FALSE}
#adding interactive repo link if branch and already not independent int repo
OCS_info %<>% mutate(int_repo_branch = paste0(repo_url, "/tree/interactive"))
OCS_info %<>% mutate(int_repo_version = paste0('<a href="', int_repo_branch,'"',' target="_blank"','>',"<br></br>Interactive Version Repository","</a>"))
#Only add interactive version repo if url exists
OCS_info %<>% mutate(`GitHub Repository` = case_when(
url.exists(int_repo_branch) == TRUE & str_detect(pattern = "Interactive", OCS_info$`Case Study`) == TRUE & str_detect(pattern = "Interactive", OCS_info$`GitHub Repository`) == FALSE ~ paste0(`GitHub Repository`, int_repo_version),
TRUE ~ `GitHub Repository`))
```
```{r, include = FALSE}
#change health expenditure icon for static version
OCS_info %<>% mutate('Case Study' = case_when(
str_detect(Category, pattern = "General") == TRUE ~ str_replace(string =OCS_info$`Case Study`, pattern = "icon-bahi.png", replacement = "icon.png"),
TRUE ~OCS_info$'Case Study'))
save(OCS_info, file = here::here( "ocs_info_int_with_repo.rda"))
```
```{r table-display, echo = FALSE}
library(DT)
library(dplyr)
load(file = here::here("ocs_info_int_with_repo.rda"))
OCS_info_table <- OCS_info %>%
select(`Case Study`, `GitHub Repository`, Packages, Objectives, Category)%>%
DT::datatable(
rownames = FALSE,
#caption = 'Table 1: This is a simple caption for the table.',
class = 'cell-border stripe',
escape = FALSE,
filter = "top",
extensions = list("Scroller"),
# options = list(autoWidth = FALSE,
# columnDefs = list(width = '300px', width = '200px', width = '90px', width = '200px', width = '100px'),
options = list(autoWidth = TRUE,
columnDefs = list(list(targets=c(0), visible=TRUE, width='120'),
list(targets=c(1), visible=TRUE, width='120'),
list(targets=c(2), visible=TRUE, width='110'),
list(targets=c(3), visible=TRUE, width='260'),
list(targets=c(4), visible=TRUE, width='100'),
list(targets='_all', visible=FALSE)),
pageLength = 10, scrollY = TRUE, scrollX= TRUE, scrollCollapse = TRUE, fillContainer = TRUE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#888888', 'color': '#fff'});",
"}"))
)
```
<div style = "width:100%; height:auto; margin: 0; padding:10px">
``` {r fig1, echo = FALSE, include=TRUE}
OCS_info_table
```
</div>