Skip to content

Further considerations for callModule processing #17

@dgkf

Description

@dgkf

A long follow-up to #1 and #2 to discuss handling of callModule calls.

Using the nested shiny modules example app, the code it generates looks like this at the moment (below).

It's not particularly pretty, even by automatically generated code standards. It requires wrapping the output list and return result of the callModule call in an additional class structure and then using special accessor functions to extract the various components.

library(scriptgloss)
library(shiny)
library(dplyr)
library(ggplot2)
my_data <- ggplot2::mpg
scatterPlot <- function(data, cols) ggplot(data, aes_string(x = cols[1], y = cols[2])) + geom_point(aes(color = selected_)) + scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
linkedScatter <- function(input, output, session, data, left, right) {
    uniquesTable <- function(input, output, session, data, col) {
        filteredData <- function() {
            data() %>% dplyr::filter(selected_) %>% .[[col()]]
        }
        output$table <- function() {
            table(filteredData())
        }
        uniques <- function() {
            unique(filteredData())
        }
        .return <- uniques
        return(structure(output, return = .return, submodules = structure(list(), .Names = character(0)), class = c("scriptgloss_module", "list")))
    }
    uniques_table <- uniquesTable(input = structure(list(), .Names = character(0)), output = list(), session = NULL, data = dataWithSelection, col = function() right()[[1]])
    u <- callStaticModule(uniques_table)
    dataWithSelection <- function() {
        brushedPoints(data(), input$brush, allRows = TRUE)
    }
    output$plot1 <- function() {
        scatterPlot(dataWithSelection(), left())
    }
    output$plot2 <- function() {
        scatterPlot(dataWithSelection(), right())
    }
    output$uniques <- function() {
        paste("Unique Values:", paste(as.character(u()), collapse = ", "))
    }
    .return <- dataWithSelection
    return(structure(output, return = .return, submodules = list(uniques_table = uniques_table), class = c("scriptgloss_module", "list")))
}
scatters <- linkedScatter(input = structure(list(), .Names = character(0)), output = list(), session = NULL, data = function() my_data, left = function() c("cty", "hwy"), right = function() c("drv", "hwy"))
df <- callStaticModule(scatters)
sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions