-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
94 lines (69 loc) · 2.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
library(shiny)
library(dplyr)
library(jsonlite)
library(visNetwork)
shinyServer(function(input, output) {
output$track_view <- renderVisNetwork({
# Create a Progress object
progress <- shiny::Progress$new()
# Make sure it closes when reactive is exited, even if there's an error
on.exit(progress$close())
progress$set(value = 0.3, message = "Creating visualization",
detail = "Reading")
if (input$config_mode == "example") {
# Check for input
shiny::validate(
need(input$config_example != 'none', message = "Please choose an example config")
)
config <- fromJSON(paste0(input$config_example, ".json"))
} else {
# Check for input
shiny::validate(
need(input$config_file, message = "Please provide a 'config.json' file")
)
# Attempt to load JSON
try(config <- fromJSON(input$config_file$datapath), silent = TRUE)
}
# Check that input is valid
shiny::validate(
need(exists("config"), message = "Problem with file/invalid JSON")
)
progress$set(value = 0.5, detail = "Parsing")
if (is.null(config$exercises$deprecated)) {
config$exercises$deprecated <- NA
}
# Build nodes data frame from exercise data
nodes <- config$exercises %>%
rename(id = slug, value = difficulty) %>%
mutate(label = id,
group = case_when(
deprecated ~ "deprecated",
core ~ "core",
is.na(unlocked_by) ~ "floating",
!is.na(unlocked_by) ~ "bonus"
),
title = topics,
topics = sapply(config$exercises$topics, paste0, collapse = ",")) %>%
select(id, label, group, title, topics, value)
# Get names for core exercises (used to create edges between core exercises)
core_exercises <- nodes %>%
filter(group == "core") %>%
select(id)
# Build edges data frame from core exercises and unlocked_by attributes
from = config$exercises$unlocked_by[!is.na(config$exercises$unlocked_by)] %>%
c(core_exercises$id[1:nrow(core_exercises) - 1])
to = config$exercises$slug[!is.na(config$exercises$unlocked_by)] %>%
c(core_exercises$id[-1])
edges <- data.frame(from = from, to = to)
progress$set(value = 0.8, detail = "Rendering")
# Create network visualization
visNetwork(nodes, edges,
main = paste0("Track config for ", config$language)) %>%
visNodes(shape = input$node_shape) %>%
visEdges(smooth = FALSE, arrows = "to", width = 2) %>%
visOptions(highlightNearest = list(enabled = T, degree = 1)) %>%
visOptions(selectedBy = list(variable = "topics", multiple = T)) %>%
visLegend(position = "right") %>%
visInteraction(navigationButtons = TRUE, hideEdgesOnDrag = TRUE)
})
})