R plotly plot_ly函数:当我在项目内部调用plotly时,轴标题不出现

时间:2018-11-12 16:37:10

标签: r rstudio plotly axis r-plotly

我正在使用R3.4.0,并且正在Windows 10的RStudio下以项目的形式开发一个程序包。 我不明白为什么轴的标题没有出现。我什至没有错误消息。 版本1:

#' TestPlotly
#' @export
#' @importFrom plotly plot_ly

TestPlotly=function()
{
  p<-plot_ly(x = 1:10, y = 1:10,xaxis=list(title="tr"),yaxis=list(title="ts"),type="scatter",mode="lines")
  print(p)
}

enter image description here

注意:我在有人建议的这段代码中遇到了同样的问题:

#' TestPlotly
#' @export
#' @importFrom plotly plot_ly

TestPlotly=function()
{
  plot_ly(x = 1:10, y = 1:10,type="scatter",mode="lines") %>%
    layout(
     xaxis = list(title="tr"),
     yaxis = list(title="ts")
   )
}

重要说明:当我不在项目之外时,它会起作用。为什么是这样? 但是我仍然需要在项目中使用它

> TestPlotly=function()
+ {
+     plot_ly(x = 1:10, y = 1:10,type="scatter",mode="lines") %>% 
+         layout(
+             xaxis = list(title="tr"),
+             yaxis = list(title="ts")
+         )
+     
+ }
> TestPlotly()

enter image description here

让我们回到包装表中。

debugonce(plot_ly)

,当我致电:

 Browse[2]> print(attrs)

,除其他外,我得到的似乎是正确的:

$xaxis
$xaxis$title
[1] "tr"


$yaxis
$yaxis$title
[1] "ts"

在plot_ly的调试中,我然后在该函数的末尾调试config,并且仍然可以得到前面的内容。我正在向您展示我在配置末尾获得的内容,对我来说似乎很正确,这很奇怪。

        Browse[3]> print(p$dependencies)
        [[1]]
        List of 10
         $ name      : chr "typedarray"
         $ version   : chr "0.1"
         $ src       :List of 1
          ..$ file: chr "C:/Program Files/R/R-3.4.0/library/plotly/htmlwidgets/lib/typedarray"
         $ meta      : NULL
         $ script    : chr "typedarray.min.js"
         $ stylesheet: NULL
         $ head      : NULL
         $ attachment: NULL
         $ package   : NULL
         $ all_files : logi FALSE
         - attr(*, "class")= chr "html_dependency"

        [[2]]
        List of 10
         $ name      : chr "jquery"
         $ version   : chr "1.11.3"
         $ src       :List of 1
          ..$ file: chr "C:/Program Files/R/R-3.4.0/library/crosstalk/lib/jquery"
         $ meta      : NULL
         $ script    : chr "jquery.min.js"
         $ stylesheet: NULL
         $ head      : NULL
         $ attachment: NULL
         $ package   : NULL
         $ all_files : logi TRUE
         - attr(*, "class")= chr "html_dependency"

        [[3]]
        List of 10
         $ name      : chr "crosstalk"
         $ version   : chr "1.0.0"
         $ src       :List of 1
          ..$ file: chr "C:/Program Files/R/R-3.4.0/library/crosstalk/www"
         $ meta      : NULL
         $ script    : chr "js/crosstalk.min.js"
         $ stylesheet: chr "css/crosstalk.css"
         $ head      : NULL
         $ attachment: NULL
         $ package   : NULL
         $ all_files : logi TRUE
         - attr(*, "class")= chr "html_dependency"

        [[4]]
        List of 10
         $ name      : chr "plotly-htmlwidgets-css"
         $ version   : chr "1.39.2"
         $ src       :List of 1
          ..$ file: chr "C:/Program Files/R/R-3.4.0/library/plotly/htmlwidgets/lib/plotlyjs"
         $ meta      : NULL
         $ script    : NULL
         $ stylesheet: chr "plotly-htmlwidgets.css"
         $ head      : NULL
         $ attachment: NULL
         $ package   : NULL
         $ all_files : logi FALSE
         - attr(*, "class")= chr "html_dependency"

        [[5]]
        List of 10
         $ name      : chr "plotly-main"
         $ version   : chr "1.39.2"
         $ src       :List of 1
          ..$ file: chr "C:/Program Files/R/R-3.4.0/library/plotly/htmlwidgets/lib/plotlyjs"
         $ meta      : NULL
         $ script    : chr "plotly-latest.min.js"
         $ stylesheet: NULL
         $ head      : NULL
         $ attachment: NULL
         $ package   : NULL
         $ all_files : logi FALSE
         - attr(*, "class")= chr "html_dependency"

        Browse[3]> 
        debug: nms <- sapply(p$x$config[["modeBarButtonsToAdd"]], "[[", "name")
        Browse[3]> 
        debug: hasCollab <- sharingButton()[["name"]] %in% nms
        Browse[3]> 
        debug: if (collaborate && !hasCollab) {
            nAdd <- length(p$x$config[["modeBarButtonsToAdd"]])
            p$x$config[["modeBarButtonsToAdd"]][[nAdd + 1]] <- sharingButton()
        }
        Browse[3]> 
        debug: nAdd <- length(p$x$config[["modeBarButtonsToAdd"]])
        Browse[3]> 
        debug: p$x$config[["modeBarButtonsToAdd"]][[nAdd + 1]] <- sharingButton()
        Browse[3]> 
        debug: if (!collaborate) {
            p$x$config[["modeBarButtonsToAdd"]][nms %in% sharingButton()[["name"]]] <- NULL
        }
        Browse[3]> 
        debug: p$x$config$cloud <- cloud
        Browse[3]> 
        debug: p
        Browse[3]> print(x$config)
        Error in print(x$config) : object 'x' not found
        Browse[3]> print(p$x$config)
        $modeBarButtonsToAdd
        $modeBarButtonsToAdd[[1]]
        $modeBarButtonsToAdd[[1]]$name
        [1] "Collaborate"

        $modeBarButtonsToAdd[[1]]$icon
        $modeBarButtonsToAdd[[1]]$icon$width
        [1] 1000

        $modeBarButtonsToAdd[[1]]$icon$ascent
        [1] 500

        $modeBarButtonsToAdd[[1]]$icon$descent
        [1] -50

        $modeBarButtonsToAdd[[1]]$icon$path
        [1] "M487 375c7-10 9-23 5-36l-79-259c-3-12-11-23-22-31-11-8-22-12-35-12l-263 0c-15 0-29 5-43 15-13 10-23 23-28 37-5 13-5 25-1 37 0 0 0 3 1 7 1 5 1 8 1 11 0 2 0 4-1 6 0 3-1 5-1 6 1 2 2 4 3 6 1 2 2 4 4 6 2 3 4 5 5 7 5 7 9 16 13 26 4 10 7 19 9 26 0 2 0 5 0 9-1 4-1 6 0 8 0 2 2 5 4 8 3 3 5 5 5 7 4 6 8 15 12 26 4 11 7 19 7 26 1 1 0 4 0 9-1 4-1 7 0 8 1 2 3 5 6 8 4 4 6 6 6 7 4 5 8 13 13 24 4 11 7 20 7 28 1 1 0 4 0 7-1 3-1 6-1 7 0 2 1 4 3 6 1 1 3 4 5 6 2 3 3 5 5 6 1 2 3 5 4 9 2 3 3 7 5 10 1 3 2 6 4 10 2 4 4 7 6 9 2 3 4 5 7 7 3 2 7 3 11 3 3 0 8 0 13-1l0-1c7 2 12 2 14 2l218 0c14 0 25-5 32-16 8-10 10-23 6-37l-79-259c-7-22-13-37-20-43-7-7-19-10-37-10l-248 0c-5 0-9-2-11-5-2-3-2-7 0-12 4-13 18-20 41-20l264 0c5 0 10 2 16 5 5 3 8 6 10 11l85 282c2 5 2 10 2 17 7-3 13-7 17-13z m-304 0c-1-3-1-5 0-7 1-1 3-2 6-2l174 0c2 0 4 1 7 2 2 2 4 4 5 7l6 18c0 3 0 5-1 7-1 1-3 2-6 2l-173 0c-3 0-5-1-8-2-2-2-4-4-4-7z m-24-73c-1-3-1-5 0-7 2-2 3-2 6-2l174 0c2 0 5 0 7 2 3 2 4 4 5 7l6 18c1 2 0 5-1 6-1 2-3 3-5 3l-174 0c-3 0-5-1-7-3-3-1-4-4-5-6z"


        $modeBarButtonsToAdd[[1]]$click
        [1] "function(gd) { \n        // is this being viewed in RStudio?\n        if (location.search == '?viewer_pane=1') {\n          alert('To learn about plotly for collaboration, visit:\\n https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html');\n        } else {\n          window.open('https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html', '_blank');\n        }\n      }"
        attr(,"class")
        [1] "JS_EVAL"



        $cloud
        [1] FALSE

        Browse[3]> print(p$x)
        $visdat
        $visdat$`35446c1114b9`
        function () 
        plotlyVisDat
        <environment: 0x0000000019eb2c60>


        $cur_data
        [1] "35446c1114b9"

        $attrs
        $attrs$`35446c1114b9`
        $attrs$`35446c1114b9`$x
         [1]  1  2  3  4  5  6  7  8  9 10

        $attrs$`35446c1114b9`$y
         [1]  1  2  3  4  5  6  7  8  9 10

        $attrs$`35446c1114b9`$xaxis
        $attrs$`35446c1114b9`$xaxis$title
        [1] "tr"


        $attrs$`35446c1114b9`$yaxis
        $attrs$`35446c1114b9`$yaxis$title
        [1] "ts"


        $attrs$`35446c1114b9`$mode
        [1] "lines"

        $attrs$`35446c1114b9`$alpha_stroke
        [1] 1

        $attrs$`35446c1114b9`$sizes
        [1]  10 100

        $attrs$`35446c1114b9`$spans
        [1]  1 20

        $attrs$`35446c1114b9`$type
        [1] "scatter"



        $layout
        $layout$width
        NULL

        $layout$height
        NULL

        $layout$margin
        $layout$margin$b
        [1] 40

        $layout$margin$l
        [1] 60

        $layout$margin$t
        [1] 25

        $layout$margin$r
        [1] 10



        $source
        [1] "A"

        $config
        $config$modeBarButtonsToAdd
        $config$modeBarButtonsToAdd[[1]]
        $config$modeBarButtonsToAdd[[1]]$name
        [1] "Collaborate"

        $config$modeBarButtonsToAdd[[1]]$icon
        $config$modeBarButtonsToAdd[[1]]$icon$width
        [1] 1000

        $config$modeBarButtonsToAdd[[1]]$icon$ascent
        [1] 500

        $config$modeBarButtonsToAdd[[1]]$icon$descent
        [1] -50

        $config$modeBarButtonsToAdd[[1]]$icon$path
        [1] "M487 375c7-10 9-23 5-36l-79-259c-3-12-11-23-22-31-11-8-22-12-35-12l-263 0c-15 0-29 5-43 15-13 10-23 23-28 37-5 13-5 25-1 37 0 0 0 3 1 7 1 5 1 8 1 11 0 2 0 4-1 6 0 3-1 5-1 6 1 2 2 4 3 6 1 2 2 4 4 6 2 3 4 5 5 7 5 7 9 16 13 26 4 10 7 19 9 26 0 2 0 5 0 9-1 4-1 6 0 8 0 2 2 5 4 8 3 3 5 5 5 7 4 6 8 15 12 26 4 11 7 19 7 26 1 1 0 4 0 9-1 4-1 7 0 8 1 2 3 5 6 8 4 4 6 6 6 7 4 5 8 13 13 24 4 11 7 20 7 28 1 1 0 4 0 7-1 3-1 6-1 7 0 2 1 4 3 6 1 1 3 4 5 6 2 3 3 5 5 6 1 2 3 5 4 9 2 3 3 7 5 10 1 3 2 6 4 10 2 4 4 7 6 9 2 3 4 5 7 7 3 2 7 3 11 3 3 0 8 0 13-1l0-1c7 2 12 2 14 2l218 0c14 0 25-5 32-16 8-10 10-23 6-37l-79-259c-7-22-13-37-20-43-7-7-19-10-37-10l-248 0c-5 0-9-2-11-5-2-3-2-7 0-12 4-13 18-20 41-20l264 0c5 0 10 2 16 5 5 3 8 6 10 11l85 282c2 5 2 10 2 17 7-3 13-7 17-13z m-304 0c-1-3-1-5 0-7 1-1 3-2 6-2l174 0c2 0 4 1 7 2 2 2 4 4 5 7l6 18c0 3 0 5-1 7-1 1-3 2-6 2l-173 0c-3 0-5-1-8-2-2-2-4-4-4-7z m-24-73c-1-3-1-5 0-7 2-2 3-2 6-2l174 0c2 0 5 0 7 2 3 2 4 4 5 7l6 18c1 2 0 5-1 6-1 2-3 3-5 3l-174 0c-3 0-5-1-7-3-3-1-4-4-5-6z"


        $config$modeBarButtonsToAdd[[1]]$click
        [1] "function(gd) { \n        // is this being viewed in RStudio?\n        if (location.search == '?viewer_pane=1') {\n          alert('To learn about plotly for collaboration, visit:\\n https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html');\n        } else {\n          window.open('https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html', '_blank');\n        }\n      }"
        attr(,"class")
        [1] "JS_EVAL"



        $config$cloud
        [1] FALSE


        attr(,"TOJSON_FUNC")
        function (x, ...) 
        {
            jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE, 
                null = "null", na = "null", ...)
        }
        <environment: namespace:plotly>

重新启动计算机后,问题仍然存在

我现在正在研究更深层次的内容,但似乎仍然没事:

debugonce(as_widget)

debugonce(htmlwidgets::createWidget)

    Browse[4]> print(x)
    $visdat
    $visdat$e004cb169ef
    function () 
    plotlyVisDat
    <environment: 0x0000000005873be0>


    $cur_data
    [1] "e004cb169ef"

    $attrs
    $attrs$e004cb169ef
    $attrs$e004cb169ef$x
     [1]  1  2  3  4  5  6  7  8  9 10

    $attrs$e004cb169ef$y
     [1]  1  2  3  4  5  6  7  8  9 10

    $attrs$e004cb169ef$xaxis
    $attrs$e004cb169ef$xaxis$title
    [1] "tr"


    $attrs$e004cb169ef$yaxis
    $attrs$e004cb169ef$yaxis$title
    [1] "ts"


    $attrs$e004cb169ef$mode
    [1] "lines"

    $attrs$e004cb169ef$alpha_stroke
    [1] 1

    $attrs$e004cb169ef$sizes
    [1]  10 100

    $attrs$e004cb169ef$spans
    [1]  1 20

    $attrs$e004cb169ef$type
    [1] "scatter"



    $layout
    $layout$width
    NULL

    $layout$height
    NULL

    $layout$margin
    $layout$margin$b
    [1] 40

    $layout$margin$l
    [1] 60

    $layout$margin$t
    [1] 25

    $layout$margin$r
    [1] 10



    $source
    [1] "A"

    attr(,"TOJSON_FUNC")
    function (x, ...) 
    {
        jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE, 
            null = "null", na = "null", ...)
    }
    <environment: namespace:plotly>     

2 个答案:

答案 0 :(得分:1)

这是使用绘图布局实现所需功能的正确形式:

TestPlotly=function()
{
   plot_ly(x = 1:10, y = 1:10,type="scatter",mode="lines") %>% 
    layout(
      xaxis = list(title="tr"),
      yaxis = list(title="ts")
      )

 }

希望有帮助!

答案 1 :(得分:1)

我在plotly论坛上亲自从Carson Sievert得到了答案: 我已经通过用plotly :: layout()替换layout()解决了我的问题 我认为让我感到困惑的是,我使用的是图形的布局,而不是使用plotly的布局,因为当我在Google上查看时,我发现的唯一布局就是图形的布局。