Как настроить размер моей боковой панели в Shiny в R? - PullRequest
0 голосов
/ 12 апреля 2020

Я просто понятия не имею, как настроить мой 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 мог стать шире или уместиться на весь экран?

Потому что он уже выглядел слишком маленьким.

И есть ли рекомендации по разработке макета тоже?

Большое спасибо.

1 Ответ

1 голос
/ 12 апреля 2020

Если вы удалите аргументы высоты и ширины из вашего plotOutput, это позволит столбцу вести себя в соответствии с шириной веб-страницы. Таким образом, если вы установите столбец (ширина = 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)

Что может добавить к вашему пользовательскому интерфейсу, так это центрировать ваш Элементы пользовательского интерфейса. Вы можете сделать это, просто добавив пустые столбцы перед вашими элементами пользовательского интерфейса и позволяя блестящему обрабатывать отзывчивость экрана (вместо пользовательских 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) 
...