robust
robust

Reputation: 634

How can I import from multiple files in r-drake?

I want to import data of a similar category from multiple source files.

Every source has a short label.

How can I incorporate this into drake, without writing out every file as its own target?

I thought the following would work, but it does not. Ideally, I would like to have the targets raw_a and raw_b.

input_files <- list(
  'a' = 'file_1.csv',
  'b' = 'file_2.csv'
)

plan <-
  drake::drake_plan(
    raw = drake::target(
      import_file(file),
      transform = map(
        file = file_in(!! input_files)
      )
    )
  )

with

import_file <- function(file) {
  readr::read_csv(file, skip = 2)
}

Upvotes: 0

Views: 244

Answers (3)

mtwigzell
mtwigzell

Reputation: 1

file_in needs to around the whole string

plan <-
  drake::drake_plan(
    raw = drake::target(
      import_file(file),
      transform = map(
        file = list(
  file_in('file_1.csv'),
  file_in('file_2.csv')
)
      )
    )
  )

Upvotes: 0

landau
landau

Reputation: 5841

You are so close. file_in() needs to go literally in the command, not the transformation.

library(drake)
input_files <- c("file_1.csv", "file_2.csv")

plan <- drake_plan(
  raw = target(
    import_file(file_in(file)),
    transform = map(file = !!input_files)
  )
)

config <- drake_config(plan)
vis_drake_graph(config)

Created on 2019-10-19 by the reprex package (v0.3.0)

Upvotes: 3

robust
robust

Reputation: 634

This is probably the idiomatic solution.

plan <-
  drake::drake_plan(
    raw = drake::target(
      import_file(file),
      transform = map(
        file = file_in('file_1.csv', 'file_2.csv'),
        label = c('a', 'b'),
        .id = label
      )
    )
  )

Upvotes: 1

Related Questions