根据窗口大小动态调整闪亮绘图输出的高度和/或宽度 [英] dynamically adjust height and/or width of shiny-plotly output based on window size
问题描述
我希望将闪亮的输出高度和宽度调整为当前窗口大小。
ShinyUi< - fluidPage(
#应用程序标题
titlePanel(title),
sidebarLayout(
sidebarPanel(
...输入...
),
mainPanel(
plotlyOutput(distPlot,height ='auto',width ='auto')
)
))
ShinyServer< ; - 函数(输入,输出,会话){
输出$ distPlot< - renderPlotly({
p< - ggplot(dataShow,aes(x = dataShow $ X ,y = dataShow $ Y))+
geom_point(shape = 1,alpha = 0.5,color =grey50)
ggplotly(p)
} )
}
#运行应用程序
shinyApp(ui = ShinyUi,server = ShinyServer)
您是否可以在服务器功能中使用其他选项,而不是上面的UI功能?
小窗口:
展开的窗口: 你的问题,但符合我的意见,你可以使用来自
<现在当你把窗口变得更小时,你仍然会得到一个占据整个屏幕的图(没有滚动条!),如下所示:
I would like to have the shiny-plotly output height and width adjusted to the current window size. I have tried to use the below but of no use.
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)
Would you know of any other options to use maybe in server function instead of the above UI function usage?
Smaller Window:
Expanded Window:
It does not answer your question but in line to my comments you can add the plot height and width to the ggplotly
function using the js from this link.
I have prepared a minimal example of what you are want.
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)
The output you get is as follows:
Now when you make the window even smaller you still get a plot which occupies the whole screen (no scrollbars!) as follows:
这篇关于根据窗口大小动态调整闪亮绘图输出的高度和/或宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!