【问题标题】:How to optimize this R script to use the minimum CPU and Memory possible如何优化此 R 脚本以使用尽可能少的 CPU 和内存
【发布时间】:2020-03-31 06:53:26
【问题描述】:

我构建了这个生成地图和背景图块的 R 脚本,问题是,我需要在 PowerBI 服务上运行它,它的资源(内存和 CPU)非常有限,我附上了一个可重现的示例

这个例子在 PowerBI 服务中运行良好,但是当我用我的真实数据尝试它时,只有栅格或地图可以工作,但是当我同时这样做时,我让你超出了可用的资源,并且由于它没有记录,我不知道是 CPU 还是 RAM 的问题。

分析此代码并检查要更改的部分的最佳方法是什么

请注意数据集是保存为 ASCII 的栅格,使用 saveRDS,它在 PowerBI 外部完成并作为 csv 文件加载,因为 PowerBI 不读取二进制数据

# Input load. Please do not change, the dataset is generated by PowerBI, I change it only to have a reproducible example #
`dataset` = read.csv('https://raw.githubusercontent.com/djouallah/loadRobjectPBI/master/powerbidf.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
library(sf)
library(dplyr)
library(tmap)
library(tidyr)

tempdf <- dataset %>%
  filter(!is.na(Value))%>%
  dplyr::select(Index,Value)%>%
  arrange(Index)%>%
  mutate(Value = strsplit(as.character(Value), "@")) %>%
  unnest(Value)%>%
  dplyr::select(Value)


write.table(tempdf, file="test3.rds",row.names = FALSE,quote = FALSE,  col.names=FALSE)
rm(tempdf)
background <- readRDS('test3.rds', refhook = NULL)

dataset <- dataset[c("x","y","color","status","labels")]
dataset$color <- as.character(dataset$color)
dataset$labels <- as.character(dataset$labels)

map <- st_as_sf(dataset,coords = c("x", "y"), crs = 4326)

chartlegend <- dataset %>%
   dplyr::select(status,color)%>%
   distinct(status, color)%>%
   arrange(status)

rm(dataset)

tm_shape(background)+
           tm_rgb() +
           rm(background)+
           tm_shape(map) +
           tm_symbols(col = "color", size = 0.04,shape=19)+
           tm_shape(filter(map, !is.na(labels))) +
           tm_text(text="labels",col="white")+
           rm(map)+
           tm_add_legend(type='fill',labels=chartlegend$status, col=chartlegend$color)+
           tm_layout(frame = FALSE,bg.color = "transparent",legend.width=2)+
           tm_legend(position=c("left", "top"),text.size = 1.3)+
           rm(chartlegend)

【问题讨论】:

  • 如果我理解正确,您的脚本会(由 PowerBI)提供一个像您的 dataset 这样的对象,这是一个编码栅格,要恢复为栅格,您可以将其写为一个使用write.table 的表,它神奇地是一个栅格对象的RDS 表示,这样background 现在是一个栅格?因为 PowerBI 已经以这种格式对栅格数据进行了编码?
  • 其实我做到了,这里有更多上下文,datamonkeysite.com/2019/12/06/…

标签: r tidyr sf tmap


【解决方案1】:

更改代码以使用 base R 只是有点帮助

# Input load. Please do not change #
`dataset` = read.csv('https://raw.githubusercontent.com/djouallah/loadRobjectPBI/master/powerbidf.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #


library(sf)
library(tmap)

tempdf <-  dataset[dataset$Value!="",]
tempdf <- tempdf[c("Index","Value")]
tempdf <-  tempdf[order(tempdf$Index),]
tempdf <-  stack(setNames(strsplit(as.character(tempdf$Value),'@'), tempdf$Index))
tempdf <- tempdf["values"]
write.table(tempdf, file="test3.rds",row.names = FALSE,quote = FALSE,  col.names=FALSE)
rm(tempdf)
background <- readRDS('test3.rds', refhook = NULL)

dataset <- dataset[c("x","y","color","status","labels")]
dataset$color <- as.character(dataset$color)

map <- st_as_sf(dataset,coords = c("x", "y"), crs = 4326)


chartlegend <-     unique(dataset[c("status","color")])

rm(dataset)


tm_shape(background)+
tm_rgb() +
rm(background)+
tm_shape(map) +
tm_symbols(col = "color", size = 0.04,shape=19)+
tm_text(text="labels",col="white")+
rm(map)+
tm_add_legend(type='fill',labels=chartlegend$status, col=chartlegend$color)+
tm_layout(frame = FALSE,outer.margins = c(0.005, 0.6, 0.06, 0.005),bg.color = "transparent",legend.width=2)+
tm_legend(position=c("right", "top"),text.size = 1.3)+
rm(chartlegend)

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 2011-11-06
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多