Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: y scrolling code block, select block added, debug filter block e… #3

Merged
merged 25 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4eb5646
WIP: y scrolling code block, select block added, debug filter block e…
DivadNojnarg Sep 13, 2023
34cfb68
start add plot block
DivadNojnarg Sep 14, 2023
24a7f97
using generic for ui output
DivadNojnarg Sep 14, 2023
fc11371
play around with bslib for better block layout
DivadNojnarg Sep 15, 2023
2718e6c
cleanup theme
DivadNojnarg Sep 15, 2023
ae42b4f
Merge branch 'main' into select-block
DivadNojnarg Sep 26, 2023
e16684f
append
DivadNojnarg Sep 26, 2023
83241f1
missing ggplot2 dep
DivadNojnarg Sep 26, 2023
0ba8093
will fix lintr
DivadNojnarg Sep 26, 2023
09ad2b4
failing test due to tagList usage
DivadNojnarg Sep 26, 2023
df6f76b
warnings and notes
DivadNojnarg Sep 26, 2023
396dfa8
add method for server plot output
DivadNojnarg Sep 26, 2023
9ee4712
better title for blocks
DivadNojnarg Sep 26, 2023
9d3530d
fix select block
DivadNojnarg Sep 27, 2023
c48b76e
broken ...
DivadNojnarg Sep 28, 2023
9b786cd
Merge branch 'main' into select-block
DivadNojnarg Sep 28, 2023
c9dda36
missing remaining conflicts
DivadNojnarg Sep 28, 2023
99c4d3f
missing func
DivadNojnarg Sep 28, 2023
b4b31d7
select block works but not plot (infinite recursion ...)
DivadNojnarg Sep 28, 2023
02588b4
working plot block but recursive issue in the stack server initializa…
DivadNojnarg Oct 3, 2023
a6d5359
deps
DivadNojnarg Oct 3, 2023
419e257
fix warning in doc
DivadNojnarg Oct 3, 2023
71afa27
cleanup extra warning
DivadNojnarg Oct 3, 2023
11d4039
lint
DivadNojnarg Oct 3, 2023
cdb56e0
lint ...
DivadNojnarg Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
^LICENSE\.md$
^README\.(Rmd|html)$
^\.github$
^\.lintr$
^.*\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
README.html
.Rproj.user
4 changes: 4 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters: linters_with_defaults(
object_name_linter = NULL, # Because we use S3 and end up with is_initialized.field
object_usage_linter = NULL # When code is WIP this is annoying ...
)
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ Imports:
shiny,
dplyr,
utils,
methods
ggplot2,
bslib,
methods,
DT
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Generated by roxygen2: do not edit by hand

S3method(evalute_block,data_block)
S3method(evalute_block,plot_block)
S3method(evalute_block,transform_block)
S3method(generate_code,block)
S3method(generate_code,transform_block)
S3method(generate_server,block)
S3method(generate_server,data_block)
S3method(generate_server,plot_block)
S3method(generate_server,stack)
S3method(generate_server,transform_block)
S3method(generate_ui,block)
S3method(generate_ui,stack)
S3method(initialize_block,data_block)
S3method(initialize_block,plot_block)
S3method(initialize_block,transform_block)
S3method(initialize_field,field)
S3method(input_ids,block)
Expand All @@ -20,6 +23,7 @@ S3method(is_initialized,block)
S3method(is_initialized,field)
S3method(server_code,block)
S3method(server_output,block)
S3method(server_output,plot_block)
S3method(ui_code,block)
S3method(ui_input,hidden_field)
S3method(ui_input,list_field)
Expand All @@ -28,6 +32,7 @@ S3method(ui_input,select_field)
S3method(ui_input,string_field)
S3method(ui_input,variable_field)
S3method(ui_output,block)
S3method(ui_output,plot_block)
S3method(ui_update,hidden_field)
S3method(ui_update,list_field)
S3method(ui_update,range_field)
Expand All @@ -36,6 +41,7 @@ S3method(ui_update,string_field)
S3method(ui_update,variable_field)
S3method(update_field,field)
S3method(update_fields,data_block)
S3method(update_fields,plot_block)
S3method(update_fields,transform_block)
S3method(validate_field,hidden_field)
S3method(validate_field,list_field)
Expand Down Expand Up @@ -65,12 +71,16 @@ export(new_field)
export(new_filter_block)
export(new_hidden_field)
export(new_list_field)
export(new_plot_block)
export(new_range_field)
export(new_select_block)
export(new_select_field)
export(new_stack)
export(new_string_field)
export(new_variable_field)
export(plot_block)
export(range_field)
export(select_block)
export(select_field)
export(serve_stack)
export(server_code)
Expand All @@ -86,4 +96,5 @@ export(validate_field)
export(value)
export(values)
export(variable_field)
import(ggplot2)
importFrom(magrittr,"%>%")
113 changes: 100 additions & 13 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
#' @export
new_block <- function(fields, expr, name = rand_names(), ...,
class = character()) {

stopifnot(
is.list(fields), length(fields) >= 1L, all(lgl_ply(fields, is_field)),
is.language(expr),
is_string(name)
)

structure(fields, name = name, expr = expr, result = NULL, ...,
class = c(class, "block"))
structure(fields,
name = name, expr = expr, result = NULL, ...,
class = c(class, "block")
)
}

#' @param x An object inheriting form `"block"`
Expand Down Expand Up @@ -66,7 +67,6 @@ generate_code.block <- function(x) {
#' @rdname new_block
#' @export
generate_code.transform_block <- function(x) {

if (!is_initialized(x)) {
return(quote(identity()))
}
Expand All @@ -91,19 +91,31 @@ evalute_block.data_block <- function(x, ...) {
#' @rdname new_block
#' @export
evalute_block.transform_block <- function(x, data, ...) {
stopifnot(...length() == 0L)
eval(
substitute(data %>% expr, list(expr = generate_code(x))),
list(data = data)
)
}

#' @param data Result from previous block
#' @rdname new_block
#' @export
evalute_block.plot_block <- function(x, data, ...) {
stopifnot(...length() == 0L)
tmp_expr <- strsplit(deparse1(generate_code(x)), "\\+")[[1]]
gg_init <- str2lang(tmp_expr[[1]])
gg_geom <- str2lang(tmp_expr[[2]])

eval(
substitute(data %>% expr, list(expr = generate_code(x))),
substitute(data %>% expr + expr2, list(expr = gg_init, expr2 = gg_geom)),
list(data = data)
)
}

#' @rdname new_block
#' @export
new_data_block <- function(...) {

is_dataset_eligible <- function(x) {
inherits(
get(x, envir = as.environment("package:datasets"), inherits = FALSE),
Expand Down Expand Up @@ -139,7 +151,6 @@ data_block <- function(...) {
#' @rdname new_block
#' @export
initialize_block.data_block <- function(x, ...) {

env <- list()

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

#' @param data Tabular data to filter (rows)
#' @param columns,values Definition of the equality filter
#' @param columns Definition of the equality filter.
#' @param values Definition of the equality filter.
#' @rdname new_block
#' @export
new_filter_block <- function(data, columns = colnames(data)[1L],
Expand Down Expand Up @@ -240,10 +252,80 @@ filter_block <- function(data, ...) {
initialize_block(new_filter_block(data, ...), data)
}

#' @param data Tabular data in which to select some columns.
#' @param columns Column(s) to select.
#' @rdname new_block
#' @export
initialize_block.transform_block <- function(x, data, ...) {
new_select_block <- function(data, columns = colnames(data)[1], ...) {
all_cols <- function(data) colnames(data)

# Select_field only allow one value, not multi select
fields <- list(
column = new_select_field(columns, all_cols, multiple = TRUE)
)

new_block(
fields = fields,
expr = quote(
dplyr::select(.(column))
),
...,
class = c("select_block", "transform_block")
)
}

#' @rdname new_block
#' @export
select_block <- function(data, ...) {
initialize_block(new_select_block(data, ...), data)
}

#' @param data Tabular data in which to select some columns.
#' @param plot_opts List containing options for ggplot (color, ...).
#' @param ... Any other params. TO DO
#' @rdname new_block
#' @import ggplot2
#' @export
new_plot_block <- function(
data,
plot_opts = list(colors = c("blue", "red")),
...
) {
# For plot blocks, fields will create input to style the plot ...
all_cols <- function(data) colnames(data)
fields <- list(
x_var = new_select_field(all_cols(data)[[1]], all_cols),
y_var = new_select_field(all_cols(data)[[2]], all_cols),
color = new_select_field(plot_opts$colors[[1]], plot_opts$colors)
)

new_block(
fields = fields,
expr = quote(
ggplot() +
geom_point(
# We have to use aes_string over aes
mapping = aes_string(
x = .(x_var),
y = .(y_var)
),
color = .(color)
)
),
...,
class = c("plot_block")
)
}

#' @rdname new_block
#' @export
plot_block <- function(data, ...) {
initialize_block(new_plot_block(data, ...), data)
}

#' @rdname new_block
#' @export
initialize_block.transform_block <- function(x, data, ...) {
env <- list(data = data)

for (field in names(x)) {
Expand All @@ -254,6 +336,10 @@ initialize_block.transform_block <- function(x, data, ...) {
x
}

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

#' @rdname new_block
#' @export
update_fields <- function(x, ...) {
Expand All @@ -264,13 +350,11 @@ update_fields <- function(x, ...) {
#' @rdname new_block
#' @export
update_fields.data_block <- function(x, session, ...) {

args <- list(...)

stopifnot(setequal(names(args), names(x)))

for (field in names(x)) {

env <- args[-which(names(args) == field)]

x[[field]] <- update_field(x[[field]], args[[field]], env)
Expand All @@ -284,13 +368,11 @@ update_fields.data_block <- function(x, session, ...) {
#' @rdname new_block
#' @export
update_fields.transform_block <- function(x, session, data, ...) {

args <- list(...)

stopifnot(setequal(names(args), names(x)))

for (field in names(x)) {

env <- c(
list(data = data),
args[-which(names(args) == field)]
Expand All @@ -302,3 +384,8 @@ update_fields.transform_block <- function(x, session, data, ...) {

x
}

#' @param data Block input data
#' @rdname new_block
#' @export
update_fields.plot_block <- update_fields.transform_block
5 changes: 0 additions & 5 deletions R/field.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#' @export
new_field <- function(value, ..., type = c("literal", "name"),
class = character()) {

x <- list(value = value, ...)

stopifnot(is.list(x), length(unique(names(x))) == length(x))
Expand Down Expand Up @@ -44,7 +43,6 @@ update_field <- function(x, new, env = list()) {
#' @rdname new_field
#' @export
update_field.field <- function(x, new, env = list()) {

x <- eval_set_field_value(x, env)

if (is_truthy(new)) {
Expand Down Expand Up @@ -85,7 +83,6 @@ is_field <- function(x) inherits(x, "field")
#' @rdname new_field
#' @export
validate_field.string_field <- function(x) {

val <- value(x)

if (!is.character(val) || length(val) != 1L) {
Expand All @@ -108,7 +105,6 @@ string_field <- function(...) validate_field(new_string_field(...))
#' @rdname new_field
#' @export
validate_field.select_field <- function(x) {

val <- value(x)
opt <- value(x, "choices")

Expand Down Expand Up @@ -144,7 +140,6 @@ select_field <- function(...) validate_field(new_select_field(...))
#' @rdname new_field
#' @export
value <- function(x, name = "value") {

stopifnot(is_field(x))

res <- x[[name]]
Expand Down
Loading