Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fourstep #74

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ Imports:
sf,
purrr,
checkmate,
DTedit,
htmlwidgets
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
RoxygenNote: 7.2.1
URL: https://github.com/asiripanich/emdash
BugReports: https://github.com/asiripanich/emdash/issues
Suggests:
knitr,
rmarkdown,
testthat
Remotes: DavidPatShuiFong/DTedit@7ce8de1b5fcd7195beee73ed4acce8ac33140ab7
Config/testthat/edition: 3
VignetteBuilder: knitr
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export(query_cleaned_section)
export(query_cleaned_trips)
export(query_confirmed_trips_by_timestamp)
export(query_diary_summ)
export(query_manual_survey_response)
export(query_max_trip_timestamp)
export(query_min_trip_timestamp)
export(query_most_recent_n_trip_docs)
export(query_raw_trips)
export(query_server_calls)
export(query_stage_profiles)
export(query_stage_uuids)
export(query_time_use_response)
export(query_trip_confirm_response)
export(query_usercache_get_summ)
export(query_usercache_put_summ)
export(run_app)
Expand Down
126 changes: 5 additions & 121 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@ app_server <- function(input, output, session) {
)

# DATA TABS
#
# use these to generate lists of columns to inform which columns to remove
# data_r$participants %>% colnames() %>% dput()
# data_geogr$trips %>% colnames() %>% dput()
# POSSIBLE LINE: allNames[!(allNames %in% config$column_names)]
# cols_to_remove_from_participts_table <- c("first_trip_datetime",
# "last_trip_datetime")

# Only append supplementary table tabs on startup.
startup_load_supplementary <- reactiveVal(TRUE)
tableList <- getOption("emdash.supplementary_tables")

observeEvent(data_r$click, {
callModule(mod_DT_server, "DT_ui_participants",
data = data_r$participants %>%
Expand All @@ -155,116 +143,12 @@ app_server <- function(input, output, session) {
skip_absent = TRUE
)
)
})

# For each supplementary table, append a new tabPanel and run the server function
# that specifies table behavior
for (t in tableList) {
table_type <- names(t)
table_title <- t[[table_type]]$tab_name

suppl_table <- data_r[[table_type]]

# Set the options used by DT::renderDataTable within dtedit and mod_DT


# If the table has a timestamp, make a copy of the timestamp column called fmt_time
if ("ts" %in% colnames(suppl_table)) {
suppl_table[["fmt_time"]] <- suppl_table[["ts"]]
fmt_time_index <- which(names(suppl_table) == "fmt_time")

# Add columnDefs to datatable options to convert fmt_time to a Date
datatable_options <- list()
datatable_options[["columnDefs"]] <- list(list(
# target column indices start from 0.
# However, DT adds a row number column to the display as the 0th column
targets = fmt_time_index,
render = htmlwidgets::JS(
"function(data, type, row) {",
"return new Date(data*1000);",
"}"
)
))
}

suppl_table <- data_r[[table_type]]

# If the table has a timestamp, make a copy of the timestamp column called fmt_time
if ("ts" %in% colnames(suppl_table)) {
suppl_table[["fmt_time"]] <- suppl_table[["ts"]]
fmt_time_index <- which(names(suppl_table) == "fmt_time")

# Add columnDefs to datatable options to convert fmt_time to a Date
datatable_options <- list()
datatable_options[["columnDefs"]] <- list(list(
# target column indices start from 0.
# However, DT adds a row number column to the display as the 0th column
targets = fmt_time_index,
render = htmlwidgets::JS(
"function(data, type, row) {",
"return new Date(data*1000);",
"}"
)
))
}

# If the table is Bike Check In and there is an 'editable' field, use dtedit
if (table_type == "Checkinout" & "editable" %in% names(t$Checkinout)) {
if (isTRUE(startup_load_supplementary())) {
editable_table_tab <- tabPanel(
status = "primary",
title = table_title,
value = table_type,
mod_DTedit_ui(id = paste0("DTedit_", table_type))
)

appendTab(
inputId = "tabs",
tab = editable_table_tab,
select = FALSE,
menuName = NULL,
session = getDefaultReactiveDomain()
)
}

callModule(
module = mod_DTedit_server,
id = paste0("DTedit_", table_type),
table_data = suppl_table,
table_type = table_type,
suppl_table_sublist = t,
DT_options = datatable_options,
cons
)
}
# For other supplementary tables, use mod_DT
else {
if (isTRUE(startup_load_supplementary())) {
regular_tab <- tabPanel(
status = "primary",
title = table_title,
value = table_type,
mod_DT_ui(id = paste0("DT_ui_", table_type))
)

appendTab(
inputId = "tabs",
tab = regular_tab,
select = FALSE,
menuName = NULL,
session = getDefaultReactiveDomain()
)
}

# Run mod_DT_server using data for the current table
callModule(
module = mod_DT_server,
id = paste0("DT_ui_", table_type),
data = suppl_table,
DT_options = datatable_options
)
}
}
startup_load_supplementary(FALSE)
observeEvent(data_r$click, {
callModule(mod_DT_server, "DT_ui_timeuse",
data = data_r$timeuse
)
})

observeEvent(data_geogr$click, {
Expand Down
9 changes: 8 additions & 1 deletion R/app_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ app_ui <- function(request) {
# List the first level UI elements here
dashboardPage(
skin = "green",
dashboardHeader(title = paste0("emdash v", packageVersion("emdash"))),
# dashboardHeader(title = paste0("Fourstep v", packageVersion("emdash"))),
dashboardHeader(title = paste0("Fourstep dashboard")),

# Sidebar -----------------------------------------------------------------
dashboardSidebar(
Expand Down Expand Up @@ -115,6 +116,12 @@ app_ui <- function(request) {
title = "Trips",
value = "trips",
mod_DT_ui("DT_ui_trips")
),
tabPanel(
status = "primary",
title = "Time-use",
value = "timeuse",
mod_DT_ui("DT_ui_timeuse")
)
)
)
Expand Down
4 changes: 4 additions & 0 deletions R/emdash_config_get.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
emdash_config_get <- function(name, config_file = Sys.getenv("EMDASH_CONFIG", here::here("_emdash.yaml"))) {
message("config_file: ", config_file)
config::get(value = name, file = config_file)
}
85 changes: 38 additions & 47 deletions R/mod_load_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,53 @@ mod_load_data_server <- function(input, output, session, cons) {

observeEvent(input$reload_data,
{
if (checkmate::test_file_exists(emdash_config_get("user_email_csv"), extension = "csv")) {
user_email_csv <- read.csv(emdash_config_get("user_email_csv"))
if (checkmate::test_character(user_email_csv$user_email, min.len = 1, all.missing = FALSE)) {
stage_uuids <- query_stage_uuids(cons, user_emails = na.omit(user_email_csv$user_email))
} else {
stage_uuids <- query_stage_uuids(cons)
}
}
message("About to load participants")
data_r$participants <-
tidy_participants(query_stage_profiles(cons), query_stage_uuids(cons)) %>%
tidy_participants(query_stage_profiles(cons), stage_uuids) %>%
summarise_trips_without_trips(., cons) %>%
summarise_timeuse(., cons) %>%
summarise_server_calls(., cons)

if (getOption("emdash.remove_from_participants_file") != "" &&
checkmate::test_file_exists(getOption("emdash.remove_from_participants_file"), extension = "txt") &&
readLines(getOption("emdash.remove_from_participants_file")) != 0) {
participants_to_remove <- readLines(getOption("emdash.remove_from_participants_file"))
col_to_remove_participants <- getOption("emdash.remove_from_participants_col")

message(
sprintf(
"Removing %s participants listed in %s",
length(participants_to_remove),
getOption("emdash.remove_from_participants_file")
)
)

data_r$participants <- data_r$participants %>%
subset(!base::get(col_to_remove_participants) %in% participants_to_remove)
}

message("Finished loading participants")
message(sprintf("Participants size is: %s kb", format(object.size(data_r$participants), units = "kB", standard = "SI")))

table_list <- getOption("emdash.supplementary_tables")

# For each supplementary table, query the corresponding data
for (t in table_list) {
table_type <- names(t)
table_title <- t[[table_type]]$tab_name

message(paste("About to load", table_title))
if (table_type == "Checkinout") {
# Get bike check in and include the object ID so we can use it instead of user_id for CUD
data_r[[table_type]] <-
cons$Checkinout$find(
query = "{}",
fields = "{}" # get all fields, including objectId
) %>%
as.data.table()
} else {
data_r[[table_type]] <- cons[[table_type]]$find("{}") %>%
as.data.table()
}

if ("user_id" %in% colnames(data_r[[table_type]])) {
data_r[[table_type]] %>%
message("Loading timeuse data")
timeuse_records <- cons$Stage_timeseries$find(
query = sprintf(
'{
"metadata.key" : "manual/survey_response",
"data.name" : "TimeUseSurvey",
%s
}',
mongo_create_find_in_uuid_query("user_id", stage_uuids$uuid_decoded)
),
fields = '{
"user_id": true,
"metadata.write_fmt_time": true,
"data.label": true,
"data.response.sub_label": true,
"data.response.activity_startdate": true,
"data.response.activity_starttime": true,
"data.response.activity_endtime": true
}'
)
if (nrow(timeuse_records) != 0) {
data_r$timeuse <- timeuse_records %>%
as.data.table() %>%
normalise_uuid() %>%
data.table::setcolorder(c("user_id"))
.[, .SD, .SDcols = -patterns("^X_id$")] %>%
merge(data_r$participants[, .(user_id, user_email)], by = "user_id") %>%
data.table::setcolorder(c("user_id", "user_email"))
} else {
data_r$timeuse <- data.frame()
}
message(paste("Finished loading", table_title))
}

data_r$click <- runif(1)
},
ignoreNULL = FALSE
Expand Down
4 changes: 2 additions & 2 deletions R/run_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#' @importFrom shiny shinyApp
#' @importFrom golem with_golem_options

run_app <- function(mongo_url, config_file = getOption("emdash.config_file"), ...) {
if (!missing(mongo_url)) {
run_app <- function(mongo_url = NULL, config_file = getOption("emdash.config_file"), ...) {
if (!is.null(mongo_url)) {
checkmate::assert_string(mongo_url)
options("emdash.mongo_url" = mongo_url)
}
Expand Down
Loading