2016-12-13 136 views
1

我有依賴於用戶的輸入的曲線圖。 根據輸入,繪圖大小將有所不同。閃亮plotOutput動態特性

我可以動態控制繪圖的高度? 我知道,在我plotOutput()的高度的說法,但我不能找到一種方法來動態地改變它。

重複的例子,當你選擇A,情節看起來不錯,但如果你選b,它更以高

library(shiny) 
library(ggplot2) 

df1 <- data.frame(x = 1:2000, y = rnorm(2000), type = rep(LETTERS[1:8], 250)) 
df2 <- data.frame(x = 1:100, y = rexp (100), type = rep(c('A','B'), 50)) 

ui <- shinyUI(fluidPage(title = '', 
    fluidRow(selectInput("table",'', choices = c('A','B'))), 
    fluidRow(plotOutput("my_plot", height = '1000px')) 
) 
) 

server <- shinyServer(function(input, output) { 
    output$my_plot <- renderPlot({ 
    t <- if(input$table == 'A') df1 
    else df2 
    ggplot(t) + facet_grid(type~.) + 
     geom_point(mapping = aes(x=x, y=y)) 
    } 
) 
}) 
shinyApp(ui, server) 

最後一兩件事,在實際應用這並不是說我有2個不同尺寸,這取決於尺寸需要改變的輸入。

回答

1

要做到你需要什麼,你需要使用服務器端呈現。 UI不知道情節有什麼以及如何動態調整任何內容。它只需要服務器生成的內容並將其彈出到屏幕上。

下面是一段代碼,它(我想你需要什麼)。順便說一句 - 我也把'數據'部分放入它自己的反應函數中。您可以進一步修改我的代碼以使像素高度'計算'與硬編碼等相關。

library(shiny) 
library(ggplot2) 

df1 <- data.frame(x = 1:2000, y = rnorm(2000), type = rep(LETTERS[1:8], 250)) 
df2 <- data.frame(x = 1:100, y = rexp (100), type = rep(c('A','B'), 50)) 

ui <- shinyUI(fluidPage(title = '', 
         fluidRow(selectInput("table",'', choices = c('A','B'))), 
         fluidRow(uiOutput('myPlotUI')) 
) 
) 

server <- shinyServer(function(input, output) { 
    myData <- reactive({ 
    if (input$table == 'A') 
     df1 
    else 
     df2 
    }) 
    myPlot <- reactive({ 
    output$myPlot <- renderPlot({ 
     ggplot(myData()) + facet_grid(type~.) + 
     geom_point(mapping = aes(x=x, y=y)) 
    }) 
    if (input$table == 'A') { 
     plotOutput('myPlot', height = '1000px') 
    } else { 
     plotOutput('myPlot', height = '250px') 
    } 
    }) 
    output$myPlotUI <- renderUI({ 
    myPlot() 
    }) 
}) 
shinyApp(ui, server) 
+0

非常感謝您! 我不知道我可以在服務器中呈現。 完美:) –