2017-06-02 104 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

相關問題