简体   繁体   English

rBokeh 动态绘图大小

[英]rBokeh dynamic plot size

I am trying to create an rBokeh plot as an output in my Shiny app with rbokehOutput('plot') in the ui and我正在尝试在我的 Shiny 应用程序中创建一个 rBokeh 图作为输出,在 ui 中使用rbokehOutput('plot')

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

in the server part.在服务器部分。 I'd like the size of the plot to be dynamic in a sense that it should dynamically resize to fill the entire plot window.我希望绘图的大小在某种意义上是动态的,它应该动态调整大小以填充整个绘图窗口。 I've been playing with the height and width arguments, both in the ui and server parts, but have not been able to make it work;我一直在 ui 和服务器部分中使用heightwidth参数,但无法使其工作; I also tried using sizing_mode = "stretch_both" in the server part.我还尝试在服务器部分使用sizing_mode = "stretch_both" When I display the plot in RStudio without Shiny, the plot does not fill the entire plot window either, it keeps its square shape and aspect ratio.当我在没有 Shiny 的 RStudio 中显示绘图时,绘图也不会填满整个绘图窗口,而是保持其方形和纵横比。 I would like it to behave like a normal R plot, ie when I enlarge the plot window, the plot automatically resizes to fill the full window.我希望它表现得像一个正常的 R 绘图,即当我放大绘图窗口时,绘图会自动调整大小以填充整个窗口。 I found this link , but it only deals with Python implementation.我找到了这个链接,但它只涉及 Python 实现。 Ideally, I would like the height in Shiny to be fixed at 500px and the width to change dynamically based on the (size of) browser.理想情况下,我希望 Shiny 的高度固定为 500px,宽度根据浏览器的(大小)动态变化。

Minimal working example:最小工作示例:

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)

Updated MWE:更新的 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)

Updating my answer..更新我的答案..

I've added width and height into my call to figure() and the plot now re-sizes responsively.我已经在我对 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)

Is this what you wanted?这是你想要的吗?

Inside of your ui body, you can add the following code:在您的 ui 主体内部,您可以添加以下代码:

# 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);
                          });
                          '))

Then you can use the dynamic size as an input oject like然后你可以使用动态大小作为输入对象

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM