3

I am trying to get a Shiny dashboard although my graph will not work.

I have a data.frame consisting of a Date column in date format and 4 numeric column types which are ISA, NonISA, FixedRateInterest and VariableRateInterest and have filled the N/A values with 0.

I have checkboxes with a date range slider and I can get the graph to render but when I try and add in the DateRangeslider it doesn't not work and throws the following error:

Unknown input:data.frame

This is my code to create the dashboard. Can anyone help me understand why it is not working please? I know it has something to do with the ggplot line but not sure what.

Thanks

isa <- read.csv("isa.csv")
isa$Date <- paste0(isa$Date, "01")
isa$Date <- as.character(isa$Date)
isa$Date <- as.Date(isa$Date, format = "%Y%m%d")
date <- isa$Date


# Define UI ----
ui <- fluidPage(
  titlePanel("Customer"),
  sidebarLayout(
  sidebarPanel(
    br(),
    br(),
    br(),
    br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
img(src = "Logo.jpg", height = 100, width = 150)),
  fluidRow(
      h1("Choose Dataset"),
    br(),
  br(),
  column(1,
         checkboxGroupInput(inputId = "product",
                     h3("Product"),
                     choices = c('ISA',
                                    "Non-ISA",
                                    "Fixed",
                                    "Variable"),
                     selected = 1)),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),
  br(),

  column(2,
         dateRangeInput(inputId = "dates", h3("Date Range"),
                        start = min(isa$Date),
                        end =  max(isa$Date),
                        min = min(isa$Date),
                        max = max(isa$Date),
                        separator = "-", format = "yyyy/mm/dd")

  ))),
  br(),
  br(),
  br(),



mainPanel(
  plotOutput("barplot")
   )
 )
)
# Define server logic ----
server <- function(input, output) {




  dataInput <- reactive({

switch(input$product,
       "ISA" = ISA,
       "Non-ISA" = isa$NonISA,
       "Fixed" = isa$FixedRateInterest,
       "Variable" = isa$VariableRateInterest)

It Is here that it seems to fail

      })
 dateInputx <- reactive({
    isa[isa$Date >= input$dates[1] &  isa$Date <= input$dates[2],]
   })


  output$barplot <- renderPlot({

And doesn't recognise the x axis on ggplot

ggplot(data = dataInput(), aes_string(x= dateInputx(), y = dataInput())) +
         geom_line(stat ="identity")
  })

}


# Run the app ----
shinyApp(ui = ui, server = server)
geds133
  • 1,503
  • 5
  • 20
  • 52
  • at the beginning of the ```server``` part, you put ```date <- isa$Date``` and then you put ```isa$Date``` in ```as.Date``` format. But in ```aes```, you use ```date``` so maybe you should put ```Date``` instead (I hope it's clear) – bretauv Jul 04 '19 at 16:44
  • This was so that the x axis is just a selection of Dates. When i tried your way, I get this `error: object 'Date' not found` – geds133 Jul 08 '19 at 13:05
  • after `date <- isa$Date`, what is `str( date )`, is it a character? – SymbolixAU Jul 09 '19 at 00:10

1 Answers1

1

Your data argument in ggplot should be a dataframe and not a column. Then, use aes_string() with the corresponding column names. I've tried to make a reproducible example below:

library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('ycol','Y axis',choices = c('City Mileage','Highway Mileage')) # 
    ),
    mainPanel(
      plotOutput('plot')
    )
  )
)

server <- function(input,output){
  data(mpg)  # Replace mpg with your dataframe of choice(isa in your case)

  dataInput <- reactive({  # Required y column name as in dataframe
    switch(input$ycol,
           "City Mileage" = 'cty',
           "Highway Mileage" = 'hwy')

  })


  output$plot <- renderPlot({
                         # Use aes_string for column names stored in strings
    p <- ggplot(data=mpg,aes_string(x='displ',y=dataInput()))+
      geom_line()

    p
  })
}

shinyApp(ui,server)
Rohit
  • 1,967
  • 1
  • 12
  • 15