wlandau / remakeGenerator

Generate large remake-style workflows with minimal code.
http://will-landau.com/remakeGenerator
5 stars 2 forks source link

Import remake project #6

Open krlmlr opened 7 years ago

krlmlr commented 7 years ago

Started a project with remake, now want to try your package. Challenge: Get a working workflow.R from a remake.yml file. The following code worked for me:

library(tibble)
library(dplyr)
library(forcats)
library(tidyr)

is_syntactic <- function(x) {
  ret <- make.names(x) == x
  ret[is.na(x)] <- FALSE
  ret
}

tickit <- function(x) {
  needs_ticks <- !is_syntactic(x)
  x[needs_ticks] <- paste0("`", gsub("`", "\\\\`", x[needs_ticks]), "`")
  x
}

remake <- yaml::yaml.load_file("remake.yml")

library <- "library(remakeGenerator)"

commands <-
  remake$targets %>%
  enframe("target") %>%
  mutate(target = fct_inorder(target)) %>%
  mutate(value = lapply(value, enframe)) %>%
  unnest %>%
  spread(name, value) %>%
  mutate(command = unlist(command)) %>%
  summarize(text = paste0(tickit(as.character(target)), " = ", command, collapse = ",\n  ")) %>%
  { paste0("commands <- commands(\n  ", .$text, ")") }

targets <- "targets <- targets(imported = commands)"

packages <- paste0("packages <- c(", paste0('"', remake$packages, '"', collapse = ", "), ")")

sources <- paste0("sources <- c(", paste0('"', remake$sources, '"', collapse = ", "), ")")

workflow <- "workflow(targets = targets, sources = sources, packages = packages)"

con <- file("workflow.R", "w+")

writeLines(c(
  library, "",
  commands, "",
  targets, "",
  packages, "",
  sources, "",
  workflow), con)

close(con)

Let me know if you'd support something like this in your package.

wlandau commented 7 years ago

Neat idea, @krlmlr! I would absolutely support this. Got a pull request in mind?

krlmlr commented 7 years ago

I'll let it simmer a bit. Probably the packages used by this script should be added as "Suggests:"?

wlandau commented 7 years ago

If we plan to subsume this script into remakeGenerator as a function (maybe something like import_remake("remake.yml") or recover_generator("remake.yml")), I'm actually hoping to use "Imports:" and importFrom rather than "Suggests:". Sound okay?

Also, just so I understand: what is the role of backticks in parsing the targets? If you use commands_string() or commands_batch() instead of commands(), will we still need to be as careful as you are about how symbol are handled?

I just tried your script on the remake.yml from example_remakeGenerator("basic"); source("basic/workflow.R"), and I got "Error: wrong result size (25), expected 34 or 1" on mutate(command = unlist(command)). I'm using R 3.3.2 and the latest CRAN releases of tibble, dplyr, forcats, and tidyr.

wlandau commented 7 years ago

Also, no rush on the pull request. Regardless of which one of us gets to this first, let's make sure to add you as a contributor when the functionality is implemented.