2
votes

I am trying to make shiny app.

I want my app to generate multiple plots based on number of check box clicked. Iris is the dataframe which has various quantitative variables and i am trying to dynamically generate density plots of 4 variables which are in iris. Below is my server and ui page till now.

library(shiny)
library(ggplot2)
library(tidyverse)


df<-iris[,colnames(iris)!="Species"]

ui<-fluidPage(

titlePanel("Density Plots of Quantitative Variables"),
sidebarLayout( sidebarPanel( sliderInput("bw","Slide to change bandwidth 
                                         of Plot",min=0.1,max=20,value=3,step=0.1,animate=TRUE),

                             checkboxGroupInput("variableinp","Choose variables",
                                                choices=colnames(df),selected = colnames(df)[1]),verbatimTextOutput("value")
),
mainPanel( plotOutput("densityplot"))
)
)


server<-function(input,output){

# observeEvent(input$variableinp, {
#      print((input$variableinp))
#  })

output$densityplot <- renderPlot({

    if(!is.null(input$variableinp)) {

        getoutandquant <- function(x) {
            q1<-quantile(x)[[2]]
            q3<-quantile(x)[[4]]
            IQR<-q3-q1

            out1<-q3+(1.5)*IQR
            out2<-q1-(1.5)*IQR

            #Finding the list of points which are outliers for a particular 
           variable.
            out<-x[x>out1]
            out2<-x[x<out2]
            outliers<-tibble(x=c(out,out2),y=0)

            return(outliers)
        }
        nplot<-length(input$variableinp)
        x<-input$variableinp

        for ( i in 1:nplot) {
            outlier<-getoutandquant(df[,x[i]])

        }    

        p1<-ggplot(df,aes_string(input$variableinp[i]))+
            stat_density(geom="line",adjust=input$bw)+ ylab("Density\n")
        p1+geom_point(data=outlier,aes(x,y),shape=23)
    }
})
}


shinyApp(ui=ui,server=server)

Below is my output as far. As it can be seen only one plot is generated. With 2 check boxes checked I would like 2 plots, with 3 checkbox 3 plots and so on. :

Density Plots of variables

Can anyone give me hints or any lead to on how should i do it. My plots are generating good. I just need the multiple plots to be produced based on number of checkboxes clicked.

1
Your code can not work properly, please modified what you have done so far, and use dput to post your dataframe.Jim Chen
Please share some reproducbile dataVishesh Shrivastav
@VisheshShrivastav I have included iris data sets to start with.Naveen Gabriel
@JimChen Obviously it is not working. I need multiple plots I have included iris datasets as an example.Naveen Gabriel

1 Answers

2
votes

Try to store your ggplot output in a list, then use do.call and grid.arrange to combine all of it into one plot:

Try the following code, it should run perfectly: You should install gridExtra package to combine multiple ggplot.

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

df<-iris[,colnames(iris)!="Species"]

ui<-fluidPage(

  titlePanel("Density Plots of Quantitative Variables"),
  sidebarLayout( sidebarPanel( sliderInput("bw","Slide to change bandwidth 
                                           of Plot",min=0.1,max=20,value=3,step=0.1,animate=TRUE),

                               checkboxGroupInput("variableinp","Choose variables",
                                                  choices=colnames(df),selected = colnames(df)[1]),verbatimTextOutput("value")
  ),
  mainPanel( plotOutput("densityplot"))
  )
  )


server<-function(input,output){

  # observeEvent(input$variableinp, {
  #      print((input$variableinp))
  #  })

  output$densityplot <- renderPlot({

    if(!is.null(input$variableinp)) {

      getoutandquant <- function(x) {
        q1<-quantile(x)[[2]]
        q3<-quantile(x)[[4]]
        IQR<-q3-q1

        out1<-q3+(1.5)*IQR
        out2<-q1-(1.5)*IQR

        #Finding the list of points which are outliers for a particular 
        out<-x[x>out1]
        out2<-x[x<out2]
        outliers<-tibble(x=c(out,out2),y=0)

        return(outliers)
      }
      nplot<-length(input$variableinp)
      x<-input$variableinp

      p<-list()
      for ( i in 1:nplot) {
        outlier<-getoutandquant(df[,x[i]])
        p[[i]]<-ggplot(df,aes_string(input$variableinp[i]))+
          stat_density(geom="line",adjust=input$bw)+ ylab("Density\n")+
          geom_point(data=outlier,aes(x,y),shape=23)
      }    
      do.call(grid.arrange,p)
    }
  })
}


shinyApp(ui=ui,server=server)

Also , please select this post as the best answer if it solved your problem, thanks a million.