首页 > 解决方案 > 不使用 Shiny 的链接图

问题描述

我创建了一个shiny应用程序来显示大型数据集的相关热图。按下热图图块时,将显示相应的散点图。

但是,我需要制作其中几个应用程序,这超出了我在shinyapps.io. 我的公司不愿意升级到付费计划。我曾尝试使用替代方法来发布应用程序,例如RInno, 无济于事(我认为应用程序太复杂了?)。

如果有人能告诉我如何用plotly单独而不是用来制作相同的东西shiny,我将永远感激不尽。我相信类似的东西crosstalk可能是将热图图块链接到散点图的路径?

谢谢

示例来自这里

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)

在此处输入图像描述

标签: rshinyplotlyinteractiveggplotly

解决方案


最好的答案可能是crosstalkflexdashboard https://rmarkdown.rstudio.com/flexdashboard/结合使用。

在这里可以找到同时使用这两种方法的实时示例:http ://rstudio-pubs-static.s3.amazonaws.com/209203_02f14fea3274448bbbf8d04c99c6051b.html 。

最终结果只是一个易于共享和使用的 html 页面。根据您的示例,您不需要闪亮,并且您应该能够在此用例中使用串扰。否则,您需要跳出 R 才能获得该功能。祝你好运!


推荐阅读