Is there a way to export raw data in the form of excel in plotly graph. Example
asd <- data.frame(week = c(1,2,3), a = c(12,41,33), b = c(43,21,23), c = c(43,65,43), d = c(33,45,83))
plot_ly(asd, x = ~week, y = ~`a`, name = 'a', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~`b`, name = 'b', mode = 'lines') %>%
layout(xaxis = list(title = paste0("Week"),showgrid = F,rangemode = "normal"),
yaxis = list(title = "",showgrid = F,rangemode = "normal"),
hovermode = 'x unified')
We can down the static image in png. But we cannot down load the raw data behind the graph. Can we download it? So basically, data in asd
should be downloaded
Too get an XLS/XLSX file, one would need a library. Here is a way to export to a CSV file. This only exports the visible data.
library(plotly)
asd <- data.frame(
week = c(1, 2, 3),
a = c(12, 41, 33),
b = c(43, 21, 23),
c = c(43, 65, 43),
d = c(33, 45, 83)
)
js <- c(
'function(gd) {',
' var text = "";',
' for (var i = 0; i < gd.data.length; i++) {',
' var data = gd.data[i];',
' text +=',
' gd.layout.xaxis.title.text + data.name + "," + data.x + "\\n";',
' text +=',
' gd.layout.yaxis.title.text + data.name + "," + data.y + "\\n";',
' }',
' var blob = new Blob([text], { type: "text/plain" });',
' var a = document.createElement("a");',
' const object_URL = URL.createObjectURL(blob);',
' a.href = object_URL;',
' a.download = "data.csv";',
' document.body.appendChild(a);',
' a.click();',
' URL.revokeObjectURL(object_URL);',
'}'
)
CSV_SVGpath <- "M284.1,150.5V31H108.3c-11.7,0-21.1,9.4-21.1,21.1v407.8c0,11.7,9.4,21.1,21.1,21.1h295.3c11.7,0,21.1-9.4,21.1-21.1V171.6 H305.2C293.6,171.6,284.1,162.1,284.1,150.5z M199.8,277.1c0,3.9-3.1,7-7,7h-7c-7.8,0-14.1,6.3-14.1,14.1v28.1 c0,7.8,6.3,14.1,14.1,14.1h7c3.9,0,7,3.1,7,7v14.1c0,3.9-3.1,7-7,7h-7c-23.3,0-42.2-18.9-42.2-42.2v-28.1 c0-23.3,18.9-42.2,42.2-42.2h7c3.9,0,7,3.1,7,7V277.1z M238.7,368.5h-10.8c-3.9,0-7-3.1-7-7v-14.1c0-3.9,3.1-7,7-7h10.8 c5.2,0,9.1-3.1,9.1-5.8c0-1.1-0.7-2.3-1.9-3.4l-19.2-16.5c-7.4-6.3-11.7-15.4-11.7-24.7c0-18.7,16.7-33.9,37.3-33.9H263 c3.9,0,7,3.1,7,7v14.1c0,3.9-3.1,7-7,7h-10.8c-5.2,0-9.1,3.1-9.1,5.8c0,1.1,0.7,2.3,1.9,3.4l19.2,16.5c7.4,6.3,11.7,15.4,11.7,24.7 C275.9,353.3,259.2,368.5,238.7,368.5L238.7,368.5z M312.2,263v18.3c0,17.8,5,35.3,14.1,50c9.1-14.7,14.1-32.2,14.1-50V263 c0-3.9,3.1-7,7-7h14.1c3.9,0,7,3.1,7,7v18.3c0,31.2-11.3,60.5-31.9,82.7c-2.7,2.9-6.4,4.5-10.3,4.5s-7.6-1.6-10.3-4.5 c-20.6-22.1-31.9-51.5-31.9-82.7V263c0-3.9,3.1-7,7-7h14.1C309.1,256,312.2,259.1,312.2,263z M418.6,123.3l-86-86.1 c-4-4-9.3-6.2-14.9-6.2h-5.4v112.5h112.5v-5.4C424.8,132.6,422.6,127.2,418.6,123.3z"
CSVexport <- list(
name = "CSV",
icon = list(
path = CSV_SVGpath,
width = 512,
height = 512
),
click = htmlwidgets::JS(js)
)
plot_ly(
asd, x = ~week, y = ~`a`, name = "a", type = "scatter", mode = "lines"
) %>%
add_trace(y = ~`b`, name = "b", mode = "lines") %>%
layout(
xaxis = list(title = "Week", showgrid = FALSE, rangemode = "normal"),
yaxis = list(title = "", showgrid = FALSE, rangemode = "normal"),
hovermode = "x unified"
) %>%
config(modeBarButtonsToAdd = list(CSVexport))
You can get the data used in the graph by using plotly_data
function.
myplot <- plot_ly(asd, x = ~week, y = ~`a`, name = 'a', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~`b`, name = 'b', mode = 'lines') %>%
layout(xaxis = list(title = paste0("Week"),showgrid = F,rangemode = "normal"),
yaxis = list(title = "",showgrid = F,rangemode = "normal"),
hovermode = 'x unified')
plotly_data(myplot)
# A tibble: 3 x 5
# week a b c d
# <dbl> <dbl> <dbl> <dbl> <dbl>
#1 1 12 43 43 33
#2 2 41 21 65 45
#3 3 33 23 43 83
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.