1

I am trying to create multiple plots in the same shiny window. Not sure what I am doing wrong. It throws an error that something is up with my main panel.

I tried a few different things but nothing seems to work. Btw this is an adaptation of Mike's answer to this question How can put multiple plots side-by-side in shiny r?

library(ggplot2)
library(gridExtra)
library(shiny)

ui <- fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
                sidebarPanel("sidebar panel",
  checkboxInput("donum1", "plot1", value = T),
  checkboxInput("donum2", "plot2", value = F),
  checkboxInput("donum3", "plot3", value = F)
),
  mainPanel("main panel", outputplot="myPlot")))

server <- function(input, output, session) {
  
  Feature_A <- c(1, 2,1, 4,2)
  Feature_B <- c(4,5,6,6,6)
  Feature_C <- c(22,4,3,1,5)
  df<- data.frame(Feature_A ,Feature_B ,Feature_C)
  
  pt1 <- reactive({
    if (!input$donum1) return(NULL)
  p1<-ggplot(data=df, aes(Feature_A))+ geom_histogram()
  })
  pt2 <- reactive({
    if (!input$donum2) return(NULL)
  p2<-ggplot(data=df, aes(Feature_B))+ geom_histogram()
  })
  pt3 <- reactive({
    if (!input$donum3) return(NULL)
  p3<-ggplot(data=df, aes(Feature_C))+ geom_histogram()
  })
  
  output$myPlot = renderPlot({
    grid.arrange(p1, p2, p3, ncol=2,top="Main Title")
  })
  
}

shinyApp(ui, server)

EDIT:

Thanks to YBS's comment below, I have been able to figure out the above issue and have made the change to my code above. But the code is not generating any plot. Can someone help please!

1 Answers1

2

That mainPanel() call wasn't nested correctly. It needed a comma before it, because it's an argument to sidebarLayout() (which in turn is an argument to fluidPage().

ui <- fluidPage(
  titlePanel("title panel"),
  sidebarLayout(
    position = "left",
    sidebarPanel(
      "sidebar panel",
      checkboxInput("donum1", "plot1", value = T),
      checkboxInput("donum2", "plot2", value = F),
      checkboxInput("donum3", "plot3", value = F)
    ),
    mainPanel("main panel", outputplot="myPlot")
  )
)

I like using the indention/tab level to show the nested structure more clearly. It helps me catch my mistakes.

After your second question:

I'm not sure how far you want to take it towards the example you gave. The most important difference I see is that you're not returning anything from pt1, pt2, & pt3. Remove the assignment part (e.g., p1<-).

server <- function(input, output, session) {
  Feature_A <- c(1, 2,1, 4,2)
  Feature_B <- c(4,5,6,6,6)
  Feature_C <- c(22,4,3,1,5)
  df<- data.frame(Feature_A ,Feature_B ,Feature_C)
  
  pt1 <- reactive({
    if (!input$donum1) return(NULL)
    ggplot(data=df, aes(Feature_A))+ geom_histogram()
  })
  pt2 <- reactive({
    if (!input$donum2) return(NULL)
    ggplot(data=df, aes(Feature_B))+ geom_histogram()
  })
  pt3 <- reactive({
    if (!input$donum3) return(NULL)
    ggplot(data=df, aes(Feature_C))+ geom_histogram()
  })
  
  output$myPlot = renderPlot({
    ptlist <- list(pt1(),pt2(),pt3())
    wtlist <- c(input$wt1,input$wt2,input$wt3)
    # remove the null plots from ptlist and wtlist
    to_delete <- !sapply(ptlist,is.null)
    ptlist <- ptlist[to_delete] 
    wtlist <- wtlist[to_delete]
    if (length(ptlist)==0) return(NULL)

    grid.arrange(grobs=ptlist,widths=wtlist,ncol=length(ptlist))
  })
  
}
wibeasley
  • 4,460
  • 2
  • 31
  • 58