-
Notifications
You must be signed in to change notification settings - Fork 1
/
app-v2.R
164 lines (136 loc) · 6.23 KB
/
app-v2.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
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
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(DT)
library(shinycssloaders)
library(igraph)
library(dplyr)
library(arcdiagram)
# Input data preparation
# Read 14 BiCKL organisation list for
org_list <- read.csv(file = 'data/org_list.csv')
org_key_value <- setNames(as.list(org_list$org_id),org_list$org)
org_key_value <- append(c('Select an Organization' = 0),org_key_value)
get_dataframe <- function(org_id = 0,level = 1) {
if (org_id != 0) {
# Create the file iname
file_name <- paste('data/org_',org_id,'.',level,'.csv', sep = '')
if (file.exists(file_name)) {
# Read the preloaded dataframe with the desire level of depth
return(read.csv(file_name))
} else {
return(NULL)
}
} else {
return(NULL)
}
}
prep_dataheading <- function(org_id = 0, org_data = NULL) {
if (!is.null(org_data) && (org_id != 0)) {
org_name <- org_data$organisation_name.x[org_data$organisation_id.x == org_id][1]
org_relations <- nrow(org_data)
return (paste("<h3>",org_name,"</h3><p><strong>(",org_relations," relationships)</strong></p>", sep = ""))
} else {
return ('<p>No organization selected.</p>')
}
}
prep_dataplot <- function(org_data = NULL) {
if (!is.null(org_data)) {
dset<-select(dataTable,organisation_abbr.x,organisation_abbr.y)
data_matrix<-as.matrix(dset)
return(data_matrix)
} else {
return(rbind(c('No','Organization'),c('Organization','Selected')))
}
}
prep_datatable <- function(org_data = NULL, depth = 1) {
if (! is.null(org_data)) {
# Prepare dataTable depending of the depth level
if (depth == 1) {
org_data$organisation_id.x <- NULL
org_data$organisation_name.x <- NULL
org_data$organisation_abbr.x <- NULL
# Sorting by related organization name
org_data <- org_data[order(org_data$organisation_name.y),]
# Create valid organization URL in Y
org_data$organisation_name.y <- paste('<a href="https://fairsharing.org/organisations/',org_data$organisation_id.y,'" target="_blank">',org_data$organisation_name.y,'</a>', sep='')
# Renaming column names
colnames(org_data) <- c('Related To', 'ID', 'Abbr')
} else {
# Sorting by related organization name
org_data <- org_data[order(org_data$organisation_name.x,org_data$organisation_name.y),]
# Create valid organization URL in X and Y
org_data$organisation_name.x <- paste('<a href="https://fairsharing.org/organisations/',org_data$organisation_id.x,'" target="_blank">',org_data$organisation_name.x,'</a>', sep='')
org_data$organisation_name.y <- paste('<a href="https://fairsharing.org/organisations/',org_data$organisation_id.y,'" target="_blank">',org_data$organisation_name.y,'</a>', sep='')
# Renaming column names
colnames(org_data) <- c('From', 'From ID', 'From Abbr', 'To', 'To ID', 'To Abbr')
}
return(org_data)
} else {
# Sets a default blank dataframe
default <- data.frame(nodata = c('No Organization data available'))
colnames(default) <- c('No Data')
return(default)
}
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Custom CSS
includeCSS('www/style.css'),
# Application title
titlePanel("GBIF Organization Relationships"),
# Sidebar with filter options
sidebarLayout(
sidebarPanel(
selectInput('filter_org_list','Organization',org_key_value),
selectInput('filter_depth','Depth Level',c('First level' = 1,'Second level' = 2))
),
# Show the selected organization name and ID
mainPanel(
htmlOutput("h3_org"),
tabsetPanel(id = "tabs", type = "tabs",
tabPanel("Relationships",
withSpinner(DT::dataTableOutput('relationships_table'),
type = 4,
color = "#4787fb",
size = 1
)
),
tabPanel("Graph",
withSpinner(plotOutput('org_plot'),
type = 4,
color = "#4787fb",
size = 1
)
)
)
)
),
includeHTML('www/footer.html')
)
server <- function(input, output) {
# Get the corresponding data for the selected organization. If no selection then NULL
dataSet <- reactive({get_dataframe(input$filter_org_list,input$filter_depth)})
output$h3_org <- renderText(prep_dataheading(input$filter_org_list,dataSet()))
dataPlot <- reactive({prep_dataplot(dataSet())})
output$org_plot<-renderPlot(arcplot(dataPlot(), col.arcs = palette("ggplot2")))
dataTable <- reactive({prep_datatable(dataSet(),input$filter_depth)})
output$relationships_table <- DT::renderDataTable({datatable(dataTable(),
escape = FALSE,
selection = "single",
options = list(
columnDefs = list(
list(visible = FALSE,
targets=c(0))
)
)
)})
}
# Run the application
shinyApp(ui = ui, server = server)