使用R的map命令出现‘Corrupt x: no names’错误的排查与解决
The error you're encountering stems from a function name conflict: you loaded the maps package after tidyverse, which means the map() function from maps (used for drawing geographic maps) is masking the map() function from purrr (used for applying functions to lists). When you run map(by_country$data, country_model), R tries to use maps::map() instead of purrr::map()—which expects completely different inputs, hence the confusing error message.
Fix 1: Explicitly use purrr::map()
The simplest solution is to specify you want the list-processing map() from the purrr package:
models <- purrr::map(by_country$data, country_model)
Fix 2: Adjust library loading order
If you load tidyverse after maps, purrr::map() will take precedence over maps::map(). Rearrange your library calls like this:
# Load non-tidyverse packages first library(maps) library(mapdata) library(viridis) library(lubridate) library(stringr) library(modelr) # Load tidyverse last to ensure purrr's functions are active library(tidyverse)
Fix 3: Detach maps temporarily
If you don't need the maps package for this part of your workflow, unload it before using map():
detach("package:maps", unload = TRUE) models <- map(by_country$data, country_model)
Bonus: Keep models in your data frame
Instead of creating a separate models object, add the models directly to your by_country data frame using mutate()—this avoids function masking issues entirely:
by_country <- by_country %>% mutate(model = purrr::map(data, country_model))
Preventing this in the future
To check which map() function is active, run map::map. If it returns function (.x, .f, ...) purrr::map(.x, .f, ...), purrr::map() is in use. If it returns a function related to geographic maps, maps::map() is masking it.
内容的提问来源于stack exchange,提问作者Alec Smythe

