使用 plotly::api_create 和 ggplot2::geom_sf 函数时文件太大

File too big when using plotly::api_create and ggplot2::geom_sf functions

我正在尝试在 R 中使用 plotly 在线绘制空间数据,但我收到一条错误消息 "Request Entity Too Large This file is too big! Your current subscription is limited to 524 KB uploads." 关于如何解决该问题的任何线索?为了重现我的代码,您需要 (i) 在 plotly and (ii) download shapefiles of French departments available on my github repo 上注册。这 3 个文件应位于名为 shapefile 的文件夹中。在我看来,是 ggplot2 函数 geom_sf 产生的文件太大。我的代码如下

require(tidyverse)
require(ggplot2)

#Info required for online plotting 
Sys.setenv("plotly_username"="replace_by_your_username")
Sys.setenv("plotly_api_key"="replace_by_your_apikey")

#Read shapefile
dep <- sf::st_read("replace_with_the_correctPATH/shapefile/DEPARTEMENT.shp")

#Variable to plot
zz<-runif(length(dep$CODE_DEPT),-10,3)

#ggplot2 object
gg <- dep %>%
mutate(discrete = cut(zz, c(-10, seq(-3, 3, by = 1)))) %>%
ggplot() +
geom_sf(aes(fill = discrete, text = paste("Department:", dep$CODE_DEPT, "<br>", "bli", zz))) +
scale_fill_brewer(palette = "PuOr", name = "bla")

#Plotting the figure on your local computer works
#plotly::ggplotly(gg, tooltip = c("text"))

#Generate an error message
plotly::api_create(gg, tooltip = c("text"),filename = "sthing")

这似乎不是你的问题,但我也遇到过同样的事情。对于未来的读者,我的问题似乎是我使用的数据框(比如 100 行)是一个大于文件限制的大型数据集(15,000 行)的子集。

虽然我的子集很小并且完全在上传限制之内,但我不得不将子集保存为 csv,将其重新加载并使用新加载的数据帧作为我的 plotly 上传。尽管导入的数据框与子集数据框的行数相同,但我不得不断开子集与原始较大数据集的连接,不知道为什么。