I have started using targets (from package(targets)) in order to avoid re-runing my analyses several times and make them more reproducible. However, the same analysis that works fine when R in a normal R script gives me errors when I run the run.R file (using targets::tar_make()).I am using the data and script from this course: https://biostats-r.github.io/biostats/targets/
The data can be obtained using:
usethis::use_course("biostats-r/targets_workflow_svalbard")
My _targets.R file looks like this:
# Load packages required to define the pipeline:library(targets)# Set target options:tar_option_set( packages = c("tibble", "tidyverse"))# Run the R scripts in the R/ folder with your custom functions:tar_source("R/functions2.R")# Replace the target list below with your own:list( tar_target( name = raw_traits, command = "data/PFTC4_Svalbard_2018_Gradient_Traits.csv", format = "file" ), tar_target( name = traits, command = clean_data(raw_traits) ), tar_target( name = mod_area, command = fit_model(data = traits, response = "Value", predictor = "Gradient") ), tar_target( name = fig_area, command = make_figure(traits) ))
And myy functions2.R file is as follows:
# clean dataclean_data <- function(raw_traits){ traits <- raw_traits |> filter(!is.na(Value)) |> # order factor and rename variable gradient mutate(Gradient = case_match(Gradient,"C" ~ "Control","B" ~ "Nutrients"), Gradient = factor(Gradient, levels = c("Control", "Nutrients"))) |> filter(Taxon == "alopecurus magellanicus", Trait == "Leaf_Area_cm2")}# run a linear regressionfit_model <- function(data, response, predictor){ mod <- lm(as.formula(paste(response, "~", predictor)), data = data) mod}# make figuremake_figure <- function(traits){ ggplot(traits, aes(x = Gradient, y = Value)) + geom_boxplot(fill = c("grey80", "darkgreen")) + labs(x = "", y = expression(Leaf~area~cm^2)) + theme_bw()}
When I run targets:tar_make() I get the following error:
✔ skip target raw_traits▶ start target traits✖ error target traits▶ end pipeline [0.61 seconds]Error:! Error running targets::tar_make() Error messages: targets::tar_meta(fields = error, complete_only = TRUE) Debugging guide: https://books.ropensci.org/targets/debugging.html How to ask for help: https://books.ropensci.org/targets/help.html Last error: no applicable method for 'filter' applied to an object of class "character"
I have tried using only one function to make it simpler, but I always end up having a similar problem. Any suggestions on what the problem is, and how I could solve it? I find it strange that the script runs fine by itself, but then gives errors when using targets.