same code for function plot_ly in R gives differen

2019-08-18 18:59发布

问题:

I'm launching a simple function inside a project in RStudio for R3.4.0 for Windows 10.

#' TestPlotly
#' @export
#' @importFrom plotly plot_ly
#' @importFrom graphics layout
#' @importFrom magrittr "%>%"

TestPlotly=function()
{

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

}

I get:

 Error in layout(., xaxis = list(title = "tr"), yaxis = list(title = "ts")) : 
  unused arguments (xaxis = list(title = "tr"), yaxis = list(title = "ts")) 
  9.
  function_list[[k]](value) 
  8.
  withVisible(function_list[[k]](value)) 
  7.
  freduce(value, `_function_list`) 
  6.
  `_fseq`(`_lhs`) 
  5.
  eval(quote(`_fseq`(`_lhs`)), env, env) 
  4.
  eval(quote(`_fseq`(`_lhs`)), env, env) 
  3.
  withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
  2.
  plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines") %>% 
      layout(xaxis = list(title = "tr"), yaxis = list(title = "ts")) at             TestPlotly.R#11
  1.
  TestPlotly::TestPlotly()

But when I launch under RGui:

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

Everything works fine

NB: this time I get a different result under the package, ie no graphics at all, where as the last time I tried, I got graphics without titles on the axis. This why I am asking a similar question this time The question was: R plotly plot_ly function: axis titles don't appear when I call plotly inside a project

回答1:

I got the answer from Carson Sievert in person on the forum of plotly: I've solved my problem by replacing layout() with plotly::layout() When something works on Rgui but not in a package it might be the solution. I think that what got me was that I was using the layout from graphics instead of the layout from plotly because when I looked on google the only layout I found was the layout from graphics.