Whenever I open a specific file ('mapping-functions.R') in my package's R/ directory, RStudio crashes with the message "R Session Aborted":
Only opening this specific file in this specific project produces the consistent error. If I open the file in another project there is no crash. If I open a different file in the same project, there is no crash.
I tried resetting RStudio state by renaming .local/share/rstudio, removing .Rproj.user, .Rprofile, and .Rhistory to no avail.
If I comment out everything in mapping-functions.R, RStudio doesn't crash. If I copy and paste the contents of the file to another .R file, RStudio crashes all the same.
Following the guide here I wasn't able to find a /cores/core.xxxx file as expected. But I did see some errors at the terminal console:
Failed load Ace snippets for mode 'rd'
Since it says something about snippets, I disabled snippets in RStudio, removed .Rproj.user and tried again to no avail. Still crashes.
Here are the contents of the file:
#' Map Device Trackings
#'
#' Create Leaflet map widget of device trackings
#'
#' @inheritParams get_trackings
#' @param add_lines Should trackings be included as lines on the map?
#' @param add_points Should trackings be included as points on the map?
#' @param simplify_tolerance Should lines be simplified to reduce space? Units in degrees.
#' @param line_options List of other arguments passed to \code{addPolylines()}.
#' @param point_options List of other arguments passed to \code{addCircles()}.
#' @param ... Other arguments passed to \code{get_trackings}
#'
#' @importFrom dplyr distinct arrange
#' @importFrom purrr walk2
#' @importFrom rlang .data .env
#' @importFrom grDevices rainbow
#' @importFrom lubridate is.POSIXct
#'
#' @export
map_trackings <- function(devices,
start_date_time = NULL,
end_date_time = NULL,
add_lines = TRUE,
add_points = FALSE,
simplify_tolerance = 0.0001,
line_options = list(opacity = 0.8, weight = 5),
point_options = list(opacity = 0.9, radius = 1, fillOpacity = 0.9),
...) {
if (!requireNamespace(package = "sf", quietly = TRUE)) {
stop("sf package is required to use this function",
call. = FALSE)
}
if (!requireNamespace(package = "leaflet", quietly = TRUE)) {
stop("leaflet package is required to use this function",
call. = FALSE)
}
if (all(!add_lines, !add_points)) {
stop(call. = FALSE,
"Both add_lines & add_points cannot be FALSE")
}
stopifnot(is.numeric(devices) | is.character(devices))
stopifnot(is.POSIXct(try(as.POSIXct(start_date_time), silent = TRUE)))
stopifnot(is.POSIXct(try(as.POSIXct(end_date_time), silent = TRUE)))
if (is.character(devices)) {
devices <- get_device_ids(devices)
}
d <- get_trackings(
devices = devices,
start_date_time = start_date_time,
end_date_time = end_date_time,
columns = c("device_id", "timestamp", "latitude", "longitude")
)
if (is.null(d)) stop(call. = FALSE, "No trackings found")
d <- d %>%
distinct(.data$device_id, .data$latitude, .data$longitude, .keep_all = TRUE) %>%
arrange(.data$device_id, .data$timestamp)
lf <- leaflet::leaflet() %>%
leaflet::addProviderTiles(map = ., provider = 'Esri.WorldImagery')
device_colors <- qualitative_colors(length(unique(d$device_id)))
walk2(
unique(d$device_id),
device_colors,
.f = function(device_id, color) {
d <- d[which(d$device_id == device_id), ]
d2 <- matrix(c(d$longitude, d$latitude),
ncol = 2,
byrow = F)
sf_linestring <-
sf::st_linestring(d2) %>%
sf::st_sfc() %>%
sf::st_simplify(x = ., dTolerance = simplify_tolerance)
if (add_lines) {
lf <<- do.call(leaflet::addPolylines,
c(list(
map = lf,
data = sf_linestring,
color = color
),
line_options))
}
if (add_points) {
sf_point <-
sf_linestring %>%
sf::st_cast("POINT")
lf <<- do.call(leaflet::addCircles,
c(list(
map = lf,
data = sf_point,
fillColor = color,
color = color
),
point_options))
}
}
)
lf
}
I've been able to isolate the problem to only when running RStudio inside the specific project (already pointed that out) and only when "Show diagnostics for R" is toggled on in Global Options (everything else in Diagnostics tab is left unchecked). If I uncheck Show diagnostics for R, the crash never occurs.
Also, here is a smaller amount of code that reliably produces the crash after saving in an .R file inside the project and with the above toggle on:
walk2(
unique(d$device_id),
device_colors,
.f = function(device_id, color) {
d <- d[which(d$device_id == device_id), ]
d2 <- matrix(c(d$longitude, d$latitude),
ncol = 2,
byrow = F)
sf_linestring <-
sf::st_linestring(d2) %>%
sf::st_sfc() %>%
sf::st_simplify(x = ., dTolerance = simplify_tolerance)
if (add_lines) {
lf <<- do.call(leaflet::addPolylines,
c(list(
map = lf,
data = sf_linestring,
color = color
),
line_options))
}
if (add_points) {
sf_point <-
sf_linestring %>%
sf::st_cast("POINT")
lf <<- do.call(leaflet::addCircles,
c(list(
map = lf,
data = sf_point,
fillColor = color,
color = color
),
point_options))
}
}
)