1

I have created a table using plot_ly() and then converted it into.JSON and saved locally.

Now, I want to open the saved file in R to inspect the created tables, but i struggle how to open such a file?

This is how i have exported the files.

tab <- plot_ly(

# creates table

    ))

then i wrote it as .JSON:

tab <- plotly_json(tab, FALSE)
tabName <- paste0("summary.json" )
write(tab, paste0(directory, "/", tabName))

How can i import this file back to R?

Nneka
  • 1,764
  • 2
  • 15
  • 39

1 Answers1

1

You could e.g. use jsonlite::fromJSON. Here you can find other options.

Please check the follwing:

library(plotly)
library(datasets)
library(jsonlite)

tab <- plot_ly(
  type = 'table',
  columnwidth = c(100, 100),
  columnorder = c(0, 1),
  header = list(
    values = c("Cut","Price"),
    align = c("center", "center"),
    line = list(width = 1, color = 'black'),
    fill = list(color = c("grey", "grey")),
    font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(head(diamonds)$cut, head(diamonds)$price),
    align = c("center", "center"),
    line = list(color = "black", width = 1),
    font = list(family = "Arial", size = 12, color = c("black"))
  ))

tab_json <- plotly_json(tab, FALSE)
write(tab_json, "summary.json")

tab_data <- fromJSON("summary.json", flatten = TRUE)
tab_data$data$cells.values[[1]]
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78