Skip to content

Commit

Permalink
added the split by group to correlation tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Oct 31, 2024
1 parent fa15755 commit 9e24a9d
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 39 deletions.
97 changes: 62 additions & 35 deletions bs/R/OperationsModule.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TODO: draw boxes around intermediate results & append to df in order to seperate them
# TODO: rename subset in rows and add cols function. cols(df, colName); cols(intermeidate_var_df, colName)
# TODO: the names of hte columns the new ones have to be modified by makenames

# TODO: store original dataset. Add option to reset dataset

OperatorEditorSidebar <- function(id) {
ui <- fluidPage(
Expand Down Expand Up @@ -47,7 +47,7 @@ OperatorEditorSidebar <- function(id) {
actionButton(NS(id, "div"), "/", class = "add-button"),
actionButton(NS(id, "bracket_open"), "(", class = "add-button"),
actionButton(NS(id, "bracket_close"), ")", class = "add-button"),
actionButton(NS(id, "bracket_close"), ",", class = "add-button"),
actionButton(NS(id, "comma"), ",", class = "add-button"),
class = "boxed-output"
),
div(
Expand Down Expand Up @@ -93,8 +93,11 @@ OperatorEditorSidebar <- function(id) {
actionButton(NS(id, "strsplit"), "strsplit", class = "add-button"),
actionButton(NS(id, "tolower"), "tolower", class = "add-button"),
actionButton(NS(id, "toupper"), "toupper", class = "add-button"),
actionButton(NS(id, "subset"), "subset", class = "add-button",
title = 'Filter by row. For example subset(df, ColName == "Control") or subset(df, colName == 10)'),
actionButton(NS(id, "get_rows"), "get_rows", class = "add-button",
title = 'Filter by row. For example get_rows(df, ColName == "Control") or get_rows(df, colName == 10)'),
actionButton(NS(id, "get_cols"), "get_cols", class = "add-button",
title = 'Extract column from a data frame (a table).
For example get_cols(df, ColName) or get_cols(df, ColName1, ColName2)'),
class = "boxed-output"
),
div(
Expand Down Expand Up @@ -179,25 +182,34 @@ OperatorEditorUI <- function(id) {
textAreaInput(NS(id, "editable_code"), "Operation:", value = "", rows = 12),
class = "boxed-output"
),
fluidRow(
column(
7,
actionButton(NS(id, "run_op_intermediate"), "Run operation and store intermediate results"),
br(),
div(
class = "boxed-output",
fluidRow(
column(
7,
actionButton(NS(id, "run_op_intermediate"), "Run operation and store intermediate results"),

),
column(
4,
textInput(NS(id, "iv"), "Intermediate variable name:", value = "")
),
column(
4,
textInput(NS(id, "iv"), "Intermediate variable name:", value = "")
)
)
),
fluidRow(
column(
7,
actionButton(NS(id, "run_op"), "Run operation and append to dataset")
),
column(
4,
textInput(NS(id, "nc"), "New column name:", value = "")
br(),
div(
class = "boxed-output",
fluidRow(

column(
7,
actionButton(NS(id, "run_op"), "Run operation and append to dataset")
),
column(
4,
textInput(NS(id, "nc"), "New column name:", value = "")
)
)
),
uiOutput(NS(id, "head")),
Expand Down Expand Up @@ -225,9 +237,10 @@ OperationEditorServer <- function(id, data) {
div(
class = "var-box-output",
h4("df",
title = # TODO: add that only an excerpt is shown of the dataset
title =
"This is the dataset. Using the text df you can access the entire dataset.
If you only want to work with one of the column you can use the respective column title",
If you only want to work with one of the column you can use the respective column title.
As a side note only the first 6 rows of the data table are shown.",
class = "var-output"),
renderTable(head(r_vals$df))
)
Expand All @@ -237,28 +250,33 @@ OperationEditorServer <- function(id, data) {
# Observe intermeidate results
output$intermediate_results <- renderUI({
iv_list <- r_vals$intermediate_vars
if (length(iv_list) == 1) return()
iv_list <- iv_list[names(iv_list) != "df"]
iv_ui <- lapply(names(iv_list), function(name) {
div(
class = "var-box-output",
h4(name, title = paste("This is the variable", name, ". You can use it by entering:", name, " within the Operation text field."), class = "var-output"),
h4(name, title = paste("This is the variable", name,
". You can use it by entering:", name, " within the Operation text field."), class = "var-output"),
verbatimTextOutput(NS(id, paste0("iv_", name))),
actionButton(NS(id, paste0("remove_iv_", name)), "Remove", class = "btn-danger")
)
})
do.call(tagList, iv_ui)
})

# Show intermediate variables
observe({
iv_list <- r_vals$intermediate_vars
for (name in names(iv_list)) {
output[[paste0("iv_", name)]] <- renderPrint({
iv_list[[name]]
})
}
lapply(names(iv_list), function(name) {
observeEvent(r_vals$intermediate_vars[[name]], {
output[[paste0("iv_", name)]] <- renderPrint({
r_vals$intermediate_vars[[name]]
})
}, ignoreInit = TRUE)
})
})

# Observe and render each intermediate result
# Observe remove of intermediate variables
observe({
iv_list <- r_vals$intermediate_vars
for (name in names(iv_list)) {
Expand Down Expand Up @@ -370,7 +388,7 @@ OperationEditorServer <- function(id, data) {
output[["colnames_list"]] <- renderUI({
req(!is.null(r_vals$df))
req(is.data.frame(r_vals$df))
colnames <- names(r_vals$df)
colnames <- c("df", names(r_vals$df)) # TODO: what is the case if one of the column is named df?
button_list <- lapply(colnames[1:length(colnames)], function(i) {
actionButton(
inputId = paste0("OP-colnames_", i, "_", r_vals$counter_id),
Expand All @@ -384,7 +402,7 @@ OperationEditorServer <- function(id, data) {
# React to colnames buttons
observe({
req(r_vals$df)
colnames <- names(r_vals$df)
colnames <- c("df", names(r_vals$df))
lapply(colnames, function(col) {
observeEvent(input[[paste0("colnames_", col, "_", r_vals$counter_id)]], {
current_text <- input[["editable_code"]]
Expand Down Expand Up @@ -423,12 +441,16 @@ OperationEditorServer <- function(id, data) {
updated_text <- paste(current_text, "(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})

observeEvent(input$bracket_close, {
current_text <- input$editable_code
updated_text <- paste(current_text, ")", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$comma, {
current_text <- input$editable_code
updated_text <- paste(current_text, ",", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})

observeEvent(input$log, {
current_text <- input$editable_code
Expand Down Expand Up @@ -595,9 +617,14 @@ OperationEditorServer <- function(id, data) {
updated_text <- paste(current_text, "toupper(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$subset, {
observeEvent(input$get_rows, {
current_text <- input$editable_code
updated_text <- paste(current_text, "get_rows(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$get_cols, {
current_text <- input$editable_code
updated_text <- paste(current_text, "subset(", sep = " ")
updated_text <- paste(current_text, "get_cols(", sep = " ")
updateTextAreaInput(session, "editable_code", value = updated_text)
})
observeEvent(input$mean, {
Expand Down
142 changes: 142 additions & 0 deletions bs/R/SplitByGroup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# TODO: by group --> open popup --> choose column(e.g. substance column) --> choose level(compound x1234) --> work with this subset
# --> correlation, assumptions, visulaisation and Tests

SplitByGroupUI <- function(id) {
ui <- fluidPage(
tags$head(
tags$style(HTML("
.boxed-output {
border: 2px solid #900C3F;
padding: 10px;
border-radius: 5px;
margin-top: 10px;
}
.add-button {
position: relative;
padding-right: 20px;
}
.add-button::after {
content: '\\2295';
position: absolute;
top: 1.1px;
right: 5px;
font-size: 16px;
font-weight: bold;
color: #900C3F;
background-color: white;
width: 15px;
height: 15px;
display: flex;
justify-content: center;
align-items: center;
}
.model {
background-color: #f8f9fa;
padding: 15px;
border: 2px solid #c8c8c8;
border-radius: 5px;
margin-top: 10px;
}
.title {
font-size: 14px;
font-weight: bold;
margin-bottom: 10px;
color: #333;
}
"))
),
fluidRow(
div(
uiOutput(NS(id, "colnames_dropdown")),
class = "boxed-output"
),
div(
uiOutput(NS(id, "levels_dropdown")),
class = "boxed-output"
),
actionButton(NS(id, "split_data"), "Split data")
)
)
}

SplitByGroupServer <- function(id, data) {
moduleServer(id, function(input, output, session) {
# Reactive values
r_vals <- reactiveValues(
df = NULL,
is_filtered = FALSE
)

observe({
r_vals$df <- data$df
})

# Create colnames dropdown
output[["colnames_dropdown"]] <- renderUI({
req(!is.null(r_vals$df))
req(is.data.frame(r_vals$df))
colnames <- names(r_vals$df)
tooltip <- "Select the column by name which you want to split by"
div(
tags$label(
"Variable",
class = "tooltip",
title = tooltip,
`data-toggle` = "tooltip"
),
selectInput(
inputId = paste0("SG-colnames-dropdown_"),
label = "Variable",
choices = colnames[1:length(colnames)],
selected = NULL
)
)
})

# Show levels based on column which is choosen
output[["levels_dropdown"]] <- renderUI({
req(!is.null(r_vals$df))
req(is.data.frame(r_vals$df))
selected_col <- input[[paste0("colnames-dropdown_")]]
if (is.null(selected_col) || selected_col == "") {
return(NULL)
}
vals <- unique(r_vals$df[, selected_col])
tooltip <- "Select the level (group) by name which you want to use"
div(
tags$label(
"Variable levels",
class = "tooltip",
title = tooltip,
`data-toggle` = "tooltip"
),
selectInput(
inputId = paste0("SG-levels-dropdown_"),
label = "Variable levels",
choices = vals[1:length(vals)],
selected = NULL
)
)
})

# React to split data
observeEvent(input$split_data, {
req(!is.null(r_vals$df))
req(is.data.frame(r_vals$df))
e <- try({
selected_col <- input[[paste0("colnames-dropdown_")]]
selected_group <- input[[paste0("levels-dropdown_")]]
data$backup_df <- r_vals$df
data$df <- r_vals$df[r_vals$df[ ,selected_col] == selected_group, ]
data$filter_col <- selected_col
data$filter_group <- selected_group
})
if (inherits(e, "try-error")) {
showNotification("Invalid formula", type = "error")
}
})

# TODO: If data is split a button called remove filter has to appear

})
}
8 changes: 7 additions & 1 deletion bs/R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ source("statisticalTests.R")
source("DoseResponse.R")
source("OperationsModule.R")
source("FormulaModule.R")
source("SplitByGroup.R")

ui <- fluidPage(
useShinyjs(),
Expand Down Expand Up @@ -83,7 +84,10 @@ ui <- fluidPage(
)

server <- function(input, output, session) {
dataSet <- reactiveValues(df = NULL, formula = NULL)
dataSet <- reactiveValues(
df = NULL, formula = NULL,
backup_df = NULL, filter_col = NULL, filter_group = NULL
)

output$conditional_data_ui <- renderUI({
if (Sys.getenv("RUN_MODE") != "SERVER") {
Expand Down Expand Up @@ -212,6 +216,8 @@ server <- function(input, output, session) {
testsServer("TESTS", dataSet, listResults)
DoseResponseServer("DOSERESPONSE", dataSet, listResults)
FormulaEditorServer("FO", dataSet)
SplitByGroupServer("SG", dataSet)

}

Sys.setenv(RUN_MODE = "BROWSER") # SERVER
Expand Down
2 changes: 1 addition & 1 deletion bs/R/check_ast.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ allowed_fcts <- function() {
"mean", "sd", "median", "quantile", "range",
"sum", "diff", "min", "max", "scale",
"c", "vector", "length", "matrix", "~",
"subset"
"get_rows", "get_cols"
)
}

Expand Down
Loading

0 comments on commit 9e24a9d

Please sign in to comment.