读取Google Sheet数据构建ShinyApp地图时col_types参数报错求助
解决
googlesheets4::read_sheet()中col_types参数报错问题 问题根源
你的代码出现! col_types must have length 1, not length 10错误,核心原因有两个:
- 参数格式错误:
read_sheet()的col_types不支持命名向量格式,必须用cols()函数(来自googlesheets4或readr)定义列类型映射。 - 类型拼写错误:你使用的
"numerical"不是合法类型标识,正确的数值类型应为"numeric"(或简写"n")。
修正后的代码
library(shiny) library(leaflet) library(tidyverse) library(googlesheets4) library(shinyjs) ORGANIZATION_SHEET_ID <- "###googlesheetsID###" organization_data <- googlesheets4::read_sheet( ORGANIZATION_SHEET_ID, col_types = cols( Name = col_character(), Needs_Met = col_character(), Address = col_character(), Org_Structure = col_character(), Contact = col_character(), Submission_Date = col_character(), Lon = col_numeric(), Lat = col_numeric(), Type = col_character(), Hours = col_character() ) )
额外说明
- 若想简化写法,也可用字符串简写格式,比如
col_types = "ccccccnncc"(每个字符对应一列类型,c代表character,n代表numeric),但cols()写法可读性更强,适合列数较多的场景。 - 对于不需要强制指定类型的列,可使用
col_guess()让函数自动识别类型。
内容的提问来源于stack exchange,提问作者Jacob_white
相关产品推荐
相关产品推荐

