2016-04-15 10 views
3

Ich nahm das zweite Beispiel von http://rstudio.github.io/shinydashboard/get_started.html und das Problem ist, dass für einige Arten von Rendering die Skalierung nicht gut ist.Shiny Armaturenbrett nicht gut skalieren

Armaturenbrett geöffnet: enter image description here

Armaturenbrett geschlossen: enter image description here

Armaturenbrett geschlossene und offene Konsole (diesmal ist es die Handlung skaliert, wie es von begining getan haben sollte) enter image description here

Ist es möglich, um das Grundstück neu zu rendern, wenn das Armaturenbrett geschlossen/geöffnet ist?

Antwort

4

Sie können eine Grßenänderungsereignis Kraft auf das Fenster, wenn das Armaturenbrett Öffnen/Schließen-Schaltfläche mithilfe von jQuery geklickt wird, eine Funktion der Taste wie folgt binden:

library(shinydashboard) 

ui <- dashboardPage(

    dashboardHeader(title = "Basic dashboard"), 
    dashboardSidebar(), 
    dashboardBody(
    tags$script(' 
     // Bind function to the toggle sidebar button 
     $(".sidebar-toggle").on("click",function(){ 
     $(window).trigger("resize"); // Trigger resize event 
     })' 
    ), 

    # Boxes need to be put in a row (or column) 
    fluidRow(
     box(plotOutput("plot1", height = 250)), 

     box(
     title = "Controls", 
     sliderInput("slider", "Number of observations:", 1, 100, 50) 
    ) 
    ) 
) 
) 

server <- function(input, output, session) { 
    set.seed(122) 
    histdata <- rnorm(500) 

    output$plot1 <- renderPlot({ 
    data <- histdata[seq_len(input$slider)] 
    hist(data) 
    }) 
} 

shinyApp(ui, server) 

Wenn Sie nicht wollen, zu zwingen Ein re-size Event für alle Elemente Sie können das plotOutput mit den Funktionen shiny :: uiOutput und shiny :: renderUI jedes Mal neu erstellen, wenn die Sidebar umgeschaltet wird.

library(shinydashboard) 

ui <- dashboardPage(

    dashboardHeader(title = "Basic dashboard"), 
    dashboardSidebar(), 
    dashboardBody(
    tags$script(' 
     // Bind function to the toggle sidebar button 
     $(".sidebar-toggle").on("click",function(){ 
     // Send value to Shiny 
     Shiny.onInputChange("toggleClicked", Math.random()); 
     })' 
    ), 

    # Boxes need to be put in a row (or column) 
    fluidRow(
     #box(plotOutput("plot1", height = 250)), 
     box(uiOutput('plotUi')), 

     box(
     title = "Controls", 
     sliderInput("slider", "Number of observations:", 1, 100, 50) 
    ) 
    ) 
) 
) 

server <- function(input, output, session) { 
    # Helper function to create the needed ui elements 
    updateUI <- function(){ 
    output$plotUi <- renderUI({ 
     plotOutput("plot1", height = 250) 
    }) 
    } 

    # Plot data to plotOutput 
    updatePlot <- function(){ 
    output$plot1 <- renderPlot({ 
     hist(data()) 
    }) 
    } 

    set.seed(122) 
    histdata <- rnorm(500) 

    # Initialize UI and create plotOutput 
    updateUI() 
    updatePlot() 

    # Create a reactive dataset 
    data <- eventReactive(input$slider,{ 
    histdata[seq_len(input$slider)] 
    }) 

    # This is triggered when the toggle dashbord button is clicked 
    # this is achived by the javascript binding in the ui part 
    observeEvent(input$toggleClicked,{ 
    updateUI() 
    updatePlot() 
    }) 
} 

shinyApp(ui, server)