Skip to content

Commit

Permalink
Merge pull request #373 from blockr-org/110-2-single-block-ctor
Browse files Browse the repository at this point in the history
Validation rework
  • Loading branch information
nbenn authored Jun 6, 2024
2 parents 238cecd + f044a1b commit ba16b88
Show file tree
Hide file tree
Showing 90 changed files with 1,868 additions and 2,164 deletions.
55 changes: 17 additions & 38 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Generated by roxygen2: do not edit by hand

S3method("value<-",field)
S3method("value<-",filesbrowser_field)
S3method("value<-",list_field)
S3method("value<-",upload_field)
S3method(block_body,block)
S3method(block_code,block)
S3method(block_combiner,plot_block)
Expand Down Expand Up @@ -31,14 +35,14 @@ S3method(download_ui,block)
S3method(download_ui,data_block)
S3method(download_ui,plot_block)
S3method(download_ui,transform_block)
S3method(evaluate_block,block)
S3method(evaluate_block,data_block)
S3method(evaluate_block,plot_block)
S3method(evaluate_block,plot_layer_block)
S3method(evaluate_block,transform_block)
S3method(generate_code,arrange_block)
S3method(generate_code,block)
S3method(generate_code,call)
S3method(generate_code,data_block)
S3method(generate_code,group_by_block)
S3method(generate_code,plot_block)
S3method(generate_code,stack)
S3method(generate_code,transform_block)
Expand All @@ -55,18 +59,18 @@ S3method(generate_ui,workspace)
S3method(handle_remove,block)
S3method(init,stack)
S3method(init,workspace)
S3method(initialize_block,block)
S3method(initialize_block,data_block)
S3method(initialize_block,default)
S3method(initialize_block,plot_block)
S3method(initialize_block,transform_block)
S3method(initialize_field,field)
S3method(inject_remove_button,block)
S3method(inject_remove_button,stack)
S3method(input_ids,block)
S3method(input_ids,field)
S3method(input_ids,hidden_field)
S3method(input_ids,list_field)
S3method(is_initialized,block)
S3method(is_initialized,field)
S3method(is_valid,field)
S3method(layout,block)
S3method(remove_button,block)
S3method(remove_button,stack)
Expand Down Expand Up @@ -106,28 +110,30 @@ S3method(ui_update,switch_field)
S3method(ui_update,upload_field)
S3method(ui_update,variable_field)
S3method(update_field,field)
S3method(update_field,hidden_field)
S3method(update_fields,data_block)
S3method(update_fields,plot_block)
S3method(update_fields,transform_block)
S3method(validate_field,default)
S3method(validate_field,field)
S3method(validate_field,filesbrowser_field)
S3method(validate_field,hidden_field)
S3method(validate_field,keyvalue_field)
S3method(validate_field,list_field)
S3method(validate_field,numeric_field)
S3method(validate_field,range_field)
S3method(validate_field,result_field)
S3method(validate_field,select_field)
S3method(validate_field,string_field)
S3method(validate_field,submit_field)
S3method(validate_field,switch_field)
S3method(validate_field,upload_field)
S3method(validate_field,variable_field)
S3method(value,field)
S3method(value,list_field)
S3method(value,variable_field)
export("%>%")
export("value<-")
export(add_block)
export(add_block_ui)
export(add_workspace_stack)
export(arrange_block)
export(as_log_level)
export(available_blocks)
export(block_body)
Expand All @@ -144,15 +150,10 @@ export(cnd_logger)
export(construct_block)
export(create_app_link)
export(create_block)
export(csv_block)
export(data_info)
export(dataset_block)
export(download_ui)
export(evaluate_block)
export(exprs_ui)
export(filesbrowser_block)
export(filesbrowser_field)
export(filter_block)
export(from_json)
export(generate_code)
export(generate_server)
Expand All @@ -164,10 +165,7 @@ export(get_workspace_settings)
export(get_workspace_stack)
export(get_workspace_stacks)
export(get_workspace_title)
export(group_by_block)
export(handle_remove)
export(head_block)
export(hidden_field)
export(init)
export(init_lock)
export(initialize_block)
Expand All @@ -179,12 +177,10 @@ export(is_field)
export(is_initialized)
export(is_locked)
export(is_stack)
export(is_valid)
export(is_workspace)
export(join_block)
export(json_block)
export(keyvalue_field)
export(layout)
export(list_field)
export(list_workspace_stacks)
export(lock)
export(log_debug)
Expand All @@ -193,7 +189,6 @@ export(log_fatal)
export(log_info)
export(log_trace)
export(log_warn)
export(mutate_block)
export(new_arrange_block)
export(new_block)
export(new_csv_block)
Expand All @@ -216,7 +211,6 @@ export(new_range_field)
export(new_rds_block)
export(new_result_block)
export(new_result_field)
export(new_sas_block)
export(new_select_block)
export(new_select_field)
export(new_stack)
Expand All @@ -228,22 +222,14 @@ export(new_upload_block)
export(new_upload_field)
export(new_variable_field)
export(new_xpt_block)
export(numeric_field)
export(observe_lock)
export(off_canvas)
export(range_field)
export(rds_block)
export(register_block)
export(register_blocks)
export(remove_button)
export(restore_workspace)
export(result_block)
export(result_field)
export(rm_workspace_stack)
export(rm_workspace_stacks)
export(sas_block)
export(select_block)
export(select_field)
export(serve_stack)
export(serve_workspace)
export(server_code)
Expand All @@ -255,10 +241,6 @@ export(set_workspace_settings)
export(set_workspace_stack)
export(set_workspace_title)
export(stack_header)
export(string_field)
export(submit_field)
export(summarize_block)
export(switch_field)
export(to_json)
export(toggle_lock)
export(uiCode)
Expand All @@ -270,15 +252,12 @@ export(unlock)
export(unregister_blocks)
export(update_field)
export(update_fields)
export(upload_block)
export(upload_field)
export(useBlockr)
export(validate_field)
export(validation_failure)
export(value)
export(values)
export(variable_field)
export(write_log)
export(xpt_block)
import(blockr.data)
import(dplyr)
import(shiny)
Expand Down
56 changes: 34 additions & 22 deletions R/block-core.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ initialize_block <- function(x, ...) {
#' @rdname initialize_block
#' @export
initialize_block.data_block <- function(x, ...) {

env <- list()

for (field in names(x)) {
Expand All @@ -74,7 +75,8 @@ initialize_block.data_block <- function(x, ...) {

#' @rdname initialize_block
#' @export
initialize_block.transform_block <- function(x, data, ...) {
initialize_block.block <- function(x, data, ...) {

env <- list(data = data)

for (field in names(x)) {
Expand All @@ -85,15 +87,6 @@ initialize_block.transform_block <- function(x, data, ...) {
x
}

#' @rdname initialize_block
#' @export
initialize_block.default <- initialize_block.transform_block

#' @rdname initialize_block
#' @export
initialize_block.plot_block <- initialize_block.transform_block


#' S3 generic for initialization
#'
#' Checks if a block or field (currently implemented methods)
Expand Down Expand Up @@ -142,6 +135,11 @@ generate_code.block <- function(x) {
#' @rdname generate_code
#' @export
generate_code.arrange_block <- function(x) {

if (!is_initialized(x)) {
return(quote(identity()))
}

where <- lapply(x, type_trans)

do.call(
Expand All @@ -154,7 +152,25 @@ generate_code.arrange_block <- function(x) {
)
}

generate_code.group_by_block <- generate_code.arrange_block
#' @rdname generate_code
#' @export
generate_code.group_by_block <- function(x) {

if (!is_initialized(x)) {
return(quote(identity()))
}

where <- lapply(x, type_trans)

do.call(
bquote,
list(
attr(x, "expr"),
where = lapply(where, as.list),
splice = TRUE
)
)
}

#' @rdname generate_code
#' @export
Expand Down Expand Up @@ -208,25 +224,19 @@ evaluate_block <- function(x, ...) {
#' @rdname evaluate_block
#' @export
evaluate_block.data_block <- function(x, ...) {

stopifnot(...length() == 0L)

eval(generate_code(x), new.env())
}

#' @param data Result from previous block
#' @rdname evaluate_block
#' @export
evaluate_block.transform_block <- function(x, data, ...) {
stopifnot(...length() == 0L)
eval(
substitute(data %>% expr, list(expr = generate_code(x))),
list(data = data)
)
}
evaluate_block.block <- function(x, data, ...) {

#' @rdname evaluate_block
#' @export
evaluate_block.plot_block <- function(x, data, ...) {
stopifnot(...length() == 0L)

eval(
substitute(data %>% expr, list(expr = generate_code(x))),
list(data = data)
Expand All @@ -236,7 +246,9 @@ evaluate_block.plot_block <- function(x, data, ...) {
#' @rdname evaluate_block
#' @export
evaluate_block.plot_layer_block <- function(x, data, ...) {
stopifnot(...length() == 0L)

stopifnot(...length() == 0L, inherits(data, "ggplot"))

eval(
substitute(data + expr, list(expr = generate_code(x))),
list(data = data)
Expand Down
Loading

0 comments on commit ba16b88

Please sign in to comment.