BristolMyersSquibb / blockr

Composable, extensible no-code UI
https://bristolmyerssquibb.github.io/blockr/
GNU General Public License v3.0
32 stars 3 forks source link

Confusing data parameter in plot layer block #296

Open DivadNojnarg opened 7 months ago

DivadNojnarg commented 7 months ago

Can't build a facet wrap layer block. cc @nbenn @JohnCoene

Error in identity() : argument "x" is missing, with no default
devtools::load_all()
library(ggplot2)

new_ggplot_block <- function(data, ...){
  col_names <- function(data) colnames(data)
  x_default <- function(data) col_names(data)[1]
  y_default <- function(data) col_names(data)[2]
  fields <- list(
    x = new_select_field(x_default, col_names, type = "name"),
    y = new_select_field(y_default, col_names, type = "name")
  )
  expr <- quote(
    ggplot2::ggplot(mapping = aes(x = .(x), y = .(y)))
  )
  classes <- c("ggplot_block", "plot_block")
  new_block(fields = fields,
            expr = expr,
            class = classes, ...)
}

ggplot_block <- function(data, ...) {
  initialize_block(new_ggplot_block(data, ...), data)
}

new_geompoint_block <- function(data, ...) {
  new_block(
    fields = list(),
    expr = quote(
      geom_point()
    ),
    class = c("geompoint_block", "plot_layer_block", "plot_block"),
    ...
  )
}

geompoint_block <- function(data, ...) {
  initialize_block(new_geompoint_block(data, ...), data)
}

new_theme_block <- function(data, ...) {
  new_block(
    fields = list(
      theme = new_select_field(
        "theme_minimal",
        grep("^theme_.*$", ls("package:ggplot2"), perl = TRUE, value = TRUE),
        type = "name"
      )
    ),
    expr = quote(
      .(theme)()
    ),
    class = c("theme_block", "plot_layer_block", "plot_block"),
    ...
  )
}

theme_block <- function(data, ...) {
  initialize_block(new_theme_block(data, ...), data)
}

sel_cols <- function(data){
  colnames(data)
}

new_facet_block <- function(data, ...) {
  new_block(
    expr = quote(
      ggplot2::facet_wrap(facets = .(facet_var))
    ),
    fields = list(
      facet_var = new_select_field(sel_cols(data)[1], sel_cols, type = "name")
    ),
    class = c("facet_block", "plot_layer_block", "plot_block")
  )
}

facet_block <- function(data, ...) {
  initialize_block(new_facet_block(data, ...), data)
}

stack <- new_stack(
  data_block,
  ggplot_block,
  geompoint_block,
  facet_block
)

After checking within generate_code.plot_block, the latest layer does not seem to be initialised.

DivadNojnarg commented 7 months ago

I have something @JohnCoene :

new_facet_block <- function(data, ...) {
  sel_cols <- function(data){
    colnames(data$data)
  }
  sel_cols_1 <- function(data){
    sel_cols(data)[1]
  }
  new_block(
    expr = quote(
      ggplot2::facet_wrap(facets = .(facet_var))
    ),
    fields = list(
      facet_var = new_select_field(sel_cols_1, sel_cols)
    ),
    class = c("facet_block", "plot_layer_block", "plot_block")
  )
}
DivadNojnarg commented 7 months ago

The data passed to the facet_block layer is a plot object and we should get its data with data$data (I acknowledge we should maybe rename the function parameters to make it more clear because it's confusing).