rrbest rrbest - 2 months ago 25
R Question

ggplot2 in shiny error: ggplot2 doesn't know how to deal with data of class packageIQR

I am attempting to build a simple

shiny
app that creates a data table based on inputs and outputs a line plot using
ggplot2
. I receive the following error:


Error: ggplot2 doesn't know how to deal with data of class packageIQR


In this app, a user uses a slider to define the time period, or the length of X, and also the change in value by defining the starting value and the change in the value over X. The plot is a linear line. I am new to
shiny
, so if there are better ways to set up this I also would like suggestions on the best way to set up the server code, but for now I simply get an error and produce no plot.

server.R

library(shiny)
library(ggplot2)

shinyServer(function(input, output){

reactive({
data <- data.table(months = seq(1, input$months, by = 1),
value = seq(input$startingValue,
input$startingValue + input$valueChange,
length.out = input$months))
})


output$yield <- renderPlot({
p <- ggplot(data(), aes(x=months, y=value, colour=value)) +geom_line()
print(p)
})
})

Answer

You just need to define the reactive function :

data <- reactive({
        data.table(months = seq(1, input$months, by = 1),
               value  = seq(input$startingValue, 
                           input$startingValue + input$valueChange, 
                           length.out = input$months))
})

Note here you don't need to define the reactive function since you have one caller. You can put all the code in the plot section:

output$yield <- renderPlot({  
 data <- data.table(months = seq(1, input$months, by = 1),
               value  = seq(input$startingValue, 
                           input$startingValue + input$valueChange, 
                           length.out = input$months))
 p <- ggplot(data, aes(x=months, y=value, colour=value)) +geom_line()
 print(p)
})
Comments