Skip to content

Commit 252e7b2

Browse files
committed
use parse_safe() in sf.R
This patch fixes an example that triggers an error: library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) ggplot(nc) + geom_sf(aes(fill = AREA)) + scale_y_continuous( breaks = c(34, 35, 36), labels = c("34*degree*N", "", "36*degree*N") ) #> Error in parse(text = x)[[1]]: subscript out of bounds See #2867 for more details.
1 parent 5dd869e commit 252e7b2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

R/sf.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,6 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
476476
if (!is.null(graticule$plot12))
477477
graticule$degree_label[!graticule$plot12] <- NA
478478

479-
# parse labels into expressions if required
480-
if (any(grepl("degree", graticule$degree_label)))
481-
graticule$degree_label <- lapply(graticule$degree_label, function(x) parse(text = x)[[1]])
482-
483479
graticule
484480
},
485481

@@ -553,11 +549,18 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
553549
graticule <- panel_params$graticule
554550
east <- graticule[graticule$type == "E" & !is.na(graticule$degree_label), ]
555551

552+
labs <- east$degree_label
553+
554+
# parse labels into expressions if required
555+
if (any(grepl("degree", labs))) {
556+
labs <- parse_safe(as.character(labs))
557+
}
558+
556559
list(
557560
top = nullGrob(),
558561
bottom = guide_axis(
559562
east$x_start,
560-
east$degree_label,
563+
labs,
561564
position = "bottom",
562565
theme = theme
563566
)
@@ -568,10 +571,17 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
568571
graticule <- panel_params$graticule
569572
north <- graticule[graticule$type == "N" & !is.na(graticule$degree_label), ]
570573

574+
labs <- north$degree_label
575+
576+
# parse labels into expressions if required
577+
if (any(grepl("degree", labs))) {
578+
labs <- parse_safe(as.character(labs))
579+
}
580+
571581
list(
572582
left = guide_axis(
573583
north$y_start,
574-
north$degree_label,
584+
labs,
575585
position = "left",
576586
theme = theme
577587
),

0 commit comments

Comments
 (0)