使用R语言plotly包时Viewer面板显示空白的解决方法咨询
问题描述
按照plotly ggplot2入门指南操作后,RStudio的Viewer面板显示空白,无法生成可视化图表。以下是使用的代码、截图及系统环境信息:
运行代码
install.packages("plotly") devtools::install_github("ropensci/plotly") library(plotly) set.seed(100) d <- diamonds[sample(nrow(diamonds), 1000), ] p <- ggplot(data = d, aes(x = carat, y = price)) + geom_point(aes(text = paste("Clarity:", clarity)), size = 4) + geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut) ggplotly(p)
截图

系统环境信息
sessionInfo() R version 4.4.0 (2024-04-24 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22621) Matrix products: default locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C [5] LC_TIME=English_United States.utf8 time zone: Europe/Rome tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] rjson_0.2.21 scales_1.3.0 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 [6] dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 [11] tidyverse_2.0.0 plotly_4.10.4 ggplot2_3.5.1 loaded via a namespace (and not attached): [1] gtable_0.3.5 remotes_2.5.0 htmlwidgets_1.6.4 devtools_2.4.5 [5] processx_3.8.4 lattice_0.22-6 callr_3.7.6 tzdb_0.4.0 [9] ps_1.7.6 vctrs_0.6.5 tools_4.4.0 crosstalk_1.2.1 [13] generics_0.1.3 curl_5.2.1 parallel_4.4.0 fansi_1.0.6 [17] pkgconfig_2.0.3 Matrix_1.7-0 data.table_1.15.4 desc_1.4.3 [21] lifecycle_1.0.4 compiler_4.4.0 farver_2.1.2 munsell_0.5.1 [25] httpuv_1.6.15 usethis_2.2.3 htmltools_0.5.8.1 yaml_2.3.8 [29] lazyeval_0.2.2 urlchecker_1.0.1 later_1.3.2 pillar_1.9.0 [33] crayon_1.5.2 ellipsis_0.3.2 rsconnect_1.2.2 cachem_1.1.0 [37] sessioninfo_1.2.2 mime_0.12 nlme_3.1-164 tidyselect_1.2.1 [41] digest_0.6.35 stringi_1.8.4 labeling_0.4.3 splines_4.4.0 [45] fastmap_1.2.0 grid_4.4.0 colorspace_2.1-0 cli_3.6.2 [49] magrittr_2.0.3 pkgbuild_1.4.4 utf8_1.2.4 withr_3.0.0 [53] promises_1.3.0 bit64_4.0.5 timechange_0.3.0 httr_1.4.7 [57] bit_4.0.5 hms_1.1.3 memoise_2.0.1 shiny_1.8.1.1 [61] miniUI_0.1.1.1 viridisLite_0.4.2 mgcv_1.9-1 profvis_0.3.8 [65] rlang_1.1.3 Rcpp_1.0.12 xtable_1.8-4 glue_1.7.0 [69] pkgload_1.3.4 rstudioapi_0.16.0 vroom_1.6.5 jsonlite_1.8.8 [73] R6_2.5.1 fs_1.6.4
解决办法
- 卸载当前plotly包,仅安装CRAN官方版本,避免同时安装CRAN和GitHub版本引发冲突:
remove.packages("plotly") install.packages("plotly") library(plotly) - 检查RStudio Viewer设置:点击Viewer面板右上角下拉菜单,确认选中「Viewer」;若切换到「Browser」模式,查看图表能否在外部浏览器正常显示,排除Viewer自身问题。
- 清除htmlwidgets缓存:运行
htmlwidgets::setWidgetIdSeed(123)后重新生成图表,规避缓存导致的加载异常。 - 测试版本兼容性:当前ggplot2 3.5.1与plotly 4.10.4理论兼容,可尝试更新plotly至最新CRAN版本,或回退ggplot2到3.4.x版本测试。
- 在干净会话中运行:重启R后仅加载ggplot2和plotly,排除其他包的冲突影响:
library(ggplot2) library(plotly) set.seed(100) d <- diamonds[sample(nrow(diamonds), 1000), ] p <- ggplot(data = d, aes(x = carat, y = price)) + geom_point(aes(text = paste("Clarity:", clarity)), size = 4) + geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut) ggplotly(p) - 检查网络连接:plotly图表需加载外部JS资源,若处于受限网络环境(如内网),切换至正常网络后重试。
内容的提问来源于stack exchange,提问作者symkly
相关产品推荐
相关产品推荐

