0

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

Oamar Kanji
  • 1,824
  • 6
  • 24
  • 39
  • 1
    You could programmatically write `_targets.R` before running the pipeline. This is what https://github.com/wlandau/targets-shiny/blob/main/R/pipeline.R does. Alternatively, you could `level.name` and `level.value` from file targets and write the files in the app's file space. – landau May 03 '22 at 13:46
  • @landau thank you! This is helpful I will try implement this. I am struggling to find good documentation about what tar_helpers does, specifically what the path parameter does – Oamar Kanji May 03 '22 at 19:50
  • 1
    It's documented at http://docs.ropensci.org/targets/reference/tar_helper.html. It writes R code to a file you specify at `path`. – landau May 03 '22 at 20:24
  • @landau thanks for your advice, I am implementing write_pipeline in my code. When I call write_pipeline, I pass my variables to it and can print them out in the tar_helper function, but when I call tar_make() they seem to be gone and I am getting an error as the variable name does not have a variable – Oamar Kanji May 04 '22 at 04:38
  • 1
    Hard to say without a reprex, but the general technique worked in https://github.com/wlandau/targets-shiny. The `_targets.R` file needs to load the helper script you write, and the helper script or the `_targets.R` file needs to assign the global variables you need. – landau May 04 '22 at 13:49
  • Thanks for your advice once again. I think I understand what you mean but have included the code above, thanks for your time – Oamar Kanji May 04 '22 at 17:27
  • I suspect the issue might be "The _targets.R file needs to load the helper script you write". I am not sure how to achieve this, hopefully the code that I added above will help – Oamar Kanji May 04 '22 at 17:33
  • 1
    Looks like values for variables `level.name` and `level.value` were not inserted into the code. You can verify this by reading the `_targets.R` file that was generated. With `tar_helper()`, you can use [`!!` from tidy evaluation](https://www.r-bloggers.com/2019/07/bang-bang-how-to-program-with-dplyr/) to insert values from the environment, e.g. `!!level.name`. – landau May 04 '22 at 18:22
  • Thats it! It now works as expected, thank you very much for your time and expertise! It has been very helpful to me – Oamar Kanji May 04 '22 at 23:37

0 Answers0