我从http://rstudio.github.io/shinydashboard/get_started.html获取了第二个例子,问题是对于某些类型的渲染,缩放不好。
仪表板关闭并打开控制台(这次它缩放了应该从开始时开始的情节)
当仪表板关闭/打开时,是否可以重新渲染绘图?
答案 0 :(得分:3)
通过使用jQuery将函数绑定到按钮,可以在单击仪表板打开/关闭按钮时在窗口上强制调整大小事件,如下所示:
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)
如果您不想在所有元素上强制重新调整大小事件,则可以在每次切换侧边栏时使用shiny :: uiOutput和shiny :: renderUI函数重新创建plotOutput。
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)