-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
229 lines (213 loc) · 8.76 KB
/
app.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
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
#
# 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:
#
# https://shiny.posit.co/
#
library(bslib)
library(shiny)
library(megamekR)
library(dplyr)
library(stringr)
library(editbl)
library(janitor)
# the app converts all the missing values to empty strings - we need to
# convert them back
convert_missing <- function(tbl) {
tbl <- tbl |> mutate(across(everything(), function(x) {
if(is.character(x)) {
return(na_if(x, ""))
} else {
return(x)
}
}))
}
# Define UI for application that draws a histogram
ui <- page_sidebar(
title = "Planetary Data Editor",
sidebar = sidebar(
fileInput("upload", "Upload a YAML file", accept = c("yml")),
downloadButton("download")
),
tabsetPanel(id = "tabs",
tabPanel("Base Information",
card(
card_header("Overall System Information"),
eDTOutput('system')
),
card(
card_header("Planetary System Events"),
eDTOutput('systemEvents')
),
card(
card_header("Base Planet Information"),
eDTOutput('planets'),
)
))
)
# Define server logic required to draw a histogram
server <- function(input, output) {
planetary_data <- reactive({
req(input$upload)
read_planetary_data(input$upload$datapath)
})
modifiedPlanetarySystem <- reactiveVal()
modifiedSystemEvents <- reactiveVal()
modifiedPlanets <- reactiveVal()
modifiedLandmasses <- reactiveVal()
modifiedSatellites <- reactiveVal()
modifiedPlanetaryEvents <- reactiveVal()
tab_names <- reactiveVal()
descriptions <- reactiveVal()
observe({
req(planetary_data())
modifiedPlanetarySystem(eDT(id = 'system',
data = planetary_data()$system,
canDeleteRow = FALSE,
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("undo", "redo", "save")),
editable = list(target = "cell",
disable = list(columns = 1:4))))
modifiedSystemEvents(eDT(id = 'systemEvents',
data = planetary_data()$system_events,
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("add","undo", "redo", "save"))))
planets <- planetary_data()$planets
if('desc' %in% colnames(planets)) {
desc <- planets$desc
desc <- ifelse(is.na(desc), '', desc)
descriptions(desc)
planets <- planets |>
select(!desc)
} else {
descriptions(NULL)
}
modifiedPlanets(eDT(id = 'planets',
data = planets,
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("add","undo", "redo", "save"))))
landmasses <- list()
for(i in 1:length(planetary_data()$landmasses)) {
if(!is.null(planetary_data()$landmasses[[i]])) {
lm <- eDT(id = paste("landmass", i, sep=""),
data = planetary_data()$landmasses[[i]],
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("add","undo", "redo", "save")))
landmasses[[paste("landmass", i, sep="")]] <- lm
} else {
landmasses[paste("landmass", i, sep="")] <- list(NULL)
}
}
modifiedLandmasses(landmasses)
satellites <- list()
for(i in 1:length(planetary_data()$satellites)) {
if(!is.null(planetary_data()$satellites[[i]])) {
sat <- eDT(id = paste("satellite", i, sep=""),
data = planetary_data()$satellites[[i]],
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("add","undo", "redo", "save")))
satellites[[paste("satellite", i, sep="")]] <- sat
} else {
satellites[paste("satellite", i, sep="")] <- list(NULL)
}
}
modifiedSatellites(satellites)
planetary_events <- list()
for(i in 1:length(planetary_data()$planetary_events)) {
if(!is.null(planetary_data()$planetary_events[[i]])) {
pevents <- eDT(id = paste("planetary_events", i, sep=""),
data = planetary_data()$planetary_events[[i]],
options = list(dom = 'Bt',
keys = TRUE,
ordering = FALSE,
autoFill = list(update =FALSE, focus = "focus"),
buttons = list("add","undo", "redo", "save")))
planetary_events[[paste("planetary_events", i, sep="")]] <- pevents
} else {
planetary_events[paste("planetary_events", i, sep="")] <- list(NULL)
}
}
modifiedPlanetaryEvents(planetary_events)
})
observeEvent(input$upload, {
# remove old tabs
for(tab_name in tab_names()) {
removeTab(inputId = "tabs", target = tab_name)
}
n <- nrow(planetary_data()$planets)
tab_names <- c()
for(i in 1:n) {
insertTab(inputId = "tabs",
tabPanel(planetary_data()$planets$name[i],
card(card_header("Description"), textAreaInput(paste("desc", i, sep=""), NULL, descriptions()[i],
height = "200px", width = "100%", resize = "both")),
card(card_header("Landmasses"), eDTOutput(paste("landmass", i, sep=""))),
card(card_header("Satellites"), eDTOutput(paste("satellite", i, sep=""))),
card(card_header("Planetary Events"), eDTOutput(paste("planetary_events", i, sep="")))))
tab_names <- c(tab_names, planetary_data()$planets$name[i])
}
tab_names(tab_names)
})
output$download <- downloadHandler(
filename = function() {
paste(make_clean_names(planetary_data()$system$id, case = "big_camel"),
"yml", sep=".")
},
content = function(file) {
planetary_system <- planetary_data()
planetary_system$system <- convert_missing(modifiedPlanetarySystem()$result())
planetary_system$system_events <- convert_missing(modifiedSystemEvents()$result())
planetary_system$planets <- convert_missing(modifiedPlanets()$result())
# add back in descriptions
desc <- rep(NA, nrow(planetary_system$planets))
for(i in 1:nrow(planetary_system$planets)) {
x <- input[[paste("desc", i, sep="")]]
if(x != "") {
desc[i] <- x
}
}
planetary_system$planets$desc <- desc
for(i in 1:length(planetary_system$landmasses)) {
if(is.null(modifiedLandmasses()[[i]])) {
planetary_system$landmasses[i] <- list(NULL)
} else {
planetary_system$landmasses[[i]] <- convert_missing(modifiedLandmasses()[[i]]$result())
}
}
for(i in 1:length(planetary_system$satellites)) {
if(is.null(modifiedSatellites()[[i]])) {
planetary_system$satellites[i] <- list(NULL)
} else {
planetary_system$satellites[[i]] <- convert_missing(modifiedSatellites()[[i]]$result())
}
}
for(i in 1:length(planetary_system$planetary_events)) {
if(is.null(modifiedPlanetaryEvents()[[i]])) {
planetary_system$planetary_events[i] <- list(NULL)
} else {
planetary_system$planetary_events[[i]] <- convert_missing(modifiedPlanetaryEvents()[[i]]$result())
}
}
write_planetary_data(planetary_system, file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)