【问题标题】:shiny dashboard fix width of plots闪亮的仪表板修复地块的宽度
【发布时间】:2017-08-27 11:29:19
【问题描述】:

我已指定列宽为5 的图。 我的问题是我的图显示的列宽更接近2,图之间有空白填充。

这是我的问题的 MWE

library(tidyverse)
library(shiny)
library(shinydashboard)

##----------DATA------------##
set.seed(1)
df <- map(1:4, ~data.frame(x=1:10, y=(1:10)+runif(.x), z=.x))
stat <- data.frame(A=runif(4)+2, B=runif(4)+2, depth=c(10,20,30,40))
##----------END DATA------------##

## UI
ui <- dashboardPage(
            dashboardHeader(title = "Test"),
            dashboardSidebar(
                  sidebarMenu(
                        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
                  )
            ),
            dashboardBody(
                  tabItems(
                        # First tab content
                        tabItem(tabName = "dashboard",
                              fluidRow( 
                                    box(title = "Inputs",
                                         solidHeader = TRUE,
                                         collapsible = TRUE,
                                         width = 3,    
                                     selectInput(inputId="parameter", label="Parameter", choices=c("This", "That"), selected=c("This"))
                                    )
                              ), 
                              fluidRow(
                                    column(width=5,
                                         box(title = "Plot",
                                              solidHeader = TRUE,
                                              collapsible = TRUE,
                                              plotOutput(outputId="histogram")
                                         )
                                    ),      
                            column(width=5,
                                         box(title = "Summary",
                                     plotOutput(outputId="linegraph")
                                         )
                                    )
                              )
                )
            ) # end tabitems
        ) # end dashboardbody
    ) # end dashboardpage

## SERVER
server <-   function( input, output ) {
                  # Reactive data
                  data <- reactive({ df })
                  stats <- reactive({ stat })

                  # Histogram plot
            output$histogram <- renderPlot({ ggplot() +
                                                      geom_step(data=data()[[1]], aes(x=x, y=y, colour="1"), lwd=1) + 
                                                      geom_step(data=data()[[2]], aes(x=x, y=y, colour="2"), lwd=1) +
                                                      geom_step(data=data()[[3]], aes(x=x, y=y, colour="3"), lwd=1) +
                                                      geom_step(data=data()[[4]], aes(x=x, y=y, colour="4"), lwd=1) +
                                                      scale_color_manual(values=c("1"="cyan","2"="blue","3"="green","4"="red")) +
                                                      theme_classic() +
                                                      guides(color=guide_legend(title="")) +
                                                      theme(legend.position = "bottom", legend.direction = "horizontal") + 
                                                      theme(text = element_text(size=20)) +
                                                      xlab("") + ylab("") })

                  # Linegraph plot
            output$linegraph <- renderPlot({ ggplot() +
                                                      geom_point(data=stats(), aes(x=depth, y=A, color="A"), size=5) + 
                                                      geom_line(data=stats(), aes(x=depth, y=A, color="A"), lwd=1) +
                                                      geom_point(data=stats(), aes(x=depth, y=B, color="B"), size=5) + 
                                                      geom_line(data=stats(), aes(x=depth, y=B, color="B"), lwd=1) +
                                                      geom_hline(yintercept=0, lty=2, lwd=1, color="red") +
                                                      scale_color_manual(values=c("A"="black","B"="grey")) +
                                                      theme_classic() +
                                                      guides(color=guide_legend(title="")) +
                                                      theme(legend.position = "bottom", legend.direction = "horizontal") + 
                                                      theme(text = element_text(size=20)) +
                                                      xlab("") + ylab("") })
        }

shinyApp( ui = ui, server = server )

感谢您的帮助!

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    box() 的默认宽度为 6(=总宽度的 1/2)。 但是,您的框位于宽度为 5 的列内。 所以你的盒子的总尺寸是 5*0.5=2.5。

    当您想获得列的全宽时,只需在框内设置width=12

    这是您的固定 MWE(并将 tidyverse 替换为 ggplotpurrr,因为我不想污染我的 R...):

    library(ggplot2)
    library(purrr)
    library(shiny)
    library(shinydashboard)
    
    ##----------DATA------------##
    set.seed(1)
    df <- map(1:4, ~data.frame(x=1:10, y=(1:10)+runif(.x), z=.x))
    stat <- data.frame(A=runif(4)+2, B=runif(4)+2, depth=c(10,20,30,40))
    ##----------END DATA------------##
    
    ## UI
    ui <- dashboardPage(
      dashboardHeader(title = "Test"),
      dashboardSidebar(
        sidebarMenu(
          menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
        )
      ),
      dashboardBody(
        tabItems(
          # First tab content
          tabItem(tabName = "dashboard",
                  fluidRow( 
                    box(title = "Inputs",
                        solidHeader = TRUE,
                        collapsible = TRUE,
                        width = 3,    
                        selectInput(inputId="parameter", label="Parameter", choices=c("This", "That"), selected=c("This"))
                    )
                  ), 
                  fluidRow(
                    column(width = 5,
                           box(title = "Plot",
                               solidHeader = TRUE,
                               collapsible = TRUE,
                               width = 12,
                               plotOutput(outputId="histogram")
                           )
                    ),      
                    column(width = 5,
                           box(title = "Summary",
                               width = 12,
                               plotOutput(outputId="linegraph")
                           )
                    )
                  )
          )
        ) # end tabitems
      ) # end dashboardbody
    ) # end dashboardpage
    
    ## SERVER
    server <-   function( input, output ) {
      # Reactive data
      data <- reactive({ df })
      stats <- reactive({ stat })
    
      # Histogram plot
      output$histogram <- renderPlot({ ggplot() +
          geom_step(data=data()[[1]], aes(x=x, y=y, colour="1"), lwd=1) + 
          geom_step(data=data()[[2]], aes(x=x, y=y, colour="2"), lwd=1) +
          geom_step(data=data()[[3]], aes(x=x, y=y, colour="3"), lwd=1) +
          geom_step(data=data()[[4]], aes(x=x, y=y, colour="4"), lwd=1) +
          scale_color_manual(values=c("1"="cyan","2"="blue","3"="green","4"="red")) +
          theme_classic() +
          guides(color=guide_legend(title="")) +
          theme(legend.position = "bottom", legend.direction = "horizontal") + 
          theme(text = element_text(size=20)) +
          xlab("") + ylab("") })
    
      # Linegraph plot
      output$linegraph <- renderPlot({ ggplot() +
          geom_point(data=stats(), aes(x=depth, y=A, color="A"), size=5) + 
          geom_line(data=stats(), aes(x=depth, y=A, color="A"), lwd=1) +
          geom_point(data=stats(), aes(x=depth, y=B, color="B"), size=5) + 
          geom_line(data=stats(), aes(x=depth, y=B, color="B"), lwd=1) +
          geom_hline(yintercept=0, lty=2, lwd=1, color="red") +
          scale_color_manual(values=c("A"="black","B"="grey")) +
          theme_classic() +
          guides(color=guide_legend(title="")) +
          theme(legend.position = "bottom", legend.direction = "horizontal") + 
          theme(text = element_text(size=20)) +
          xlab("") + ylab("") })
    }
    
    shinyApp( ui = ui, server = server )
    

    【讨论】:

    • 啊,有道理。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 2015-04-22
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多