Puzzled by purrr

Hi All,
This is my first post and I apologize in advance if this is a silly question.
So, I am exploring the beauties of purr, and I am trying to load several packages using purr::walk.

Here is what I am doing:

# SECTION: Load packages
c("DBI", "dplyr", "fs") |>
    purrr::walk(.f = ~ require(package = .x, warn.conflicts = FALSE))

But I get an unforeseen result:

Loading required package: .x
Loading required package: .x
Loading required package: .x

I expected it would load each R package in the vector instead.
I also tried without using the pipe and it did not work either.

I REALLY can't get my head around it as I have, just before, used the exact same kind of construct to execute files en masse and it worked...
Here it is:

direxe <- function(thedir = "") {
  glue::glue("{inilis$cmn}{thedir}/") |>
    fs::dir_ls(type = "file", regexp = ".+\\.R$") |>
    purrr::walk(.f = ~ source(.x))
}

Please help.
Thx,
Stephen

This is because the require() function is not generally expecting a character value. See below for the usage of character.only=TRUE:

# SECTION: Load packages
c("DBI", "dplyr", "fs") |>
  purrr::walk(.f = ~ require(package = .x, warn.conflicts = FALSE, character.only = TRUE))
#> Loading required package: DBI
#> Loading required package: dplyr
#> Loading required package: fs

Created on 2025-01-23 with reprex v2.1.1

Oh, thx @StatSteph , did not know that, have always used library and require using the package names as strings...

And YES, it works now.
THX!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.