Skip to content

Commit

Permalink
Merge branch 'main' into 61-hide-inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene authored Oct 27, 2023
2 parents b37e0bb + 8c606ed commit 851dc8e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
linters: linters_with_defaults(
line_length_linter = line_length_linter(100L),
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 ...
)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export(add_block)
export(arrange_block)
export(asfactor_block)
export(cheat_block)
export(create_block)
export(data_block)
export(demo_arrange_block)
export(demo_data_block)
Expand Down
41 changes: 41 additions & 0 deletions R/create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' Create a new block
#'
#' Create a new block
#'
#' @param name Block name.
#' @param type Type of block to create.
#' @param file Output file.
#' If `NULL` then it constructs `R/<name>-block.R`.
#'
#' @export
create_block <- function(
name,
type = c("transform", "plot"),
file = NULL) {
if (missing(name)) {
stop("Missing `name`")
}

type <- match.arg(type) |>
(\(.) sprintf("%s_block", .))()

if (is.null(file)) {
file <- file.path(
"R",
sprintf("%s-block.R", name)
)
}

file_name <- sprintf("%s.R", type)
infile <- system.file(
file.path("templates", file_name),
package = "blockr"
)

content <- readLines(infile) |>
gsub("NAME", name, x = _)

writeLines(content, con = file)

cat("File", file, "created!\n")
}
30 changes: 30 additions & 0 deletions inst/templates/plot_block.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' NAME
#'
#' A new plot block.
#'
#' @export
NAME_block <- function(data, ...) {
all_cols <- function(data) colnames(data)

fields <- list(
x_var = new_select_field("VISIT", all_cols),
y_var = new_select_field("MEAN", all_cols)
)

new_block(
fields = fields,
expr = quote({
ggplot(
data,
mapping = aes_string(
x = .(x_var),
y = .(y_var)
)
) +
geom_point() +
geom_line()
}),
...,
class = c("NAME_block", "plot_block")
)
}
19 changes: 19 additions & 0 deletions inst/templates/transform_block.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' NAME
#'
#' A new transform block.
#'
#' @export
NAME_block <- function(data, ...) {
fields <- list(
n_rows = new_numeric_field(10, 10, nrow(data))
)

new_block(
fields = fields,
expr = quote({
head(data, .(n_rows))
}),
...,
class = c("NAME_block", "transform_block")
)
}
19 changes: 19 additions & 0 deletions man/create_block.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 851dc8e

Please sign in to comment.