Hi,
I have constructed below code from the previous questions asked in community.
I want to have a download option for the chart that I dynamically generate as HTMLWidgets.widget.
Unfortunately, I get a blank "chart.png" in the download.
Please help!
# ui.R
# --------------------------------------------------------
ui <- shinyUI(fluidPage(
title = 'Basic usage of D3Table in Shiny',
fluidRow(column(width = 12, downloadButton("downloadData", "Download"))),
fluidRow(column(width = 12, D3TableOutput('test'))
)
))
# server.R
# --------------------------------------------------------
server <- shinyServer(function(input, output, session) {
tbl <- readr::read_tsv("data.tsv")
table_1 <- D3Table(data = tbl);
gt <- renderD3Table({table_1})
output$test <- gt
output$downloadData <- downloadHandler(
filename = "chart.png",
content = function(file) {
saveWidget(table_1, "temp.html", selfcontained = TRUE)
webshot::webshot(url = "temp.html", file = file)
}
)
})
As per the suggestion I changed the code to following. The package is installed, but I am still getting a blank test.html as well as the chart.png file. Is there a way to check if the data in table_1 is correct?
server <- shinyServer(function(input, output, session) {
tbl <- readr::read_tsv("data.tsv")
table_1 <- D3Table(data = tbl);
gt <- renderD3Table({table_1})
output$test <- gt
install.packages("remotes")
remotes::install_github("rstudio/webshot2")
library(webshot2)
output$downloadData <- downloadHandler(
filename = "chart.png",
content = function(file) {
saveWidget(table_1, "temp.html", selfcontained = TRUE)
webshot2::webshot(url = "temp.html", file = file)
}
)
})
ok, lets start from the begining.
where are these functions coming from ? They are not listed on rdocumentation org, are they from a private repo ?
D3TableOutput()
D3Table()
renderD3Table()
D3GeneTable()
renderD3GeneTable()
Thanks for your help!
The last two functions are obsolete. I changed the code accordingly. The first three are as follows.
I create a D3.js lib based HTMLWidgets.widget(). There I have all the js etc. to create a custom rshiny widget using java script.
Hence, D3Table is a table widget, which calls htmlwidgets::createwidget(). The D3TableOutput calls htmlwidgets::shinyWidgetOutput and renderD3Table calls htmlwidgets::shinyRenderWidget functions.
In the end, the server.R gives me the required table output. The problem is in the download section where for some reason everything is blank.
HI, I am still not able to solve it. Does anyone has a suggestion ?
I think this is going to be hard for a fellow forum user to debug, as you are reporting issue with javascript codes that are private to yourself. Perhaps you could engage with htmlwidgets community i.e. issues. ? ramnathv/htmlwidgets: HTML Widgets for R (github.com)
but I expect that whoever you look to get support from would need access to a representative example of the widget you would wish to saveout.
Hi,
Just in case an example is easy to understand the issue. I am referring to shinyJsTutorials/tutorials/materials2/C3_demo at master · FrissAnalytics/shinyJsTutorials · GitHub</titl
Lets say that I want to give the user an option to download one of the several widgets as image that are built in server.R
I added a download button in ui.R as below
fluidRow(column(width = 12, downloadButton("downloadData", "Download"))),
and now in server.R i want to have something like below.
There is a widget called C3StackedAreaChart. I want to fill the place for widget_to_be_saved in saveWidget of downloadHandler.
output$stackedAreaChart <- renderC3StackedAreaChart({
dataset <- data.frame(Time = Data()$Time,
GREEN = Data()$Perc$GREEN,
AMBER = Data()$Perc$AMBER,
RED = Data()$Perc$RED)
colors <- list(GREEN = "#2CA02C", AMBER = "#FF7F0E", RED = "Red")
C3StackedAreaChart(dataset,colors)
})
output$downloadData <- downloadHandler(
filename = "chart.png",
content = function(file) {
saveWidget(widget_to_be_saved, "temp.html", selfcontained = TRUE)
webshot2::webshot(url = "temp.html", file = file)
}
)
Or please guide me where am I wrong in my understanding of how it should be done.
Thanks in advance.
in general the approach would be:
widget_to_be_saved <- reactive({
dataset <- data.frame(Time = Data()$Time,
GREEN = Data()$Perc$GREEN,
AMBER = Data()$Perc$AMBER,
RED = Data()$Perc$RED)
colors <- list(GREEN = "#2CA02C", AMBER = "#FF7F0E", RED = "Red")
C3StackedAreaChart(dataset,colors)
})
output$stackedAreaChart <- renderC3StackedAreaChart({
req(widget_to_be_saved())
})
output$downloadData <- downloadHandler(
filename = "chart.png",
content = function(file) {
fpt <- file.path(tempdir(),"temp.html")
saveWidget(req(widget_to_be_saved()),fpt, selfcontained = TRUE)
webshot2::webshot(url = fpt, file = file)
}
)
this assumes that C3StackedAreaChart works properly to create a htmlwidget
Dear Nirgrahamuk,
I figured out the problem due to which my plot was blank. It was due to a call to shiny as below inside the htmlwidget.
Shiny.addCustomMessageHandler("setNormalization",
function setNormalization (message) {}
I am trying to explore options to extract a part of html I want to download and handle it in javascript itself. I have no idea if it is actually possible.
Please guide me how to use the download handler with a widget using shiny.
Thanks in advance.