How to pass values into _targets.R or use dynamic variables

517 views Asked by At

I have a shiny app that sources and calls tar_make() a _targets.R file. In this file I have two variables who's values I have hard coded but I want to set these variable values dynamically from input texts my Shiny UI. Is there a way for me to pass these variables to the _targets.R file? Or is there some method provided by targets that can help me?

This is the first part of my _targets.R file, level.name and level.value are the variables that I want to set dynamically.

enter image description here

Also, is it possible to run the tar_make() function multiple times, each time passing in different values for level.name and level.value ?

Edit: creating a write_pipline

app.R

source("pipeline.R")

level.name <- "Order"
level.value <- "ORDER Poputre"

level_name_dir <- file.path("level", level.name)

level_value_dir <- file.path(level_name_dir, level.value)
if (!dir.exists(level_value_dir)) dir.create(level_value_dir, recursive = TRUE)

## note: level_value_dir <- level/Order/ORDER Poputre

write_pipeline(level_value_dir, level.name, level.value)
script <- file.path(level_value_dir, "_targets.R")
store <- file.path(level_value_dir)


tar_make(
    script = script,
    store = store
)

ui <- htmlTemplate(...)
server <- function(input, output) ...
shinyApp(ui = ui, server = server)

pipeline.R (in the same directory level as app.R)

write_pipeline <- function(
  path,
  level.name,
  level.value
  ) {

  tar_helper(file.path(path, "_targets.R"), {
    library(targets)
    tar_option_set(packages = c("data.table", "tidyverse", "rjson"))
    files.sources <- list.files("_functions")
    sapply(paste("_functions/", files.sources, sep = ""), source)

    list(
      ###########################
      ### Set level variables ###
      ###########################
      tar_target(
        level_name,
        level.name
      ),
      tar_target(
        level_value,
        level.value
      )
     })
    }

This seems to create the _targets.R file and its required directories in the intended place. But the variables that I pass to write_pipeline are not being recognized when I call tar_make

enter image description here

Output

enter image description here

0

There are 0 answers