简体   繁体   English

如何在 R 的 Shiny 中调整我的 sidebarPanel 的大小?

[英]How to adjust the size of my sidebarPanel in Shiny in R?

I just have no idea on how to adjust my sidebarPanel , can anyone help me?我只是不知道如何调整我的sidebarPanel ,有人可以帮助我吗? This is my code:这是我的代码:

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)

What should I add in my ui so that my sidebarPanel can become wider or fit the entire screen?我应该在我的ui中添加什么,以便我的sidebarPanel可以变得更宽或适合整个屏幕?

Because it looked too small already.因为它看起来已经太小了。

And is there any recommendation to design the layout too?还有设计布局的建议吗?

Thank you very much.非常感谢。

If you remove the height and width args from your plotOutput, it will allow the the column to behave responsively to the width of the web page.如果从 plotOutput 中删除高度和宽度参数,它将允许列响应 web 页面的宽度。 So if you set column(width = 12, ...) it should fit the entire screen.因此,如果您设置 column(width = 12, ...) 它应该适合整个屏幕。 Same for your "Brushed Points" column (if desired)与您的“拉丝点”列相同(如果需要)

For example, this will make both elements fit the entire page:例如,这将使两个元素都适合整个页面:

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)

What might add to your UI is to center your UI elements.可能会添加到您的 UI 中的是使您的 UI 元素居中。 You can do this by simply adding empty columns before your UI elements and let shiny handle the screen responsiveness (instead of custom CSS):您可以通过简单地在 UI 元素之前添加空列并让 shiny 处理屏幕响应(而不是自定义 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM