Skip to content

Commit a52e7fd

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 a52e7fd

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

R/sf.R

Lines changed: 17 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

@@ -551,13 +547,21 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
551547

552548
render_axis_h = function(self, panel_params, theme) {
553549
graticule <- panel_params$graticule
550+
# browser()
554551
east <- graticule[graticule$type == "E" & !is.na(graticule$degree_label), ]
555552

553+
labs <- east$degree_label
554+
555+
# parse labels into expressions if required
556+
if (any(grepl("degree", labs))) {
557+
labs <- parse_safe(as.character(labs))
558+
}
559+
556560
list(
557561
top = nullGrob(),
558562
bottom = guide_axis(
559563
east$x_start,
560-
east$degree_label,
564+
labs,
561565
position = "bottom",
562566
theme = theme
563567
)
@@ -568,10 +572,17 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
568572
graticule <- panel_params$graticule
569573
north <- graticule[graticule$type == "N" & !is.na(graticule$degree_label), ]
570574

575+
labs <- north$degree_label
576+
577+
# parse labels into expressions if required
578+
if (any(grepl("degree", labs))) {
579+
labs <- parse_safe(as.character(labs))
580+
}
581+
571582
list(
572583
left = guide_axis(
573584
north$y_start,
574-
north$degree_label,
585+
labs,
575586
position = "left",
576587
theme = theme
577588
),

0 commit comments

Comments
 (0)