0

I am building a simple app using R shiny and am struggling to display multiple plots as one object. I tried grid.arrange(), ggpubr and multiplot without success. Here is my code for grid.arrange(). I am trying to display oneplot on the shiny interface. I have no trouble displaying it in the R studio console.

## Load libraries
packages<-c('shiny','dplyr','DT','data.table','datasets','shinydashboard',
            'ggplot2','tidyr',"stringr", "withr","treemap",
            "shinyBS", 'shinyjs', 'WDI', "magrittr",'shinycssloaders',
            'timevis','gridExtra','reshape2','grid','plotly')
for (package in packages) {
  if (!require(package, character.only=T, quietly=T)) {
    install.packages(package,repos="http://cran.us.r-project.org")
    library(package, character.only=T)
  }
}

data(PlantGrowth)
data1<-filter(PlantGrowth, group=='ctrl')
data2<-filter(PlantGrowth, group=='trt1')
data3<-filter(PlantGrowth, group=='trt2')

p1<-ggplot(data1) + geom_histogram(aes(x=weight),binwidth = 0.5)
p2<-ggplot(data2) + geom_histogram(aes(x=weight),binwidth = 0.5)
p3<-ggplot(data3) + geom_histogram(aes(x=weight),binwidth = 0.5)
oneplot<-grid.arrange(p1,p2,p3,nrow=2)
oneplot


server = function(input, output,session) {
  output$qaqc_dist_plot2<-renderPlot(oneplot)

}
#header -----
header <- dashboardHeader(title = "Database",
                          # Set height of dashboardHeader
                          tags$li(class = "dropdown",
                                  tags$style(HTML("@import url('//fonts.googleapis.com/css?family=Lobster|Cabin:400,700');
                                  .main-header {max-height: 400px}
                                  .main-header .title {max-height: 400px}
                                  .main-header .logo { height:auto;
                                                       background-color: #FFF;
                                                       font-family: 'Lobster', 'Times New Roman',serif;
                                                      font-color: 'white';
                                                      font-weight: bold;
                                                      font-size: 24px;
                                                      }")),
                          ),
                          disable = FALSE, titleWidth  = '250')

header$children[[3]]$children[[3]]<-tags$h1(" Database", align='left',
                                            style= "color:#FFF; font-family: 'Lobster', 'Times New Roman',serif;")
header$children[[2]]$children[[1]]<-tags$a(href='www.website.com', 
                                           tags$img(src='logo.png',title="app",alt="toxkin_app", 
                                                    style = "{margin-top:-50px;
                                               padding-bottom:0px;padding-top:0px;
                                               margin-left:-30px;
                                               margin-right:-30px;
                                               }",
                                                    height='200',width='200',
                                                    align="left"
                                           ))

## 2. siderbar ------------------------------
siderbar <- dashboardSidebar( width = 250,
                              sidebarMenu(id = 'sidebar',
                                          style = "position: relative; overflow: visible;",
                                          menuItem( "Dashboard", tabName = 'dashboard', icon = icon('dashboard'),
                                                    badgeColor = "teal" ),
                                          menuItem( "Summary Analysis", tabName = 'summary', icon = icon('file-alt'),
                                                    badgeColor = "teal" ),
                                          menuItem( "Detailed Endpoint Summary", tabName = "endpoints", icon = icon('table')),
                                          menuItem( "Data Analysis", tabName = 'analysis', icon = icon('chart-bar') )
                                          
                              )
)

## 3. body --------------------------------

body <- dashboardBody(tabItems(
  tabItem (tabName = "dashboard",
           tags$h2("Overview"),
           #uiOutput('qaqc_dist_plot')
           ), 
  tabItem (tabName = "summary",
           tags$h2("Summary Statistics")), 
  tabItem (tabName="endpoints",
           tags$h2("Detailed Summary")),
  tabItem (tabName="analysis",
           tags$h2("Visualizing  Data"),
           fluidRow(
             box(width=12, status='info', solidHeader = TRUE, 
                 title= 'QAQC', 
                 plotOutput('qaqc_dist_plot2')
             )
           ))))


## put UI together --------------------
ui <- dashboardPage(title = 'App',skin='blue',
                    header, siderbar, body 
)
shiny::shinyApp(ui=ui, server=server) # APP LAUNCH

Thank you!

  • Hi, you should reduce the size of your example, in order to have a [minimal, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, not all of these packages should be used, or you can simplify the UI – bretauv Jun 25 '20 at 19:59

1 Answers1

0

I don't know why using grid.arrange doesn't work, but you could use the package patchwork instead.

The layout you want is shown here. Here's the full example:

## Load libraries
packages<-c('shiny', 'shinydashboard',
            'ggplot2', 'dplyr', "patchwork")
for (package in packages) {
  if (!require(package, character.only=T, quietly=T)) {
    install.packages(package,repos="http://cran.us.r-project.org")
    library(package, character.only=T)
  }
}

data(PlantGrowth)
data1<-filter(PlantGrowth, group=='ctrl')
data2<-filter(PlantGrowth, group=='trt1')
data3<-filter(PlantGrowth, group=='trt2')

p1<-ggplot(data1) + geom_histogram(aes(x=weight),binwidth = 0.5)
p2<-ggplot(data2) + geom_histogram(aes(x=weight),binwidth = 0.5)
p3<-ggplot(data3) + geom_histogram(aes(x=weight),binwidth = 0.5)

server = function(input, output,session) {
  output$qaqc_dist_plot2<-renderPlot({
    p1 + p2 + p3 + plot_layout(ncol = 2)
  })
  
}
#header -----
header <- dashboardHeader(title = "Database")

## 2. siderbar ------------------------------
siderbar <- dashboardSidebar(width = 250,
                             sidebarMenu(
                               id = 'sidebar',
                               menuItem("Data Analysis", tabName = 'analysis')
                               
                             ))

## 3. body --------------------------------

body <- dashboardBody(tabItems(
  tabItem (tabName="analysis",
           tags$h2("Visualizing  Data"),
           fluidRow(
             box(width=12, status='info', solidHeader = TRUE, 
                 title= 'QAQC', 
                 plotOutput('qaqc_dist_plot2')
             )
           ))))


## put UI together --------------------
ui <- dashboardPage(title = 'App',skin='blue',
                    header, siderbar, body 
)
shiny::shinyApp(ui=ui, server=server) # APP LAUNCH
bretauv
  • 7,756
  • 2
  • 20
  • 57