I am building a Shiny app based on an HTML template and I would like to use plotly for charts. I am struggling with insertion the chart into the template.
The following code works fine with renderPlotly:
library(shiny)
library(plotly)
shinyApp(
ui <- fluidPage(plotlyOutput("plot1")),
server <- function(input, output) {
p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers')
output$plot1 <- renderPlotly({p})
}
)
But when I change the app to use a template I cannot do it neither by using renderPlotly nor renderUI.
<!doctype html>
<html lang="en">
<head>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="w3.css">
</head>
<body>
<h1>HTML Template UI</h1>
<div id="plot1" class="shiny-plot-output" style="width: 100%; height: 300px; border: 1px solid red"></div>
<div id="plot2" class="shiny-html-output" style="width: 100%; height: 300px; border: 1px solid blue"></div>
</body>
</html>
app.R
library(shiny)
library(plotly)
shinyApp(
ui <- htmlTemplate("template.html"),
server <- function(input, output) {
p <- plot_ly(mtcars, x = ~mpg, y = ~wt)
output$plot1 <- renderPlotly({p})
output$plot2 <- renderUI(HTML(paste(htmltools::tagList(list(p)))))
}
)
Is there any easy way to use plotly in a Shiny app based on a HTML template without html-widgets?