【问题标题】:How to adjust the size of my sidebarPanel in Shiny in R?如何在 R 中的 Shiny 中调整我的侧边栏面板的大小?
【发布时间】:2020-07-24 21:05:01
【问题描述】:

我只是不知道如何调整我的sidebarPanel,有人可以帮我吗? 这是我的代码:

library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux
library(shiny)

mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]

ui <- fluidPage(
  fluidRow(
    column(width = 4, class = "well",
           h4("Brush and double-click to zoom"),
           plotOutput("plot1", height = 300, width = 500,
                      click = "plot1_click",
                      dblclick = "plot1_dblclick",
                      brush = brushOpts(
                        id = "plot1_brush",
                        resetOnNew = TRUE)))),

  fluidRow(
    column(width = 6,
           h4("Brushed points"),
           verbatimTextOutput("brush_info"))))

server <- function(input, output) {

  # -------------------------------------------------------------------
  # Single zoomable plot (on left)
  ranges <- reactiveValues(x = NULL, y = NULL)


  output$plot1 <- renderPlot({
    ggplot(mtcars2, aes(wt, mpg)) + geom_point() +
      coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)})

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observeEvent(input$plot1_dblclick, {
    brush <- input$plot1_brush
    if (!is.null(brush)) {
      ranges$x <- c(brush$xmin, brush$xmax)
      ranges$y <- c(brush$ymin, brush$ymax)} 

    else {
      ranges$x <- NULL
      ranges$y <- NULL
    }})

  output$brush_info <- renderPrint({
    brushedPoints(mtcars2, input$plot1_brush)})
  }

shinyApp(ui, server)

我应该在我的ui 中添加什么,以便我的sidebarPanel 可以变得更宽或适合整个屏幕?

因为它看起来已经太小了。

还有什么设计布局的建议吗?

非常感谢。

【问题讨论】:

    标签: r user-interface shiny shinyapps


    【解决方案1】:

    如果您从 plotOutput 中删除高度和宽度参数,它将允许列响应网页的宽度。因此,如果您设置 column(width = 12, ...) 它应该适合整个屏幕。您的“拉丝点”列也是如此(如果需要)

    例如,这将使两个元素都适合整个页面:

    library(ggplot2)
    library(shiny)
    
    mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]
    
    ui <- fluidPage(
      fluidRow(
        column(width = 12, class = "well",
               h4("Brush and double-click to zoom"),
               plotOutput("plot1",
                          click = "plot1_click",
                          dblclick = "plot1_dblclick",
                          brush = brushOpts(
                            id = "plot1_brush",
                            resetOnNew = TRUE)))),
    
      fluidRow(
        column(width = 12,
               h4("Brushed points"),
               verbatimTextOutput("brush_info"))))
    
    server <- function(input, output) {
    
      # -------------------------------------------------------------------
      # Single zoomable plot (on left)
      ranges <- reactiveValues(x = NULL, y = NULL)
    
    
      output$plot1 <- renderPlot({
        ggplot(mtcars2, aes(wt, mpg)) + geom_point() +
          coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)})
    
      # When a double-click happens, check if there's a brush on the plot.
      # If so, zoom to the brush bounds; if not, reset the zoom.
      observeEvent(input$plot1_dblclick, {
        brush <- input$plot1_brush
        if (!is.null(brush)) {
          ranges$x <- c(brush$xmin, brush$xmax)
          ranges$y <- c(brush$ymin, brush$ymax)} 
    
        else {
          ranges$x <- NULL
          ranges$y <- NULL
        }})
    
      output$brush_info <- renderPrint({
        brushedPoints(mtcars2, input$plot1_brush)})
    }
    
    shinyApp(ui, server)
    

    可能会添加到您的 UI 中的是使您的 UI 元素居中。您可以通过在 UI 元素之前添加空列并让闪亮处理屏幕响应(而不是自定义 CSS)来做到这一点:

    library(ggplot2)
    library(shiny)
    
    mtcars2 <- mtcars[, c("mpg", "cyl", "disp", "hp", "wt", "am", "gear")]
    
    ui <- fluidPage(
      fluidRow(
        column(width=3),
        column(width = 6, class = "well",
               h4("Brush and double-click to zoom"),
               plotOutput("plot1",
                          click = "plot1_click",
                          dblclick = "plot1_dblclick",
                          brush = brushOpts(
                            id = "plot1_brush",
                            resetOnNew = TRUE)))),
    
      fluidRow(
        column(width=3),
        column(width = 6,
               h4("Brushed points"),
               verbatimTextOutput("brush_info"))))
    
    server <- function(input, output) {
    
      # -------------------------------------------------------------------
      # Single zoomable plot (on left)
      ranges <- reactiveValues(x = NULL, y = NULL)
    
    
      output$plot1 <- renderPlot({
        ggplot(mtcars2, aes(wt, mpg)) + geom_point() +
          coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = FALSE)})
    
      # When a double-click happens, check if there's a brush on the plot.
      # If so, zoom to the brush bounds; if not, reset the zoom.
      observeEvent(input$plot1_dblclick, {
        brush <- input$plot1_brush
        if (!is.null(brush)) {
          ranges$x <- c(brush$xmin, brush$xmax)
          ranges$y <- c(brush$ymin, brush$ymax)} 
    
        else {
          ranges$x <- NULL
          ranges$y <- NULL
        }})
    
      output$brush_info <- renderPrint({
        brushedPoints(mtcars2, input$plot1_brush)})
    }
    
    shinyApp(ui, server) 
    

    【讨论】:

    • 哦,如果你想保持高度,宽度参数,只需将绘图嵌入另一列。所以 column(width=6, ..., column(12, plotOutput("plot1", width="", height="")). 整个框的大小将取决于外列,因此大小仍然应该是什么你想要
    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2020-04-02
    • 2019-03-03
    相关资源
    最近更新 更多