ocpu.rplot调用远程R绘图函数丢失ggrepel文本标签求助
I’ve run into similar quirks with OpenCPU and ggplot extensions like ggrepel before, so here are targeted fixes to get those missing labels showing up in your ocpu.rplot output:
1. Return the ggplot object directly instead of printing it
Your current function uses print(g1) to render the plot, but ocpu.rplot handles returned ggplot objects more reliably. The print statement might not capture ggrepel’s full rendering pipeline in the OpenCPU API context. Modify your function to return the ggplot object directly:
#' generate a test graph by ggplot+ggrepel, use offical ex: https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html #' @return A ggplot graph #' @examples plot_test() #' @rdname plot_test #' @export plot_test <- function() { set.seed(42) data("mtcars") dat2 <- subset(mtcars, wt > 3 & wt < 4) # Hide all of the text labels. dat2$car <- "" # Let's just label these items. ix_label <- c(2,3,16) dat2$car[ix_label] <- rownames(dat2)[ix_label] g1 <- ggplot(dat2, aes(wt, mpg, label = car)) + geom_point(color = ifelse(dat2$car == "", "grey50", "red")) + geom_text_repel() g1 # Return the ggplot object instead of printing it }
2. Explicitly set plot device dimensions
ggrepel needs enough canvas space to render labels without clipping. ocpu.rplot often uses a smaller default device size compared to direct session.getLoc() calls. You can define dimensions either in your R function or JavaScript call:
Option A: Define device in your R function
plot_test <- function() { set.seed(42) data("mtcars") dat2 <- subset(mtcars, wt > 3 & wt < 4) dat2$car <- "" ix_label <- c(2,3,16) dat2$car[ix_label] <- rownames(dat2)[ix_label] # Create a png device with sufficient size for ggrepel labels png(width = 800, height = 600, res = 120) g1 <- ggplot(dat2, aes(wt, mpg, label = car)) + geom_point(color = ifelse(dat2$car == "", "grey50", "red")) + geom_text_repel() print(g1) dev.off() # Close the device invisible(g1) # Return object for API compatibility }
Option B: Pass device parameters in your ocpu.rplot call
ocpu.rplot("plot_test", {}, { device: "png", width: 8, height: 6, units: "in", res: 120 }, function(session) { // Load the rendered plot into your page session.getObject(function(obj){ $("#plot").attr("src", obj); }); });
3. Update ocpu.js to the latest version
The ocpu-0.4.js library is quite outdated, and newer OpenCPU server versions may have compatibility issues with older client libraries. Updating to the latest ocpu.js version will resolve many rendering inconsistencies with ggplot extensions like ggrepel.
4. Verify label placement context
Ggrepel’s label placement algorithm relies on the plot’s final dimensions during rendering. By explicitly setting size parameters, you ensure the algorithm has correct canvas boundaries to work with, preventing labels from being pushed outside the visible area (which makes them appear "missing").
After trying these steps, your ocpu.rplot output should match the correctly labeled plot from session.getLoc().
内容的提问来源于stack exchange,提问作者cywhale Weng

