Skip to content

Commit

Permalink
splitt choices into processed and unprocessed data, only one button '…
Browse files Browse the repository at this point in the history
…process data'
  • Loading branch information
arunge committed Jan 7, 2025
1 parent c5209ac commit 915d79b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 71 deletions.
25 changes: 2 additions & 23 deletions R/02-importData-configureData.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ configureDataUI <- function(id,
)
),
helpText(width = "100%",
"Use the loaded file for data processing in the tabs: 'Query with SQL' or 'Prepare' / 'Merge'.")
"Process loaded data before import in the tabs: 'Query with SQL', 'Prepare' or 'Merge'.")
),
column(6,
align = "right",
style = "margin-top: 1.5em",
actionButton(ns("keepDataForQuery"), "Create Query from file"),
actionButton(ns("keepData"), "Prepare / Merge file(s)")
actionButton(ns("keepDataForQuery"), "Process data")
)
),
downloadDataLinkUI(ns = ns,
Expand Down Expand Up @@ -149,10 +148,8 @@ configureDataServer <- function(id,
if (length(values$dataImport) == 0 ||
isNotValid(values$errors, values$warnings, ignoreWarnings) ||
dataSource$type == "dataLink") {
shinyjs::disable(ns("keepData"), asis = TRUE)
shinyjs::disable(ns("keepDataForQuery"), asis = TRUE)
} else {
shinyjs::enable(ns("keepData"), asis = TRUE)
shinyjs::enable(ns("keepDataForQuery"), asis = TRUE)
values$fileImportSuccess <-
"Data import successful"
Expand All @@ -166,24 +163,6 @@ configureDataServer <- function(id,
## button keep data ----
newDataForMergeList <- reactiveVal(NULL)

observe({
logDebug("Updating input$keepData")

newData <- list(data = values$dataImport %>%
formatColumnNames(silent = TRUE),
input = list(
source = dataSource$input,
file = getFileInputs(input)
))
attr(newData, "unprocessed") <- FALSE # disables download of data links

newDataForMergeList(newData)

# disable "keepData" to prevent loading data twice
shinyjs::disable(ns("keepData"), asis = TRUE)
}) %>%
bindEvent(input$keepData)

observe({
logDebug("Updating input$keepDataForQuery")

Expand Down
17 changes: 13 additions & 4 deletions R/02-importData-linkToData.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,23 @@ observeDownloadDataLink <- function(id, input, output, session, mergeList, downl
)
}

#' Filter Unprocessed
#'
#' @inheritParams configureDataServer
# Filter Unprocessed
#
# @inheritParams configureDataServer
filterUnprocessed <- function(mergeList) {
if (length(mergeList) == 0) return(mergeList)
mergeList[sapply(mergeList, function(x) {
!is.null(attr(x, "unprocessed")) && isTRUE(attr(x, "unprocessed"))
})]
}

# Filter Processed
#
# @inheritParams configureDataServer
filterProcessed <- function(mergeList) {
if (length(mergeList) == 0) return(mergeList)
mergeList[sapply(mergeList, function(x) {
!is.null(attr(x, "unprocessed")) && attr(x, "unprocessed")
!is.null(attr(x, "unprocessed")) && isFALSE(attr(x, "unprocessed"))
})]
}

Expand Down
74 changes: 62 additions & 12 deletions R/02-importData-mergeData.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mergeDataUI <- function(id) {
selectInput(
ns("tableX"),
"Select table x",
choices = c("Please load data under 'Select' and press 'Prepare / Merge file(s)' ..." = ""),
choices = emptyMergeListChoices(),
width = "100%"
)
),
Expand All @@ -28,7 +28,7 @@ mergeDataUI <- function(id) {
selectInput(
ns("tableY"),
"Select table y",
choices = c("Please load data under 'Select' and press 'Prepare / Merge file(s)' ..." = ""),
choices = emptyMergeListChoices(),
width = "100%"
)
),
Expand Down Expand Up @@ -101,15 +101,15 @@ mergeDataServer <- function(id, mergeList) {

tableIds(extractTableIds(mergeList()))

tableChoices <- extractMergeChoices(mergeList())
tableChoices <- extractMergeListChoices(mergeList(), addIDs = TRUE)
updateSelectInput(session,
"tableX",
choices = tableChoices,
selected = tableChoices[1])
selected = unlist(tableChoices)[1])
updateSelectInput(session,
"tableY",
choices = tableChoices,
selected = tableChoices[2])
selected = unlist(tableChoices)[2])
})

output$nRowsTableX <- renderText({
Expand Down Expand Up @@ -252,11 +252,14 @@ mergeDataServer <- function(id, mergeList) {
value = 0.75,
message = 'merging data ...')

newData <- list(data = joinedResult$data,
history = list())
attr(newData, "unprocessed") <- FALSE # disables download of data links

# UPDATE MERGELIST ----
newMergeList <- updateMergeList(mergeList = mergeList(),
fileName = input$fileNameJoined,
newData = list(data = joinedResult$data,
history = list()))
newData = newData)
mergeList(newMergeList$mergeList)

# keep filename
Expand Down Expand Up @@ -287,14 +290,61 @@ extractTableData <- function(mergeList, tableName) {
mergeList[[tableName]]$data
}

extractMergeChoices <- function(tableList) {
tableChoices <- names(tableList)
names(tableChoices) <-
paste0(extractTableIds(tableList), " -- ", tableChoices)
extractMergeListChoices <- function(mergeList, addIDs = FALSE) {
getNames <- function(dataList) {
if (length(dataList) == 0)
return(NULL)

res <- names(dataList)
names(res) <- res

res
}

# split into processed and unprocessed data
unprocessedData <- mergeList %>%
filterUnprocessed()
processedData <- mergeList %>%
filterProcessed()
choicesUnprocessed <- getNames(unprocessedData)
choicesProcessed <- getNames(processedData)

# add ids
if (addIDs) {
pasteIDs <- function(fileList, tableIds) {
if (length(fileList) == 0 ||
all(sapply(fileList, function(x)
x == ""))) {
return(fileList) # return if fileList is empty
}

newNames <- paste0(tableIds[fileList], " -- ", fileList)
names(fileList) <- newNames

fileList
}

tableIds <- extractTableIds(mergeList)

choicesUnprocessed <- choicesUnprocessed %>% pasteIDs(tableIds)
choicesProcessed <- choicesProcessed %>% pasteIDs(tableIds)
}

tableChoices
choices <- list(`unprocessed files` = choicesUnprocessed,
`processed files` = choicesProcessed)

# remove empty entries
choices <- choices[!sapply(choices, is.null)]

if (length(choices) == 0)
return(emptyMergeListChoices())

choices
}

emptyMergeListChoices <- function() {
c("Please load new data under 'Select' and press 'Process data' ..." = "")
}

#' Extract Table IDs
#'
Expand Down
14 changes: 8 additions & 6 deletions R/02-importData-prepareData.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ prepareDataUI <- function(id) {
selectInput(
ns("dataToPrep"),
"Select a File",
choices = c("Please load data under 'Select' and press 'Prepare / Merge file(s)' ..." = ""),
choices = emptyMergeListChoices(),
width = "75%"
),
renameColumnsUI(ns("renameCols")),
Expand Down Expand Up @@ -44,14 +44,13 @@ prepareDataServer <- function(id, mergeList) {
history = list())

observeEvent(mergeList(), ignoreInit = TRUE, {
logDebug("Entering input select from mergeList")
req(length(mergeList()) > 0)
logDebug("Updating input select from mergeList")
fileList <- names(mergeList())
choices <- extractMergeListChoices(mergeList())
updateSelectInput(session,
"dataToPrep",
choices = fileList,
selected = fileList[length(fileList)])
choices = choices,
selected = unlist(choices)[length(unlist(choices))])
})

observe({
Expand All @@ -76,6 +75,7 @@ prepareDataServer <- function(id, mergeList) {
observeEvent(renamedData$data, {
logDebug("Updating renamedData")
req(renamedData$data)
attr(renamedData, "unprocessed") <- FALSE # disables download of data links
newMergeList <- updateMergeList(mergeList = mergeList(),
fileName = input$dataToPrep,
newData = renamedData)
Expand All @@ -88,7 +88,7 @@ prepareDataServer <- function(id, mergeList) {
observeEvent(reducedData$data, {
logDebug("Updating reducedData")
req(reducedData$data)

attr(reducedData, "unprocessed") <- FALSE # disables download of data links
newMergeList <- updateMergeList(mergeList = mergeList(),
fileName = input$dataToPrep,
newData = reducedData)
Expand All @@ -101,6 +101,7 @@ prepareDataServer <- function(id, mergeList) {
observeEvent(joinedData$data, {
logDebug("Updating joinedData")
req(joinedData$data)
attr(joinedData, "unprocessed") <- FALSE # disables download of data links
newMergeList <- updateMergeList(mergeList = mergeList(),
fileName = input$dataToPrep,
newData = joinedData)
Expand All @@ -113,6 +114,7 @@ prepareDataServer <- function(id, mergeList) {
observeEvent(splittedData$data, {
logDebug("Updating splittedData")
req(splittedData$data)
attr(splittedData, "unprocessed") <- FALSE # disables download of data links
newMergeList <- updateMergeList(mergeList = mergeList(),
fileName = input$dataToPrep,
newData = splittedData)
Expand Down
27 changes: 16 additions & 11 deletions R/02-importData-queryData.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ queryDataServer <- function(id, mergeList, isActiveTab) {
}) %>%
bindEvent(mergeList()) # cannot set ignoreInit = TRUE because of tests

observe({
tableIds <- reactive({
req(length(unprocessedData()) > 0)
logDebug("QueryData: update inMemoryDB and input$sqlCommand")

tmpDB <- inMemoryDB()
# reset db (remove tables if the become "processed data")
# reset db (remove tables if they become "processed data")
for (i in dbListTables(tmpDB)) {
dbRemoveTable(tmpDB, i)
}
Expand All @@ -105,7 +106,13 @@ queryDataServer <- function(id, mergeList, isActiveTab) {
dbWriteTable(tmpDB, paste0("t", i), unprocessedData()[[i]]$data, overwrite = TRUE)
}
inMemoryDB(tmpDB)
tableIds(dbListTables(tmpDB))

dbListTables(tmpDB)
})

observe({
req(length(unprocessedData()) > 0)
logDebug("QueryData: update inMemoryDB and input$sqlCommand")

inMemCols <-
lapply(unprocessedData(), function(table) {
Expand Down Expand Up @@ -146,10 +153,9 @@ queryDataServer <- function(id, mergeList, isActiveTab) {
bindEvent(unprocessedData())

output$inMemoryTables <- renderDataTable({
validate(need(
!is.null(tableIds()),
"Tables: Please load data under 'Select' and press 'Create Query from file' ..."
))
validate(need(length(unprocessedData()) > 0, paste(
"Tables:", names(emptyMergeListChoices())
)))

req(tableIds())
DT::datatable(
Expand All @@ -171,10 +177,9 @@ queryDataServer <- function(id, mergeList, isActiveTab) {
})

output$inMemoryColumns <- renderDataTable({
validate(need(
!is.null(tableIds()),
"Columns: Please load data under 'Select' and press 'Create Query from file' ..."
))
validate(need(length(unprocessedData()) > 0, paste(
"Columns:", names(emptyMergeListChoices())
)))

req(tableIds())
inMemColsPasted <-
Expand Down
15 changes: 0 additions & 15 deletions man/filterUnprocessed.Rd

This file was deleted.

0 comments on commit 915d79b

Please sign in to comment.