Skip to content

Commit 7713933

Browse files
author
jwildfire
authored
Merge pull request #601 from SafetyGraphics/improveInit
Improve init
2 parents 74cc01a + 7a3af7c commit 7713933

File tree

10 files changed

+119
-130
lines changed

10 files changed

+119
-130
lines changed

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export(loadCharts)
1919
export(loadChartsUI)
2020
export(loadData)
2121
export(loadDataUI)
22-
export(loadDomains)
23-
export(loadDomainsUI)
2422
export(makeChartConfig)
2523
export(makeChartConfigFunctions)
2624
export(makeChartExport)

R/app_startup.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ app_startup<-function(domainData=NULL, meta=NULL, charts=NULL, mapping=NULL, aut
3838
message("To display these charts, set the `order` parameter in the chart object or yaml file to a positive number.")
3939
charts <- charts[purrr::map_lgl(charts, function(chart) chart$order >= 0)]
4040
}
41+
42+
chartOrder <- order(charts %>% map_int(~.x$order) %>% unlist())
43+
charts <- charts[chartOrder]
4144

4245
#Drop charts if data for required domain(s) is not found
4346
chart_drops <- charts %>% purrr::keep(~(!all(.x$domain %in% names(domainData))))

R/makeChartConfigFunctions.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ makeChartConfigFunctions <- function(chart){
3939
chart$functions$main<-chart$functions[[chart$workflow$main]]
4040
}else if(chart$type=="table"){
4141
chart$functions$ui<-DT::dataTableOutput
42-
chart$functions$server<-function(expr ){
42+
chart$functions$server<-function(expr){
4343
DT::renderDataTable(
44+
expr,
4445
rownames = FALSE,
4546
options = list(
4647
pageLength = 20,
@@ -50,12 +51,6 @@ makeChartConfigFunctions <- function(chart){
5051
)
5152
}
5253
chart$functions$main<-chart$functions[[chart$workflow$main]]
53-
#}
54-
# }else if(chart$type=="rtf"){
55-
# chart$functions$ui<-div(
56-
# downloadButton(ns("downloadRTF"), "Download RTF"),
57-
# DT::dataTableOutput(ns("chart-wrap"))
58-
# )
5954
}else if(chart$type=="htmlwidget"){
6055
# Helper functions for html widget render
6156
widgetOutput <- function(outputId, width = "100%", height = "400px") {
@@ -71,8 +66,7 @@ makeChartConfigFunctions <- function(chart){
7166
chart$functions$server<-renderWidget
7267
chart$functions$main<-htmlwidgets::createWidget
7368
chart$workflow$main <- "htmlwidgets::createWidget"
74-
75-
}else if (chart$type=="module"){
69+
}else if(chart$type=="module"){
7670
chart$functions$ui<-chart$functions[[chart$workflow$ui]]
7771
chart$functions$server<-callModule
7872
chart$functions$main <- chart$functions[[chart$workflow$server]]

R/mod_loadCharts.R

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,14 @@
1111

1212
loadChartsUI <- function(id, charts=makeChartConfig()){
1313
ns <- NS(id)
14-
labels <- charts%>%map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
1514
div(
16-
sortable::bucket_list(
17-
header = h4("Chart Loader"),
18-
group_name = ns("chartList"),
19-
orientation = "horizontal",
20-
add_rank_list(
21-
text = "Active Charts",
22-
labels = labels,
23-
input_id = ns("active")
24-
),
25-
add_rank_list(
26-
text = "Inactive Charts",
27-
labels = NULL,
28-
input_id = ns("inactive")
29-
)
30-
)
31-
)
32-
15+
h4(
16+
"Chart Loader",
17+
actionButton(ns("addCharts"), "Select All", class="btn-xs"),
18+
actionButton(ns("removeCharts"), "Remove All", class="btn-xs")
19+
),
20+
uiOutput(ns("chartLists"))
21+
)
3322
}
3423

3524
#' @title loadCharts
@@ -42,8 +31,69 @@ loadChartsUI <- function(id, charts=makeChartConfig()){
4231

4332
#' @export
4433
loadCharts <- function(input, output, session, charts=makeChartConfig()) {
34+
ns<-session$ns
35+
labels<-charts%>%map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
36+
rv <- reactiveValues(
37+
inactive = charts%>%keep(~.x$order < 1)%>%map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable")),
38+
active = charts%>%keep(~.x$order >= 1)%>%map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
39+
)
40+
output$chartLists <- renderUI({
41+
div(
42+
sortable::bucket_list(
43+
header = NULL,
44+
group_name = ns("chartList"),
45+
orientation = "horizontal",
46+
add_rank_list(
47+
text = "Active Charts",
48+
labels = rv$active,
49+
input_id = ns("active")
50+
),
51+
add_rank_list(
52+
text = "Inactive Charts",
53+
labels = rv$inactive,
54+
input_id = ns("inactive")
55+
)
56+
)
57+
)
58+
})
59+
60+
# Sync input and reactiveValues
61+
observeEvent(input$active,{
62+
rv$active <- charts %>%
63+
purrr::keep(~.x$name %in% input$active)%>%
64+
map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
65+
rv$inactive <- charts %>%
66+
purrr::keep(~.x$name %in% input$inactive)%>%
67+
map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
68+
})
69+
70+
observeEvent(input$inactive,{
71+
rv$active <- charts %>%
72+
purrr::keep(~.x$name %in% input$active)%>%
73+
map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
74+
rv$inactive <- charts %>%
75+
purrr::keep(~.x$name %in% input$inactive)%>%
76+
map(~makeChartSummary(.x,showLinks=FALSE,class="chart-sortable"))
77+
78+
})
79+
80+
# Update reactiveValues/Input on add/remove button clicks
81+
observeEvent(input$addCharts,{
82+
rv$active <- labels
83+
rv$inactive <- NULL
84+
})
85+
86+
observeEvent(input$removeCharts,{
87+
rv$active <- NULL
88+
rv$inactive <- labels
89+
})
4590
chartsR<-reactive({
46-
charts %>% purrr::keep(~.x$name %in% input$active)
91+
charts %>%
92+
purrr::keep(~.x$name %in% input$active) %>%
93+
map(function(chart){
94+
chart$order <- match(chart$name, input$active)
95+
return(chart)
96+
})
4797
})
4898
return(chartsR)
4999
}

R/mod_loadData.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ loadDataUI <- function(id, domain=NULL){
1414
actionButton(ns("load_data"), "Load"),
1515
hidden(
1616
actionLink(ns("preview_data"), "Preview")
17-
)
18-
17+
),
18+
id=ns("wrap")
1919
)
2020
}
2121

2222
#' @title loadDataServer
2323
#' @description UI that facilitates the mapping of a column data domain
2424
#'
25-
#' @param domain List of data domains to be loaded
25+
#' @param domain data domain to be loaded
2626
#' @param input Shiny input object
2727
#' @param output Shiny output object
2828
#' @param session Shiny session object
2929
#'
3030
#' @export
31+
3132
loadData <- function(input, output, session, domain) {
3233
ns <- session$ns
34+
3335
fileSummary <- reactiveVal()
3436
fileSummary("<No Data Loaded>")
3537
observeEvent(input$load_data, {
@@ -70,6 +72,6 @@ loadData <- function(input, output, session, domain) {
7072
)
7173
})
7274

73-
return(imported)
75+
return(imported$data)
7476
}
7577

R/mod_loadDomains.R

Lines changed: 0 additions & 46 deletions
This file was deleted.

R/safetyGraphicsInit.R

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#'
99
#' @export
1010

11-
safetyGraphicsInit <- function(charts=makeChartConfig(),delayTime=1000){
11+
safetyGraphicsInit <- function(charts=makeChartConfig(), delayTime=1000){
1212
charts_init<-charts
13+
all_domains <- charts_init %>% map(~.x$domain) %>% unlist() %>% unique()
14+
1315
app_css <- NULL
1416
for(lib in .libPaths()){
1517
if(is.null(app_css)){
@@ -23,19 +25,24 @@ safetyGraphicsInit <- function(charts=makeChartConfig(),delayTime=1000){
2325
tags$head(tags$style(app_css)),
2426
div(
2527
id="init",
26-
titlePanel("safetyGraphics Initialization app"),
28+
titlePanel("safetyGraphics Initializer"),
2729
sidebarLayout(
2830
position="right",
2931
sidebarPanel(
3032
h4("Data Loader"),
31-
loadDomainsUI("load-data"),
33+
all_domains %>% map(~loadDataUI(.x, domain=.x)),
3234
textOutput("dataSummary"),
3335
hr(),
3436
shinyjs::disabled(
3537
actionButton("runApp","Run App",class = "btn-block")
3638
)
3739
),
3840
mainPanel(
41+
p(
42+
icon("info-circle"),
43+
"First, select charts by dragging items between the lists below. Next, load the required data domains using the controls on the right. Finally, click Run App to start the safetyGraphics Shiny App. Reload the webpage to select new charts/data.",
44+
class="info"
45+
),
3946
loadChartsUI("load-charts", charts=charts_init),
4047
)
4148
),
@@ -49,16 +56,33 @@ safetyGraphicsInit <- function(charts=makeChartConfig(),delayTime=1000){
4956
)
5057

5158
server <- function(input,output,session){
52-
charts<-callModule(loadCharts, "load-charts",charts=charts_init)
53-
domains <- reactive({unique(charts() %>% map(~.x$domain) %>% unlist())})
54-
domainDataR <- callModule(loadDomains, "load-data", domains) #this is a reactive list with reactives (?!)
55-
domainData <- reactive({domainDataR() %>% map(~.x())})
59+
#initialize the chart selection moduls
60+
charts<-callModule(loadCharts, "load-charts",charts=charts_init)
61+
domainDataR<-all_domains %>% map(~callModule(loadData,.x,domain=.x))
62+
names(domainDataR) <- all_domains
63+
domainData<- reactive({domainDataR %>% map(~.x())})
64+
65+
66+
current_domains <- reactive({
67+
charts() %>% map(~.x$domain) %>% unlist() %>% unique()
68+
})
69+
70+
observe({
71+
for(domain in all_domains){
72+
if(domain %in% current_domains()){
73+
shinyjs::show(id=paste0(domain,"-wrap"))
74+
}else{
75+
shinyjs::hide(id=paste0(domain,"-wrap"))
76+
}
77+
}
78+
})
5679

5780
initStatus <- reactive({
81+
currentData <- domainData()
5882
chartCount<-length(charts())
59-
domainCount<-length(domainData())
60-
loadCount<-sum(domainData() %>% map_lgl(~!is.null(.x)))
61-
notAllLoaded <- any(domainData() %>% map_lgl(~is.null(.x)))
83+
domainCount<-length(current_domains())
84+
loadCount<-sum(currentData %>% map_lgl(~!is.null(.x)))
85+
notAllLoaded <- sum(currentData %>% map_lgl(~!is.null(.x))) < domainCount
6286
ready<-FALSE
6387
if(domainCount==0){
6488
status<-paste("No charts selected. Select one or more charts and then load domain data to initilize app.")
@@ -85,20 +109,19 @@ safetyGraphicsInit <- function(charts=makeChartConfig(),delayTime=1000){
85109
}
86110
})
87111

88-
89112
observeEvent(input$runApp,{
90-
print("running the app server now :p")
91113
shinyjs::hide(id="init")
92114
shinyjs::show(id="sg-app")
93115
config<- app_startup(
94-
domainData = domainData(),
116+
domainData = domainData() %>% keep(~!is.null(.x)),
95117
meta = safetyGraphics::meta,
96118
charts= charts(),
97119
#mapping=NULL,
98120
filterDomain="dm",
121+
autoMapping=TRUE,
99122
#chartSettingsPaths = NULL
100123
)
101-
124+
102125
output$sg <- renderUI({
103126
safetyGraphicsUI(
104127
"sg",
@@ -126,6 +149,5 @@ safetyGraphicsInit <- function(charts=makeChartConfig(),delayTime=1000){
126149
}
127150

128151
app <- shinyApp(ui = ui, server = server)
129-
130152
runApp(app, launch.browser = TRUE)
131153
}

man/loadData.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/loadDomains.Rd

Lines changed: 0 additions & 20 deletions
This file was deleted.

man/loadDomainsUI.Rd

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)