gpt4 book ai didi

r - 当 DataExplorer::plot_str() 调用时,其他图消失

转载 作者:行者123 更新时间:2023-12-04 12:53:25 25 4
gpt4 key购买 nike

我已经将 R 和 RStudio 与 base 和 ggplot 一起使用了几年。最近,似乎在生成某些图时,其他图会丢失/删除。
在 RStudio 中,我使用“绘图” Pane /选项卡和蓝色的左右箭头来查看已创建的绘图。消失,我的意思是创建了一个情节并且在这里可见,但是如果我使用箭头,我就看不到它了。它在那里,现在不存在了,它已经“消失”了。
enter image description here
使用“源”按钮运行下面的代码后,它做了 3 个图,我只能看到 2 个图。如果我使用 Command-Return 单步执行代码,我会看到生成的 3 个图,但中间的图丢失了。
在做了更多的测试之后,我看到这段代码是调用:

DataExplorer::plot_str(mtcars)
那就是问题所在。如果我评论该行并使用来自 DataExplorer 的另一个图,例如:
DataExplorer::plot_intro(mtcars)
脚本完成后,所有图都可用。
我相信一切都在使用最新版本:
  • mac 操作系统 - 11.5.2
  • R - R 版本 4.1.1 (2021-08-10)
  • RStudio - RStudio 2021.09.0+351
  • R 库 - 刚刚更新了所有 r 库

  • 这是一个简单的基本案例,似乎重现了这个问题。在下面的示例中,我使用的是 DataExplorer 库,但在 Keras 中发生了类似的事情。
    代码
    ###########################   Start Initialisation   ########################################################################################

    # Remove objects from environment
    rm(list = ls())

    # Clear the R studio console
    cat("\014")

    # Clear all plots in R studio
    try(dev.off(dev.list()["RStudioGD"]),silent<-TRUE)
    try(dev.off(),silent<-TRUE)

    # Load packages, installing first if not already installed
    if (!require(DataExplorer)) {
    install.packages("DataExplorer")
    library(DataExplorer)
    }

    ##################################

    # Use base plotting to plot iris, this works
    base::plot(iris, main="iris 1")

    # Use DataExplorer::plot_str() to plot mtcars, this works, but then disappears
    data("mtcars")
    # DataExplorer::plot_str(mtcars) # This causes the problem
    DataExplorer::plot_intro(mtcars) # This works

    # This plot appears, but seems to cause the one above "mtcars" to disappear
    base::plot(iris, main="iris 2")

    sessionInfo()
    输出:

    > # Clear all plots in R studio
    > try(dev.off(dev.list()["RStudioGD"]),silent<-TRUE)
    null device
    1

    > try(dev.off(),silent<-TRUE)

    > # Load packages, installing first if not already installed
    > if (!require(DataExplorer)) {
    + install.packages("DataExplorer")
    + library(DataExpl .... [TRUNCATED]

    > ##################################
    >
    > # Use base plotting to plot iris, this works
    > base::plot(iris, main="iris 1")

    > # Use DataExplorer::plot_str() to plot mtcars, this works, but then disappears
    > data("mtcars")

    > DataExplorer::plot_str(mtcars)

    > # This plot appears but seems to cause the one above "mtcars" to disappear
    > base::plot(iris, main="iris 2")

    > sessionInfo()
    R version 4.1.1 (2021-08-10)
    Platform: x86_64-apple-darwin17.0 (64-bit)
    Running under: macOS Big Sur 11.5.2

    Matrix products: default
    LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

    locale:
    [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

    attached base packages:
    [1] stats graphics grDevices utils datasets methods base

    other attached packages:
    [1] DataExplorer_0.8.2

    loaded via a namespace (and not attached):
    [1] pillar_1.6.3 compiler_4.1.1 tools_4.1.1 digest_0.6.28 jsonlite_1.7.2 evaluate_0.14 lifecycle_1.0.1 tibble_3.1.4 gtable_0.3.0 pkgconfig_2.0.3
    [11] rlang_0.4.11 igraph_1.2.6 DBI_1.1.1 yaml_2.2.1 parallel_4.1.1 xfun_0.26 fastmap_1.1.0 gridExtra_2.3 dplyr_1.0.7 knitr_1.36
    [21] generics_0.1.0 vctrs_0.3.8 htmlwidgets_1.5.4 grid_4.1.1 tidyselect_1.1.1 glue_1.4.2 data.table_1.14.2 R6_2.5.1 fansi_0.5.0 rmarkdown_2.11
    [31] ggplot2_3.3.5 purrr_0.3.4 magrittr_2.0.1 scales_1.1.1 ellipsis_0.3.2 htmltools_0.5.2 networkD3_0.4 assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2
    [41] munsell_0.5.0 crayon_1.4.1
    我认为这个问题可能是由于混合了不同的绘图库,但是下面的代码绘制了 6 个图,3 个使用 base:plot(),3 个使用 ggplot2::ggplot() 工作并生成 6 个我可以在 RStudio 中导航的图使用“绘图” Pane /选项卡上的蓝色左右箭头按钮。
    base::plot(mpg, main="mpg 1 - base")
    ggplot2::ggplot(mpg, aes(displ, hwy, colour = class)) +
    geom_point() +
    ggtitle("mpg 1 - ggplot2")

    base::plot(ToothGrowth, main="ToothGrowth 2 - base")
    ggplot2::ggplot(ToothGrowth, aes(x=supp, y=len, fill=dose))+
    geom_boxplot() +
    ggtitle("ToothGrowth 2 - ggplot2")

    base::plot(iris, main="iris 3 - base")
    ggplot2::ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species))+
    geom_boxplot() +
    ggtitle("iris 3 - ggplot2")
    RStudio是:
    RStudio 2021.09.0+351 "Ghost Orchid" Release (077589bcad3467ae79f318afe8641a1899a51606, 2021-09-20) for macOS
    Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36
    Mac 操作系统版本:
    (base)  % uname -a
    Darwin xxxx.lan 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64

    (base) % sw_vers
    ProductName: macOS
    ProductVersion: 11.5.2
    BuildVersion: 20G95

    最佳答案

    我认为原因很简单:您没有输出真实的情节(静态)。DataExplorer::plot_intro(mtcars)是有问题的“情节”,其底层代码包括 diagonalNetwork在函数结束时:

    DataExplorer
    function (data, type = c("diagonal", "radial"), max_level = NULL,
    print_network = TRUE, ...)
    {
    i <- idx <- parent <- NULL
    str_output_raw <- capture.output(str(data, vec.len = 0, give.attr = FALSE,
    give.length = FALSE, list.len = 2000000000L))
    str_output <- unlist(lapply(str_output_raw, function(x) {
    gsub(" \\.{2}\\@", "\\$\\@", x)
    }))
    n <- length(str_output)
    base_split <- tstrsplit(str_output[2:n], "\\$")
    nest_level <- (nchar(base_split[[1]]) - nchar(gsub(" \\.{2}",
    "", base_split[[1]])))/3 + 1
    diff_nl <- diff(nest_level)
    s4_start_index <- which(diff_nl > 1L) + 1L
    if (length(s4_start_index) > 0) {
    s4_end_index <- which(diff_nl == -2L)
    s4_index_range <- unique(unlist(lapply(s4_start_index,
    function(i) {
    seq.int(i, s4_end_index[which.min(abs(s4_end_index -
    i))])
    })))
    nest_level[s4_index_range] <- nest_level[s4_index_range] -
    1L
    }
    if (is.null(max_level)) {
    max_level <- max(nest_level)
    }
    else if (max_level <= 0 | max_level > max(nest_level)) {
    stop(paste0("max_level should be between 1 and ",
    max(nest_level)))
    }
    else {
    max_level <- max_level
    }
    comp_split <- tstrsplit(base_split[[2]], ":")
    comp_root <- gsub(" ", "", comp_split[[1]])
    comp_root[which(comp_root == "")] <- make.names(comp_root[which(comp_root ==
    "")], unique = TRUE)
    if (anyDuplicated(comp_root))
    comp_root[which(duplicated(comp_root))] <- make.names(comp_root[which(duplicated(comp_root))],
    unique = TRUE)
    comp_output <- paste0(comp_root, " (", trimws(gsub("NULL|\\.{3}|\\.{2}",
    "", comp_split[[2]])), ")")
    str_dt <- data.table(idx = seq_along(nest_level), nest_level,
    parent = comp_output)[nest_level <= max_level]
    str_dt <- str_dt[str_dt[, list(i = idx, nest_level = nest_level -
    1, child = parent)], on = list(nest_level, idx < i),
    mult = "last"]
    drop_columns(str_dt[is.na(parent), `:=`(parent, paste0("root (",
    str_output[1], ")"))], c("idx", "nest_level"))
    str_to_list <- function(str_dt, root_name = as.character(str_dt[["parent"]][1])) {
    str_list <- list(name = root_name)
    children <- str_dt[parent == root_name][["child"]]
    if (length(children) > 0) {
    str_list[["children"]] <- lapply(children,
    str_to_list, str_dt = str_dt)
    }
    str_list
    }
    str_list <- str_to_list(str_dt)
    if (print_network) {
    type <- match.arg(type)
    if (type == "diagonal")
    print(diagonalNetwork(str_list, ...))
    if (type == "radial")
    print(radialNetwork(str_list, ...))
    }
    invisible(str_list)
    }
    哪里 diagonalNetwork用途 htmlwidgets :
    function (List, height = NULL, width = NULL, fontSize = 10, fontFamily = "serif", 
    linkColour = "#ccc", nodeColour = "#fff", nodeStroke = "steelblue",
    textColour = "#111", opacity = 0.9, margin = NULL)
    {
    if (!is.list(List))
    stop("List must be a list object.")
    root <- List
    margin <- margin_handler(margin)
    options = list(height = height, width = width, fontSize = fontSize,
    fontFamily = fontFamily, linkColour = linkColour, nodeColour = nodeColour,
    nodeStroke = nodeStroke, textColour = textColour, margin = margin,
    opacity = opacity)
    htmlwidgets::createWidget(name = "diagonalNetwork",
    x = list(root = root, options = options), width = width,
    height = height, htmlwidgets::sizingPolicy(padding = 10,
    browser.fill = TRUE), package = "networkD3")
    }
    htmlwidgets是一个交互式“情节”,并在 RStudio 的 viewer 下打开 Pane 而不是静态 plots Pane 。

    关于r - 当 DataExplorer::plot_str() 调用时,其他图消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69387284/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com