获取窗口的闪亮大小 [英] Get the size of the window in Shiny

查看:10
本文介绍了获取窗口的闪亮大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定SHILINY中浏览器窗口的大小,以帮助我更好地布局我的绘图div。具体地说,我想确定窗口的纵横比,看看应该在屏幕上铺开多少个div,它看起来还是不错的。我最初的想法是,地块的数量应该是floor(width/(height-navbar_height))

我为此做了一些工作,但我目前找不到可能的解决方案,并且目前认为该功能根本不存在于clientData结构中。有什么想法吗?

推荐答案

参见下面的示例。它使用Javascript检测浏览器窗口大小(初始大小和任何调整大小),并使用Shiny.onInputChange将数据发送到服务器代码进行处理。它使用shiny:connected事件获取初始窗口大小,因为Shiny.onInputChange在连接SHINY之前尚未准备好使用。

library(shiny)

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         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);
                                });
                            ')),
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         verbatimTextOutput("dimension_display"),
         plotOutput("distPlot")
      )
   )
))

# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
   output$dimension_display <- renderText({
       paste(input$dimension[1], input$dimension[2], input$dimension[2]/input$dimension[1])
   })

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
})

# Run the application 
shinyApp(ui = ui, server = server)

这篇关于获取窗口的闪亮大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆