gpt4 book ai didi

R/ Shiny : Plotly reactive height discarded after window resize

转载 作者:行者123 更新时间:2023-12-04 11:58:07 25 4
gpt4 key购买 nike

从 Plotly 4.5.6 升级到 Plotly 4.6.0(或更高版本)后,在 plot_ly 中 react 性设置时会错误地计算图高度.这发生在调整窗口大小或选择 tabsetPanel 时。

先决条件

  • 通过 plot_ly(..., height=<some dependency>) 选择绘图高度
  • <some dependency>是一个 react 值(input$height 在下面的例子中)

  • 问题
  • 调整窗口大小后,高度设置为 selectInput
  • 的第一个值

    示例
  • 当在 selectInput 中选择“800”时,绘图正确绘制为 800px 的高度:

  • ex1
  • 调整窗口大小后,高度更改为 selectInput 的第一个值(100px),即使这个值根本没有改变:

  • ex2

    如果存在 tabsetPanel 也会发生同样的问题在页面中(例如在绘图下方)。单击不同的选项卡时,绘图高度会更改。
    library(shiny)
    library(plotly)


    ui <-shinyUI(fluidPage(
    selectInput("height", "Choose desired height", choices=c(100,800)),
    plotlyOutput("plot")))

    server <- shinyServer(function(input,output, session){
    output$plot <- renderPlotly({
    plot_ly(mtcars, x=~gear, y=~cyl, height = input$height)
    })
    })

    shinyApp(ui,server)

    到目前为止我尝试了什么
  • height参数在 renderPlotly最初有效,但由于 Plotly 本身从不改变其高度,因此绘图本身始终保持在 800px(只是容器高度发生了变化):

  • 用户界面: uiOutput("plot")
    服务器:
    output$plot <- renderUI({ plotlyOutput("plotly", height=input$height) })  
    output$plotly <- renderPlotly({ plot_ly(mtcars, x=~gear, y=~cyl)})
  • 当我另外制作output$plotly依赖 input$height ,在选择任何其他高度后,绘图立即“跳跃”:
  • output$plotly <- renderPlotly({ plot_ly(mtcars, x=~gear, y=~cyl, height=input$height) })
    请注意,如果有一个简单的 tabsetPanel,所有这些也会发生。在绘图下方,并在那里选择了另一个选项卡。

    最佳答案

    终于修复了,见this bug on github: .在 plotly 包中 版本 4.8.0 这种行为已得到纠正。

    请注意,您需要将 100% 的高度添加到 plotlyOutput,即 plotlyOutput("plot", height = '100%')对于完整的解决方案(这会适本地设置不可见的容器空间)。

    关于R/ Shiny : Plotly reactive height discarded after window resize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970064/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com