javascript - 在 Shiny 的可滚动框中自动调整绘图大小

标签 javascript html css shiny

我正在尝试在一个 Shiny 的应用程序中将一个情节放在一个盒子里。情节的大小发生变化,所以目前我正在定义盒子的高度,这不是解决方案。我想要一个垂直滚动的绘图,它自动调整其宽度和框高度等于窗口的高度。

这是我试过的:

library(shiny)
library(shinydashboard)

ui <-dashboardPage(
  dashboardHeader(),
  dashboardSidebar(sidebarMenu(
    menuItem("Box Plot", tabName = "boxplot")
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "boxplot",
                     fluidPage(fluidRow(column(4),
                                        column(8, box(title = "Box Plot", width = NULL,
                                                     solidHeader = TRUE, status = "primary", collapsible = TRUE,
                                                     plotOutput("plot1", inline=F, width="100%", height=1500), style = 'overflow-y: scroll;')
                                               )))))))

server <- shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    x <- data.frame(matrix(rnorm(1000), ncol = 20))
    input_data <- rnorm(ncol(x))
    d <- data.frame(x)
    plot.data <- gather(d, variable, value)
    plot.data$test_data <- as.numeric(rep(input_data, each = nrow(x)))

    p = ggplot(plot.data, aes(x = 0,  y=value)) + 
      geom_boxplot()  + 
      geom_point(aes(x = 0, y = test_data), color = "red") + 
      facet_wrap(~variable, scales = "free_y", switch = "y", nrow = 1) +
      xlab("") + ylab("") +
      theme(legend.position="none") + theme_bw() + theme(axis.text.x=element_blank(),
                                                         axis.text.y=element_text(angle=90),
                                                         axis.ticks.x=element_blank())

    print(p, vp=viewport(angle=270,  width = unit(3, "npc"), height = unit(0.32, "npc")))
  })
})

shinyApp(ui = ui, server = server)

最佳答案

这个在我的机器上看起来不错。

library(shiny)
library(shinydashboard)
library(tidyr)

ui <-dashboardPage(
  dashboardHeader(),
  dashboardSidebar(sidebarMenu(
    menuItem("Box Plot", tabName = "boxplot")
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "boxplot",
                     fluidPage(fluidRow(column(4),
                                        column(8, box(title = "Box Plot", width = NULL,
                                                      solidHeader = TRUE, status = "primary", collapsible = TRUE,
                                                      plotOutput("plot1", inline=F, width="100%", height=1500), style = 'display:block;width:100%;height:85vh;overflow-y: scroll;')
                                        )))))))

server <- shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    x <- data.frame(matrix(rnorm(1000), ncol = 20))
    input_data <- rnorm(ncol(x))
    d <- data.frame(x)
    plot.data <- gather(d, variable, value)
    plot.data$test_data <- as.numeric(rep(input_data, each = nrow(x)))

    p = ggplot(plot.data, aes(x = 0,  y=value)) + 
      geom_boxplot()  + 
      geom_point(aes(x = 0, y = test_data), color = "red") + 
      facet_wrap(~variable, scales = "free_y", switch = "y", nrow = 1) +
      xlab("") + ylab("") +
      theme(legend.position="none") + theme_bw() + theme(axis.text.x=element_blank(),
                                                         axis.text.y=element_text(angle=90),
                                                         axis.ticks.x=element_blank())

    print(p, vp=viewport(angle=270,  width = unit(3, "npc"), height = unit(0.32, "npc")))
  })
})

shinyApp(ui = ui, server = server)
  • 我创建了一个只有 CSS 的解决方案,视口(viewport)高度为 85% according to this post .
  • 由于您的流动网格列,此处不可能有 100% 的宽度。
  • 如上所述,您可以考虑使用 JavaScript/jQuery 进行高级调整,尤其是对于较旧的浏览器。
  • 另外,我在这里添加了必要的 tidyr 包。

enter image description here

关于javascript - 在 Shiny 的可滚动框中自动调整绘图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39873893/

相关文章:

javascript - 如果我们点击页面中的任何地方,值就会更新

javascript - 循环链表未按预期工作(jquery)

javascript - SVG 获取文本元素宽度

javascript - 有没有办法获取所有带有 :hover class in jQuery? 的元素

html - CSS:剪刀式图像布局

html - 由于 css 位置错误,无法显示下拉菜单栏

css - 在 VStudio MVC 5 应用程序的 html 页面中引用 css 不起作用

javascript - Jquery unwrap() 方法?

javascript - 如何在单击按钮 Angularjs 时获取表行值

css - 通过 vaadin 行