Search code examples
rplotly

Rstudio -Error: Client error: (401) Unauthorized No such user, can't open files.credentials, ./config etc


When i run the R code in Rstudio, I get below error. what's wrong?'

Error: Client error: (401) Unauthorized     No such user In addition: Warning messages: 1: In open.connection(con, "rb") :   cannot open file '/Users/i073341/.plotly/.credentials': No such file or directory 2: In open.connection(con, "rb") :   cannot open file '/Users/i073341/.plotly/.config': No such file or directory 3: You need a plotly username. See help(signup, package = 'plotly')  4: Couldn't find username  5: In open.connection(con, "rb") :   cannot open file '/Users/i073341/.plotly/.credentials': No such file or directory 6: In open.connection(con, "rb") :   cannot open file '/Users/i073341/.plotly/.config': No such file or directory 7: You need an api_key. See help(signup, package = 'plotly')  8: Couldn't find api_key

R code:

library("plotly")
a = read.csv('cleanSankey.csv', header=TRUE, sep=',')
node_names <- unique(c(as.character(a$source), as.character(a$target)))
nodes <- data.frame(name = node_names)
links <- data.frame(source = match(a$source, node_names) - 1,
                    target = match(a$target, node_names) - 1,
                    value = a$value)



#Plot
p <- plot_ly(type='sankey',
        orientation = "h",
        
        node = list(
          label = node_names,
          color = "grey",
          pad = 15,
          thinkness = 20,
          line = list(color = "grey", width = 0.5)),
 
         link = list(
           source = links$source,
           target = links$target,
           value = links$value))

plotly_IMAGE(p, out_file="out.png")       

If I remove last row code plotly_IMAGE(p, out_file="out.png"), there is no error.


Solution

  • plotly_IMAGE is used to export graphs as static images using plotly chart studio. You need an api_key for this to work. See this or help(signup, package = 'plotly').

    If you want to export static images on your local PC you can use plotly::save_image(p, "plot.png").

    For save_image to work please consider the following:

    kaleido() requires the kaleido python package to be usable via the reticulate package. Here is a recommended way to do the installation:

    install.packages('reticulate') reticulate::install_miniconda() reticulate::conda_install('r-reticulate', 'python-kaleido') reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly') reticulate::use_miniconda('r-reticulate')


    As an alternative you could use

    htmlwidgets::saveWidget(partial_bundle(p), file = "plot.HTML", selfcontained = TRUE)

    to save your chart as a standalone HTML file (e.g. as done here)