Plot from different datasets upon different inputs using conditional statement

16 views
Skip to first unread message

Prashanth Kumar

unread,
Nov 28, 2017, 3:49:02 AM11/28/17
to Shiny - Web Framework for R
I have recently started working on shiny, for my school project I am developing a Shiny application that plot the graph using ggplot upon selected inputs.

Now, one of my requirement is that, upon selecting certain inputs in the front-end I have to plot the graph using certain dataset. Say I have 3 different datasets, and 4 inputs then,<br>

   
if(input$A = 1 && input$B = 2 && input$C = 3 && input$D = 4){
     
//Plot using Dataset1
   
}
   
else if(input$A = 1 && input$B = 2 && input$C = 3 && input$D = 4){
     
//Plot using Dataset2
   
}
   
else if(input$A = 2 && input$B = 3 && input$C = 4 && input$D = 4){
     
//Plot using Dataset3
   
}



I figured out that for this sort of logic to work, I need a submit button in the form, I've checked a couple of examples online, but dint properly understand how to apply it in my case.

Now, when I try to plot the data using if condition using reactive as below, I am getting `Error:could not find function "g"`
What am I doing wrong here? and how will I be able to plot this information upon Submit button?

I'd appreciate it, if you could please take a look at my code below and help me.

Here's a sample dataset you can use to reproduce the example -

**UI.R**

   
 library(shiny)
    library
("RMySQL")
    library
(ggplot2)
    library
(plotly)
   
# Database Connection and the fetch
    dataset
<- read.csv("dataset.csv", header=TRUE)
    dataset$X
<- NULL
    dataset$sex
<- sub("^$", "Unknown", dataset$sex)
   
    fluidPage
(
      fluidRow
(
        tags$head
(
          tags$style
(HTML("
          .shiny-output-error-validation {
            color: #48ca3b;
            font-size: 14pt;
          }
          body {
            -moz-transform: scale(0.9, 0.9); /* Moz-browsers */
            zoom: 0.9; /* Other non-webkit browsers */
            zoom: 90%; /* Webkit browsers */
          }
        "
))
       
),
        titlePanel
("Define census"),
       
        sidebarPanel
(
         
        dateRangeInput
('dateRange',
                       label
= 'Date Input',
                       start
= as.Date("1967-01-01"), end = Sys.Date()),
       
        selectInput
("region", label = "Region",
                    choices
= c("All",levels(dataset$region)),
                    selected
= "ANI"),
       
        selectInput
("species", label = "Species",
                    choices
= c("All",levels(dataset$species)),
                    selected
= "ANI"),
       
        selectInput
("sex", label = "Sex",
                    choices
= unique(dataset$sex), multiple = TRUE,
                    selected
= unique(dataset$sex)),
       
        radioButtons
(
         
"standard_cat_options",
          label
="Standard Category",
          choices
=list(
           
"All",
           
"Multiple Select"), selected="All"),
        conditionalPanel
(
          condition
= "input.standard_cat_options != 'All'",
          selectInput
(
           
'standard_cat',
            label
= "Select categories", multiple = TRUE,
            choices
=unique(dataset$standard_cat)
         
)
       
),
       
        radioButtons
(
         
"age_cat_options",
          label
="Age Category",
          choices
=list(
           
"All",
           
"Multiple Select"), selected="All"),
        conditionalPanel
(
          condition
= "input.age_cat_options != 'All'",
          selectInput
(
           
'broad_cat',
            label
= "Select age category", multiple = TRUE,
            choices
=c("adult", "adjuv", "juv", "pup", "orphan", "W", "YOY", "SA1", "SA2", "SA3", "SA4", "SA5", "SA", "mature", levels(dataset$broad_cat))
         
)
       
),
       
        selectInput
('x', 'X', names(dataset), names(dataset)[[2]]),
       
        selectInput
('y', 'Y', names(dataset), names(dataset)[[8]]),
       
        submitButton
("Submit")
       
),
      mainPanel
(
        column
(12, plotlyOutput("plot1")),
        hr
(),
        column
(12, plotlyOutput("plot2"))
       
)
     
)
   
)



**Server.R**

   
library(ggplot2)
    library
("RMySQL")
    library
("mgcv")
    library
(plotly)
   
function(input, output) {
     
   
# Database Connection and the fetch
      dataset
<- read.csv("dataset.csv", header=TRUE)
     
      dataset$X
<- NULL
      dataset$sex
<- sub("^$", "Unknown", dataset$sex)
     
     
# dataset1 <- read.csv("dataset1.csv", header = TRUE, fill = TRUE)
     
# dataset2 <- read.csv("dataset2.csv", header = TRUE, fill = TRUE)
     
# dataset3 <- read.csv("dataset3.csv", header = TRUE, fill = TRUE)
     
   
# DataBase disconnected
   
# Using the datafram created with name data
   
# as in the given data Date is of String type so converted to "Date" type
   
    dataset$date
<- as.Date(dataset$date)
   
   
#reactive variable initiation for the various inputsinstall.packages('rsconnect')
   
    reactive
({
   
#if(input$region == "ANI" && input$species == "Ej" && input$sex == "Unknown" && input$standard_cat == "All" && input$broad_cat == "YOY"){
     
   
if(input$region == "ANI"){
      l
<-  subset(dataset, region %in% input$region)
         
     
      k
<- subset(l(), date >= as.Date(input$dateRange[1]) & date <= as.Date(input$dateRange[2]))
     
      m
<-
          subset
(k(), species %in% input$species)
       
     
      n
<-
        validate
(
          need
(input$sex, 'No data exists, Please select Gender input')
       
)
        subset
(m(), sex %in% input$sex)
       
     
      o
<-
          validate
(
            need
(input$standard_cat, 'No data exists, please select a Standard Category')
           
)
          subset
(n(), standard_cat %in% input$standard_cat)
       
     
      g
<-
          validate
(
            need
(input$broad_cat, 'No data exists, please select an Age Category')
         
)
          subset
(o(), broad_cat %in% input$broad_cat)
       
     
}
   
})
   
   
#output plots
   
      output$plot1
<- renderPlotly({
        p
<- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_point(alpha=0.4)
        ggplotly
(p)
     
})
      output$plot2
<- renderPlotly({
        q
<- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_smooth()
        ggplotly
(q)
     
})
   
}



Thank you.
Reply all
Reply to author
Forward
0 new messages