【问题标题】:Move plotlyOutput() to the server side in a shiny app在闪亮的应用程序中将 plotlyOutput() 移动到服务器端
【发布时间】:2019-11-13 08:10:56
【问题描述】:

我有一个闪亮的仪表板,我想在其中使用.js. 动态调整图形和框的高度问题是,只有当我在plotlyOutput() 中传递高度参数并给它input$GetScreenHeight 时才会发生这种情况包含高度。但我得到object 'input' not found。我可以只将plotlyOutput() 传递给里面的服务器吗?

jscode <-
  '$(document).on("shiny:connected", function(e) {
  var jsHeight = screen.height;
  Shiny.onInputChange("GetScreenHeight",jsHeight);
});
'

shinyApp(
  ui = dashboardPagePlus(
    header = dashboardHeaderPlus(
    ),
    sidebar = dashboardSidebar(),

    body = dashboardBody(
      tags$script(jscode),
      boxPlus(
        title = "Closable Box with dropdown", 
        closable = TRUE, 
        width = NULL,
        status = "warning", 
        solidHeader = FALSE, 
        collapsible = TRUE,
        plotlyOutput("pl",height = input$GetScreenHeight)
      )
    )

  ),
  server = function(input, output) {

    output$pl <- renderPlotly({
      x    <- faithful[, 2] 
      dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))

      p <- ggplot(dat,aes(x=xx)) +
        geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) 

      p <- ggplotly(p)
    })
  }
)

【问题讨论】:

  • 为了完整起见:renderUI 是如果您想将 plotlyOutput() 移动到服务器端的方法,就像 here 所做的那样。

标签: r ggplot2 shiny plotly ggplotly


【解决方案1】:

可以将高度设置为ggplotly,而不是在服务器中移动plotlyOutput

  ggplotly(p, height = input$GetScreenHeight)

然后在plotlyOutput中设置height = 100%

plotlyOutput("pl", height = "100%")

也许身高screen.height不是个好选择,太大了。我推荐:

jscode <-
  '$(document).on("shiny:connected", function(e) {
      var jsHeight = 0.4 * document.body.clientWidth; 
      Shiny.onInputChange("GetScreenHeight", jsHeight);
  });
'

【讨论】:

猜你喜欢
  • 2016-05-23
  • 2016-08-01
  • 2020-01-26
  • 1970-01-01
  • 2014-08-09
  • 2016-10-16
  • 2018-07-29
  • 2020-05-03
  • 2018-09-03
相关资源
最近更新 更多