2017-06-02 39 views
4

我希望将闪亮绘图的输出高度和宽度调整为当前窗口大小。我试图使用下面但没用。根据窗口大小动态调整闪亮绘图输出的高度和/或宽度

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

+0

你已经在使用'fluidPage()'? – BigDataScientist

+0

@BigDataScientist请查看更新后的文章中包含的代码结构。 – Prradep

+0

当你说'闪亮阴谋输出高度和宽度调整到当前窗口大小'时,你是什么意思?你想让它占据一定比例的屏幕尺寸吗? – SBista

回答

6

它不回答你的问题,但是在网上我的意见,你可以在情节的高度和宽度使用JS从this添加到ggplotly功能链接。

我准备了一个你想要的最简单的例子。

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

+0

是的,谢谢。虽然它没有回答我的问题,但它提供了实现解决方案的有用方法。 (如果在接下来的几天内没有其他直接的答案,我会接受这是一个(解决方法)答案。) – Prradep

相关问题