gpt4 book ai didi

r - Shiny 的仪表板不能很好地扩展

转载 作者:行者123 更新时间:2023-12-01 12:28:52 25 4
gpt4 key购买 nike

第二个例子来自 http://rstudio.github.io/shinydashboard/get_started.html问题是对于某些类型的渲染,缩放效果不好。

打开的仪表板:
enter image description here

仪表板关闭:
enter image description here

仪表板关闭并打开控制台(这次它按照从一开始就应该做的那样缩放绘图)enter image description here

仪表板关闭/打开时是否可以重新渲染绘图?

最佳答案

您可以在单击仪表板打开/关闭按钮时在窗口上强制调整大小事件,方法是使用 jQuery 将函数绑定(bind)到按钮,如下所示:

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)

关于r - Shiny 的仪表板不能很好地扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com