Infra4NextGen Webinar
Assoc. Prof. for International Relations
University of Innsbruck
Research focus: Foreign and Security Policy; (Counter-)Terrorism; USA, Europe, Austria; social science research methods (v.a. QTA, DNA); academic writing and presentation; open and reproducible science
renv (Ushey and Wickham 2025){targets} (Landau 2025)After completing this webinar, participants will be able to
renv-package;{targets}-package as a pipeline tool for their R projects.Definition
“the ability of a second researcher to receive a set of files, including data, code, and documentation, and to recreate or recover the outputs of a research project, including figures, tables, and other key quantitative and qualitative results” (Kitzes 2017, 19).
“multiple inconsistent versions of code, data, or both” (Peikert, Lissa, and Brandmaier 2021, 838)
missing documentation and copy-and-paste errors in final reports
software dependencies
undocumented or ambiguous order of documentation
click!
renvrenv?library/
local/
cellar/
lock/
python/
sandbox/
staging/
{targets}{targets}?{targets}DO NOT HAVE TO USE IT FOR simple analyses{targets} expects users to adopt a function-oriented style of programming (see Functions)
functions should be organized according to three key steps of an analysis
save functions in subfolder R in the Rproject directory
###
# 00. configuration----
###
# install packages if needed
install.packages("tidyverse") # collection of packages for data science
# load packages
library(tidyverse)
###
# 10. loading data----
###
# read Cronos3, wave 2 dataset from data-folder
df <- read_csv("data/CRON3W2e01.1.csv")
# view head of the dataframe
head(df)
# select variables:
## w2gq1: How worried about climate change
## w2gq6: Humans meant to rule over nature
df <- df |> select(w2gq1, w2gq6, agegroup35) |>
rename("climate" = w2gq1, "rule" = w2gq6) |> # rename variables
filter(climate != 9) |> # remove "No answer"
filter(rule != 9) |> # remove "No answer"
filter(agegroup35 != 9) |>
mutate(rule = case_when( # recode answers in "Humans meant to rule over nature"
rule == 1 ~ 5, # Agree strongly becomes 5
rule == 2 ~ 4,
rule == 3 ~ 3,
rule == 4 ~ 2,
rule == 5 ~ 1) # Disagree strongly becomes 1
) |>
mutate(agegroup35 = as_factor(agegroup35)) # mutate agegroup35 to factor
df$agegroup35 <- fct_recode(df$agegroup35, "under 35" = "1",
"35 and above" = "2") # recode agegroup35
###
# 20. analyzing data----
###
fit_model <- lm(climate ~ rule, df)
summary(fit_model)
###
# 30. plotting results----
###
# plotting the model
ggplot(df, aes(x = rule, y = climate)) +
geom_jitter(color = "#66B32F", alpha = 0.3) +
geom_abline(intercept = fit_model$coefficients[1], slope = fit_model$coefficients[2],
color = "#E72E6B", linetype = 2) +
labs(x = "Humans meant to rule over nature") +
labs(y = "How worried about climate change") +
theme_minimal()
# violin plot of agegroup vs "How worried about climate change"
ggplot(df, aes(agegroup35, climate)) +
geom_violin() +
geom_jitter(color = "#2A4B9B", alpha = 0.3) +
labs(x = "") +
labs(y = "How worried about climate change") +
theme_minimal()From…
###
# 10. loading data----
###
# read Cronos3, wave 2 dataset from data-folder
df <- read_csv("data/CRON3W2e01.1.csv")
# select variables:
## w2gq1: How worried about climate change
## w2gq6: Humans meant to rule over nature
df <- df |> select(w2gq1, w2gq6, agegroup35) |>
rename("climate" = w2gq1, "rule" = w2gq6) |> # rename variables
filter(climate != 9) |> # remove "No answer"
filter(rule != 9) |> # remove "No answer"
filter(agegroup35 != 9) |>
mutate(rule = case_when( # recode answers in "Humans meant to rule over nature"
rule == 1 ~ 5, # Agree strongly becomes 5
rule == 2 ~ 4,
rule == 3 ~ 3,
rule == 4 ~ 2,
rule == 5 ~ 1) # Disagree strongly becomes 1
) |>
mutate(agegroup35 = as_factor(agegroup35)) |> # mutate agegroup35 to factor
mutate(agegroup35 = fct_recode(agegroup35, "under 35" = "1", # recode agegroup35
"35 and above" = "2"))… to 01_data.R
get_data <- function(file) {
read_csv(file) |>
select(w2gq1, w2gq6, agegroup35) |>
rename("climate" = w2gq1, "rule" = w2gq6) |> # rename variables
filter(climate != 9) |> # remove "No answer"
filter(rule != 9) |> # remove "No answer"
filter(agegroup35 != 9) |>
mutate(rule = case_when( # recode answers in "Humans meant to rule over nature"
rule == 1 ~ 5, # Agree strongly becomes 5
rule == 2 ~ 4,
rule == 3 ~ 3,
rule == 4 ~ 2,
rule == 5 ~ 1) # Disagree strongly becomes 1
) |>
mutate(agegroup35 = as_factor(agegroup35)) |>
mutate(agegroup35 = fct_recode(agegroup35, "under 35" = "1", # recode agegroup35
"35 and above" = "2"))
}From…
# plotting the model
ggplot(df, aes(x = rule, y = climate)) +
geom_jitter(color = "#66B32F", alpha = 0.3) +
geom_abline(intercept = fit_model$coefficients[1], slope = fit_model$coefficients[2],
color = "#E72E6B", linetype = 2) +
labs(x = "Humans meant to rule over nature") +
labs(y = "How worried about climate change") +
theme_minimal()
# violin plot of agegroup vs "How worried about climate change"
ggplot(df, aes(agegroup35, climate)) +
geom_violin() +
geom_jitter(color = "#2A4B9B", alpha = 0.3) +
labs(x = "") +
labs(y = "How worried about climate change") +
theme_minimal()… to 03_plot.R
plot_model <- function(data, model) {
ggplot(data, aes(x = rule, y = climate)) +
geom_jitter(color = "#66B32F", alpha = 0.3) +
geom_abline(intercept = model$coefficients[1], slope = model$coefficients[2],
color = "#E72E6B", linetype = 2) +
labs(x = "Humans meant to rule over nature") +
labs(y = "How worried about climate change") +
theme_minimal()
}
plot_violin <- function(data) {
ggplot(data, aes(agegroup35, climate)) +
geom_violin() +
geom_jitter(color = "#2A4B9B", alpha = 0.3) +
labs(x = "") +
labs(y = "How worried about climate change") +
theme_minimal()
}{targets}_targets.R# Load packages required to define the pipeline:
library(targets)
library(tarchetypes)
# Set target options:
tar_option_set(
packages = c("tidyverse") # Packages that your targets need for their tasks.
)
# Run the R scripts in the R/ folder with your custom functions:
tar_source()
# Replace the target list below with your own:
list(
tar_target(file, "data/CRON3W2e01.1.csv", format = "file"),
tar_target(data, get_data(file)),
tar_target(model, fit_model(data)),
tar_target(plot, plot_model(data, model)),
tar_target(violin_plot, plot_violin(data))
)# change function in 03_plot.R
plot_model <- function(data, model) {
ggplot(data, aes(x = rule, y = climate)) +
#geom_jitter(color = "#66B32F", alpha = 0.3) +
geom_jitter(color = "#ffed00", alpha = 0.3) +
geom_abline(intercept = model$coefficients[1],
slope = model$coefficients[2],
#color = "#E72E6B", linetype = 2) +
color = "#2A4B9B", linetype = 2) +
labs(x = "Humans meant to rule over nature") +
labs(y = "How worried about climate change") +
theme_minimal()
}{targets} and Quarto_targets.R# Load packages required to define the pipeline:
library(targets)
library(tarchetypes)
# Set target options:
tar_option_set(
packages = c("tidyverse", "tibble") # Packages that your targets need for their tasks.
)
# Run the R scripts in the R/ folder with your custom functions:
tar_source()
# Replace the target list below with your own:
list(
tar_target(file, "data/CRON3W2e01.1.csv", format = "file"),
tar_target(data, get_data(file)),
tar_target(model, fit_model(data)),
tar_target(plot, plot_model(data, model)),
tar_target(violin_plot, plot_violin(data)),
tar_quarto(report, "report.qmd")
)tar_make()Infra4NextGen Webinar | 3 June 2025