调用stringr::str_glue_data()函数报错,请求技术排查
Error in eval(expr, ...) with str_glue_data Hey there, let’s break down why you’re hitting that frustrating error and how to fix it! The core issue ties into how stringr and the glue package work together these days—here’s what you need to know:
First off, str_glue_data is no longer actively maintained in stringr; it’s just a thin wrapper that forwards calls to the glue_data() function from the glue package. If glue isn’t properly loaded in your environment (even if it’s installed), R can’t access the evaluation logic needed to parse the curly brace expressions, which triggers that eval error.
Here’s how to fix your code step by step:
1. Make sure glue is loaded
Glue is a tidyverse dependency, so you probably already have it installed—just load it alongside stringr:
library(stringr) library(glue) # This is the missing piece for most users!
2. Fix your individual calls
Let’s adjust each of your original commands to work reliably:
Original first command:
mtcars %>% str_glue_data("{rownames(.)} has {hp} hp")
Fixed version (using glue’s official glue_data with pipe):
mtcars %>% glue_data("{rownames(.)} has {hp} hp")
Or, if you want to stick strictly to stringr functions, use str_glue with the .data pronoun for clearer context:
mtcars %>% str_glue("{rownames(.data)} has {.data$hp} hp")
Original second command:
str_glue_data(mtcars, "has hp {hp}")
Fixed version:
glue_data(mtcars, "has hp {hp}") # Alternatively, using stringr's str_glue directly: str_glue("has hp {mtcars$hp}")
Original third command:
str_glue_data(.x = mtcars, "has hp {hp}")
Fixed version:
glue_data(.x = mtcars, "has hp {hp}")
Why this works
Stringr moved str_glue_data to the glue package because glue handles the evaluation of curly brace expressions far more reliably. Without glue loaded, stringr’s wrapper can’t access the underlying logic needed to parse your template strings, hence the eval error.
内容的提问来源于stack exchange,提问作者David Kaufman

