我想确定Shiny浏览器窗口的大小,以帮助我更好地布局我的情节div.具体来说,我想确定窗口的宽高比,看看我应该在屏幕上分布多少div,它看起来还不错.我最初的想法是情节的数量floor(width/(height-navbar_height))
.
我做了一些寻找这个,我目前无法找到一个可能的解决方案,目前我认为这个功能根本不存在于clientData结构中.有什么想法吗?
Xio*_*Jin 30
请参阅下面的示例.它使用Javascript来检测浏览器窗口大小(初始大小和任何调整大小),并用于Shiny.onInputChange
将数据发送到服务器代码进行处理.它使用shiny:connected
事件来获取初始窗口大小,因为Shiny.onInputChange
在连接闪亮之前尚未准备好使用.
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)
Run Code Online (Sandbox Code Playgroud)
自 2021 年以来,出现了一种更简单的新方法:使用 { shinybrowser } 包。例子:
library(shiny)
ui <- fluidPage(
shinybrowser::detect(),
"Window size:",
textOutput("size")
)
server <- function(input, output, session) {
output$size <- renderText({
paste(
shinybrowser::get_width(),
"x",
shinybrowser::get_height()
)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
请注意,{shinybrowser} 目前仅在 GitHub 上,尚未在 CRAN 上(应该在不久的将来)。另请注意,{shinybrowser} 仅提供初始尺寸,但如果浏览器调整大小,则不会更新。
归档时间: |
|
查看次数: |
5866 次 |
最近记录: |