Giter Site home page Giter Site logo

Comments (4)

wlandau avatar wlandau commented on June 11, 2024 1

My recommendations are to:

  1. Use tar_files() instead of tar_change() and tar_file() individually (but please update tarchetypes to 6d72150 or later).
  2. Write a custom function to that handles the case when the directory is empty. In the example below it returns an error via stopifnot(), but you can implement different behavior: for example, supply the path of an existing file containing default placeholder data. I believe these kinds of checks should be handled early in the pipeline.
library(targets)
dir.create("tmp")
writeLines("line1", "tmp/a")
writeLines("line2", "tmp/b")

tar_script({
  library(tarchetypes)
  options(crayon.enabled = FALSE)
  list_files <- function(dir) {
    files <- dir(dir, full.names = TRUE)
    stopifnot(length(files) > 0L)
    files
  }
  tar_pipeline(
    tar_files(files, list_files("tmp")),
    tar_target(lines, readLines(files), pattern = map(files))
  )
})

tar_make()
#> ● run target files_files
#> ● run branch files_98d8cd68
#> ● run branch files_33ec7836
#> ● run branch lines_3ac9849f
#> ● run branch lines_e2330cb9

tar_read(lines)
#> [1] "line1" "line2"

Created on 2020-10-26 by the reprex package (v0.3.0)

There are workarounds that do not require tar_files():

library(targets)
dir.create("tmp")
writeLines("line1", "tmp/a")
writeLines("line2", "tmp/b")

tar_script({
  options(crayon.enabled = FALSE)
  list_files <- function(dir) {
    files <- dir(dir, full.names = TRUE)
    stopifnot(length(files) > 0L)
    files
  }
  tar_pipeline(
    tar_target(paths, list_files("tmp"), cue = tar_cue(mode = "always")),
    tar_target(files, paths, pattern = map(paths), format = "file"),
    tar_target(lines, readLines(files), pattern = map(files))
  )
})

tar_make()
#> ● run target paths
#> ● run branch files_fe460508
#> ● run branch files_dcad1233
#> ● run branch lines_ef9c4ed9
#> ● run branch lines_bc722500

Created on 2020-10-26 by the reprex package (v0.3.0)

from tarchetypes.

ginolhac avatar ginolhac commented on June 11, 2024 1

I ended using tar_files that remove all md5sum I was doing manually. And great idea with a placeholder, when I have no file, I catch this with a dummy file and listing further down is adapted. Thanks again! Much appreciated!

from tarchetypes.

ginolhac avatar ginolhac commented on June 11, 2024

Thanks a lot William for your incredible reactivity and helpful comments. I will try both approaches tomorrow.
Thanks again for your precious time

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

Glad to hear it's working for you.

from tarchetypes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.