-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.R
81 lines (74 loc) · 2.7 KB
/
server.R
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
library(shiny)
library(magrittr)
packages <- "www/packages.csv" %>%
readr::read_csv(col_types = "ccccccc") %>%
dplyr::distinct(view, package, .keep_all = TRUE) %>%
dplyr::mutate(license = sub("( [+|] )?file LICEN[SC]E", "", license, ignore.case = FALSE)) %>%
dplyr::select(view, package, title, license, description, url, authors)
decorate_url <- function(url) {
return(paste0("<a href=\"", url, "\">", sub("^(https?://)?(www.)?(.*)", "\\3", url), "</a>"))
}
make_url <- function(url) {
regex_url <- "(https?://)?[-[:alnum:]]+\\.[^\\s,]+"
url <- stringr::str_replace_all(url, ",\\s", "\n")
return(stringr::str_replace_all(url, regex_url, decorate_url))
}
shinyServer(function(input, output) {
dt <- reactiveVal()
observe({
if (input$task) {
if (length(input$view) > 1) {
temporary <- packages[packages$view %in% input$view, ]
if (any(duplicated(temporary$package))) {
# Some packages appear under multiple views, let's consolidate:
temporary <- temporary %>%
dplyr::group_by(package, title, description, url, authors, license) %>%
dplyr::summarize(view = paste0(view, collapse = ", "), views = dplyr::n()) %>%
dplyr::ungroup() %>%
dplyr::select(view, views, package, title, license, authors, description, url)
}
} else if (length(input$view) == 1) {
temporary <- packages %>%
dplyr::filter(view == input$view) %>%
dplyr::select(-view)
} else {
temporary <- NULL
}
} else {
temporary <- packages %>%
dplyr::group_by(package, title, license, authors, description, url) %>%
dplyr::summarize(view = paste0(view, collapse = ", "), views = dplyr::n()) %>%
dplyr::ungroup() %>%
dplyr::select(view, views, package, title, license, authors, description, url) %>%
dplyr::arrange(package)
}
if (!is.null(temporary)) {
if (!"Authors" %in% input$fields) {
temporary %<>% dplyr::select(-authors)
}
if (!"Description" %in% input$fields) {
temporary %<>% dplyr::select(-description)
}
if ("URL" %in% input$fields) {
temporary$url <- make_url(temporary$url)
} else {
temporary %<>% dplyr::select(-url)
}
}
dt(temporary)
})
output$packages <- DT::renderDataTable({
req(dt())
DT::datatable(
dt(),
filter = list(position = "top", clear = FALSE),
rownames = FALSE, escape = FALSE,
options = list(
search = list(regex = TRUE, caseInsensitive = TRUE),
language = list(search = "Filter:"),
pageLength = 10, lengthMenu = c(5, 10, 25, 50, 100),
order = list(list(2, "desc"))
)
)
})
})