gpt4 book ai didi

r - 根据窗口大小动态调整 Shiny 图输出的高度和/或宽度

转载 作者:行者123 更新时间:2023-12-02 04:24:40 27 4
gpt4 key购买 nike

我希望将 Shiny 的绘图输出高度和宽度调整为当前窗口大小。我尝试过使用下面的但没有用。

ShinyUi <- fluidPage(

# Application title
titlePanel("title"),

sidebarLayout(
sidebarPanel(
... inputs ...
),

mainPanel(
plotlyOutput("distPlot", height = 'auto', width = 'auto')
)
))

ShinyServer <- function(input, output, session) {

output$distPlot <- renderPlotly({

p <- ggplot(dataShow, aes(x=dataShow$X, y=dataShow$Y)) +
geom_point(shape=1, alpha = 0.5, color = "grey50")

ggplotly(p)

})

}


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

您是否知道在服务器功能中可以使用任何其他选项来代替上述 UI 功能用法?

较小的窗口: enter image description here

扩展窗口:enter image description here

最佳答案

它没有回答您的问题,但根据我的评论,您可以将绘图高度和宽度添加到 ggplotly使用 this 中的 js 函数关联。

我准备了一个您想要的最小示例。

library(shiny)
library(plotly)

ShinyUi <- fluidPage(
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);
});
')),

plotlyOutput("distPlot", width = "auto")

)

ShinyServer <- function(input, output, session) {


#To make the responsive to the change in UI size
observeEvent(input$dimension,{

output$distPlot <- renderPlotly({

p <- ggplot(iris, aes(x = Sepal.Length, y=Sepal.Width)) +
geom_point(shape=1, alpha = 0.5, color = "grey50")
ggplotly(p, width = (0.95*as.numeric(input$dimension[1])), height = as.numeric(input$dimension[2]))

})

})

}


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

您得到的输出如下: enter image description here

现在,当您将窗口缩小时,您仍然会得到一个占据整个屏幕的图(没有滚动条!),如下所示: enter image description here

关于r - 根据窗口大小动态调整 Shiny 图输出的高度和/或宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44324783/

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