Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

How to optimize this R script to use the minimum CPU and Memory possible

$
0
0

I built this R script that generate a map and a background tiles, the problem is, I need to run it on PowerBI service, which has a very constrained resources (Ram and CPU), I attached a reproducible example

This example works fine in PowerBI service, but when I tried it with my real data only the raster or the map works, but when I do both, I get you exceed the resource available, and as it is not documented, I don't know if the issue is CPU or RAM.

what's the best way to profile this code and check which section to change

please notice the dataset is a raster saved as ASCII, using saveRDS, it is done outside PowerBI and loaded as a csv file, as PowerBI does not read binary data

# 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)

Viewing all articles
Browse latest Browse all 201977

Trending Articles