簡體   English   中英

R plotly plot_ly函數:當我在項目內部調用plotly時,軸標題不出現

[英]R plotly plot_ly function: axis titles don't appear when I call plotly inside a project

我正在使用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)
}

在此處輸入圖片說明

注意:我在有人建議的這段代碼中遇到了同樣的問題:

#' 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()

在此處輸入圖片說明

讓我們回到打包表格。 當我做的時候

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>     

這是使用繪圖布局實現所需功能的正確形式:

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

 }

希望能幫助到你!

我在plotly論壇上親自獲得了Carson Sievert的回答:我已經通過用plotly :: layout()替換layout()解決了我的問題,我想我能從中使用的是圖形布局而不是圖形布局從布局的布局,因為當我看谷歌時,我發現的唯一布局是圖形的布局。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM