Drake R - Возможно ли создать цель с помощью триггера?

Я сейчас оцениваю drake для проекта, и мне интересно, есть ли способ программно создать цель или часть плана, который включает в себя триггер.

Мой идеальный пример использования / пример кода приведен ниже, но у меня возникают проблемы при попытке заставить его работать, так как Дрейк использует мою функцию в качестве команды и не читает триггер.

drake_fetch_remote_data <- function(remote_path, ...) {
  hash = retrive_remote_hash(remote_path)
  target(
    command = fetch_remote_data(remote_path),
    trigger = trigger(
      change = hash
    )
  )
}

plan <- drake_plan(
  df1 = drake_fetch_remote_data('path1'),
  df2 = drake_fetch_remote_data('path2'),
)
#> # A tibble: 2 x 2
#>   target   command                         
#>   <chr>    <expr>                          
#> 1 df1 drake_fetch_remote_data("path1")
#> 2 df2 drake_fetch_remote_data("path2")

Где, как показано ниже, код включает в себя триггер.

drake_fetch_remote_data <- function(remote_path, ...) {
  hash = retrive_remote_hash(remote_path)
  target(
    command = fetch_remote_data(remote_path),
    trigger = trigger(
      change = hash
    )
  )
}

plan <- drake_plan(
  df1 = target(
    command = fetch_remote_data('path1'),
    trigger = trigger(change = lookup_hash('path1'))
  )
)
# A tibble: 1 x 3
#   target command                    trigger                               
#   <chr>  <expr>                     <expr>                                
# 1 df1    fetch_remote_data("path1") trigger(change = lookup_hash("path1"))

1 ответ

Решение

Абсолютно! Есть несколько способов. Пожалуйста, посмотрите на https://ropenscilabs.github.io/drake-manual/plans.html.

library(drake)

# Write out paths one by one.
drake_plan(
  df = target(
    drake_fetch_remote_data(path),
    transform = map(path = c("path1", "path2"))
  )
)
#> # A tibble: 2 x 2
#>   target   command                         
#>   <chr>    <expr>                          
#> 1 df_path1 drake_fetch_remote_data("path1")
#> 2 df_path2 drake_fetch_remote_data("path2")

# Or generate a large collection of paths.
drake_plan(
  df = target(
    drake_fetch_remote_data(path),
    transform = map(path = !!paste0("path", seq_len(10)))
  )
)
#> # A tibble: 10 x 2
#>    target    command                          
#>    <chr>     <expr>                           
#>  1 df_path1  drake_fetch_remote_data("path1") 
#>  2 df_path2  drake_fetch_remote_data("path2") 
#>  3 df_path3  drake_fetch_remote_data("path3") 
#>  4 df_path4  drake_fetch_remote_data("path4") 
#>  5 df_path5  drake_fetch_remote_data("path5") 
#>  6 df_path6  drake_fetch_remote_data("path6") 
#>  7 df_path7  drake_fetch_remote_data("path7") 
#>  8 df_path8  drake_fetch_remote_data("path8") 
#>  9 df_path9  drake_fetch_remote_data("path9") 
#> 10 df_path10 drake_fetch_remote_data("path10")

# You can reference a variable that stores the paths.
paths <- paste0("path", seq_len(10))
drake_plan(
  df = target(
    drake_fetch_remote_data(path),
    transform = map(path = !!paths)
  )
)
#> # A tibble: 10 x 2
#>    target    command                          
#>    <chr>     <expr>                           
#>  1 df_path1  drake_fetch_remote_data("path1") 
#>  2 df_path2  drake_fetch_remote_data("path2") 
#>  3 df_path3  drake_fetch_remote_data("path3") 
#>  4 df_path4  drake_fetch_remote_data("path4") 
#>  5 df_path5  drake_fetch_remote_data("path5") 
#>  6 df_path6  drake_fetch_remote_data("path6") 
#>  7 df_path7  drake_fetch_remote_data("path7") 
#>  8 df_path8  drake_fetch_remote_data("path8") 
#>  9 df_path9  drake_fetch_remote_data("path9") 
#> 10 df_path10 drake_fetch_remote_data("path10")

# Shorter target names.
ids = as.numeric(seq_len(10))
paths <- paste0("path", seq_len(10))
drake_plan(
  df = target(
    drake_fetch_remote_data(path),
    transform = map(path = !!paths, id = !!ids, .id = id)
  )
)
#> # A tibble: 10 x 2
#>    target command                          
#>    <chr>  <expr>                           
#>  1 df_1   drake_fetch_remote_data("path1") 
#>  2 df_2   drake_fetch_remote_data("path2") 
#>  3 df_3   drake_fetch_remote_data("path3") 
#>  4 df_4   drake_fetch_remote_data("path4") 
#>  5 df_5   drake_fetch_remote_data("path5") 
#>  6 df_6   drake_fetch_remote_data("path6") 
#>  7 df_7   drake_fetch_remote_data("path7") 
#>  8 df_8   drake_fetch_remote_data("path8") 
#>  9 df_9   drake_fetch_remote_data("path9") 
#> 10 df_10  drake_fetch_remote_data("path10")

Создано 2019-05-03 пакетом представлением (v0.2.1)

Изменить 2019-05-05

Триггеры могут использовать группирование переменных так же, как команды.

library(drake)
ids <- as.numeric(seq_len(10))
paths <- paste0("path", seq_len(10))
drake_plan(
  df = target(
    fetch_data(path),
    trigger = trigger(change = hash_data(path)),
    transform = map(path = !!paths, id = !!ids, .id = id)
  )
)
#> # A tibble: 10 x 3
#>    target command              trigger                              
#>    <chr>  <expr>               <expr>                               
#>  1 df_1   fetch_data("path1")  trigger(change = hash_data("path1")) 
#>  2 df_2   fetch_data("path2")  trigger(change = hash_data("path2")) 
#>  3 df_3   fetch_data("path3")  trigger(change = hash_data("path3")) 
#>  4 df_4   fetch_data("path4")  trigger(change = hash_data("path4")) 
#>  5 df_5   fetch_data("path5")  trigger(change = hash_data("path5")) 
#>  6 df_6   fetch_data("path6")  trigger(change = hash_data("path6")) 
#>  7 df_7   fetch_data("path7")  trigger(change = hash_data("path7")) 
#>  8 df_8   fetch_data("path8")  trigger(change = hash_data("path8")) 
#>  9 df_9   fetch_data("path9")  trigger(change = hash_data("path9")) 
#> 10 df_10  fetch_data("path10") trigger(change = hash_data("path10"))

Создано 2019-05-06 пакетом представлением (v0.2.1)

Для большей ясности вы также можете дать команду target() в качестве именованного аргумента.

library(drake)
ids <- as.numeric(seq_len(10))
paths <- paste0("path", seq_len(10))
drake_plan(
  df = target(
    command = fetch_data(path), # now named for clarity
    trigger = trigger(change = hash_data(path)),
    transform = map(path = !!paths, id = !!ids, .id = id)
  )
)
#> # A tibble: 10 x 3
#>    target command              trigger                              
#>    <chr>  <expr>               <expr>                               
#>  1 df_1   fetch_data("path1")  trigger(change = hash_data("path1")) 
#>  2 df_2   fetch_data("path2")  trigger(change = hash_data("path2")) 
#>  3 df_3   fetch_data("path3")  trigger(change = hash_data("path3")) 
#>  4 df_4   fetch_data("path4")  trigger(change = hash_data("path4")) 
#>  5 df_5   fetch_data("path5")  trigger(change = hash_data("path5")) 
#>  6 df_6   fetch_data("path6")  trigger(change = hash_data("path6")) 
#>  7 df_7   fetch_data("path7")  trigger(change = hash_data("path7")) 
#>  8 df_8   fetch_data("path8")  trigger(change = hash_data("path8")) 
#>  9 df_9   fetch_data("path9")  trigger(change = hash_data("path9")) 
#> 10 df_10  fetch_data("path10") trigger(change = hash_data("path10"))

Создано 2019-05-06 пакетом представлением (v0.2.1)

Другие вопросы по тегам