【问题标题】:rBokeh dynamic plot sizerBokeh 动态绘图大小
【发布时间】:2017-07-05 11:53:11
【问题描述】:

我正在尝试在我的 Shiny 应用程序中创建一个 rBokeh 图作为输出,在 UI 中使用 rbokehOutput('plot')

output$plot <- renderRbokeh({ 
figure() %>%
      ly_hexbin(x,y)
    })

在服务器部分。我希望绘图的大小在某种意义上是动态的,它应该动态调整大小以填充整个绘图窗口。我一直在使用 heightwidth 参数,在 ui 和服务器部分,但无法使其工作;我还尝试在服务器部分使用sizing_mode = "stretch_both"。当我在没有 Shiny 的 RStudio 中显示绘图时,绘图也不会填满整个绘图窗口,它保持其方形和纵横比。我希望它表现得像一个普通的 R 绘图,即当我放大绘图窗口时,绘图会自动调整大小以填充整个窗口。我找到了this link,但它只处理 Python 实现。理想情况下,我希望 Shiny 中的高度固定为 500 像素,宽度根据浏览器的(大小)动态变化。

最小的工作示例:

library(shiny)
library(rbokeh)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput('numpoint', 'Number of points to plot', min = 10, max = 1000, value = 200)
    ),
    mainPanel(
      rbokehOutput('plot', width = "98%", height = "500px")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderRbokeh({
    x <- seq(1, 100, length = input$numpoint)
    y <- rnorm(input$numpoint, sd = 5)

    figure() %>%
      ly_hexbin(x,y)
  })
}

shinyApp(ui, server)

更新的 MWE:

library(shiny)
library(rbokeh)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput('numpoint', 'Number of points to plot', min = 10, max = 1000, value = 200)
    ),
    mainPanel(
      fluidRow(
        column(12,
               rbokehOutput('plot', height = "800px")
               )
        ),
      fluidRow(
        column(12,
               plotOutput('plot1', height = "800px")
               )
      )

    )
  )
)

server <- function(input, output) {
  output$plot <- renderRbokeh({
    x <- seq(1, 100, length = input$numpoint)
    y <- rnorm(input$numpoint, sd = 5)

    figure(width = 1800, height = 800) %>%
      ly_hexbin(x,y)
  })

  output$plot1 <- renderPlot({
    x <- seq(1, 100, length = input$numpoint)
    y <- rnorm(input$numpoint, sd = 5)

    plot(x,y)
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r plot shiny bokeh rbokeh


    【解决方案1】:

    更新我的答案..

    我在对 figure() 的调用中添加了宽度和高度,现在绘图会响应地重新调整大小。

    library(shiny)
    library(rbokeh)
    
    ui <- fluidPage(
        titlePanel("Hello Shiny!"),
        sidebarLayout(
            sidebarPanel(
                sliderInput('numpoint', 'Number of points to plot', min = 10, max = 1000, value = 200)
        ),
        mainPanel(
            rbokehOutput('plot', width = "100%", height = "800px")
            )
        )
    )
    
    server <- function(input, output) {
        output$plot <- renderRbokeh({
            x <- seq(1, 100, length = input$numpoint)
            y <- rnorm(input$numpoint, sd = 5)
    
            figure(width = 1800, height = 800) %>%
                ly_hexbin(x,y)
        })
    }
    
    shinyApp(ui, server)
    

    这是你想要的吗?

    【讨论】:

    • 并非如此。查看我更新的 MWE 并比较缩小窗口宽度(但可能保持高度)时两个图如何调整大小。基本图形保持在恒定的 800 像素高度,但 rBokeh 图形不是。此外,当您在对 figure() 的调用中指定 width = 800 时,绘图中的 hexbin 会被拉伸(与动态调整大小的基本图形点不同)。
    • @Skumin 我知道这已经很老了,但我也遇到了同样的问题......你找到解决方案了吗?
    • @NBE 我没有。最终我只是切换到ggplot2,我想不通。
    • @NBE 检查我的解决方案
    【解决方案2】:

    在你的 ui body 里面,你可以添加如下代码:

    # This will dynamically set the width of the rBokeh plots
    tags$head(tags$script('var dimension = [0, 0];
                              $(document).on("shiny:connected", function(e) {
                                  dimension[0] = window.innerWidth;
                                  dimension[1] = window.innerHeight;
                                  Shiny.onInputChange("dimension", dimension);
                              });
                              $(window).resize(function(e) {
                                  dimension[0] = window.innerWidth;
                                  dimension[1] = window.innerHeight;
                                  Shiny.onInputChange("dimension", dimension);
                              });
                              '))
    

    然后你可以使用动态大小作为输入对象,比如

    figure(width = input$dimension[1], height = input$dimension[2],
    

    【讨论】:

      猜你喜欢
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多