I have created a shiny
app to show a correlation heatmap for a large dataset. When the heatmap tiles are pressed, the corresponding scatterplots are displayed.
However, I need to make several of these apps and this exceeds my limit of publishing on shinyapps.io
. My company is unwilling to upgrade to a paying plan. I have tried using alternative methods to publish the app such as RInno
, to no avail (I think the app is too complex?).
If someone could please tell me how I could produce the same with plotly
alone and NOT with shiny
, I would be forever grateful. I believe something like crosstalk
might be the path to take in order to link the heat map tiles to the scatter plots?
Thank you
Example from here.
library(plotly)
library(shiny)
# compute a correlation matrix
correlation <- round(cor(mtcars), 3)
nms <- names(mtcars)
ui <- fluidPage(
mainPanel(
plotlyOutput("heat"),
plotlyOutput("scatterplot")
),
verbatimTextOutput("selection")
)
server <- function(input, output, session) {
output$heat <- renderPlotly({
plot_ly(x = nms, y = nms, z = correlation,
key = correlation, type = "heatmap", source = "heatplot") %>%
layout(xaxis = list(title = ""),
yaxis = list(title = ""))
})
output$selection <- renderPrint({
s <- event_data("plotly_click")
if (length(s) == 0) {
"Click on a cell in the heatmap to display a scatterplot"
} else {
cat("You selected: \n\n")
as.list(s)
}
})
output$scatterplot <- renderPlotly({
s <- event_data("plotly_click", source = "heatplot")
if (length(s)) {
vars <- c(s[["x"]], s[["y"]])
d <- setNames(mtcars[vars], c("x", "y"))
yhat <- fitted(lm(y ~ x, data = d))
plot_ly(d, x = ~x) %>%
add_markers(y = ~y) %>%
add_lines(y = ~yhat) %>%
layout(xaxis = list(title = s[["x"]]),
yaxis = list(title = s[["y"]]),
showlegend = FALSE)
} else {
plotly_empty()
}
})
}
shinyApp(ui, server)
The event_data() function is the most straight-forward way to access a plotly input events in shiny. Although event_data() is function, it references and returns a shiny input value, so event_data() needs to be used inside a reactive context.
As of version 0.12. 0, Shiny has built-in support for interacting with static plots generated by R's base graphics functions, and those generated by ggplot2.
With ggplotly() by Plotly, you can convert your ggplot2 figures into interactive ones powered by plotly. js, ready for embedding into Dash applications. ggplotly is free and open source and you can view the source, report issues or contribute on GitHub.
The best answer would likely be to use crosstalk
in conjunction with flexdashboard
https://rmarkdown.rstudio.com/flexdashboard/.
A live example of using both be found here: http://rstudio-pubs-static.s3.amazonaws.com/209203_02f14fea3274448bbbf8d04c99c6051b.html.
The end result is just an html page which is easy to share and use. Based on your example you shouldn't need shiny and it you should be able to use crosstalk for this use case. You'd need to step outside of R to get that functionality otherwise. Best of luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With