0
votes

I would like to input a category and a number to build a model to predict daily sales by sales rank in the different category. The first input is to subset my data to build model, and the second input is the rank as an input to the model. Finally, the output is the prediction from the model as daily sales.

Here are my ui.r and server.r

library(shiny)
shinyUI(fluidPage(

titlePanel("Daily Sales Prediction by Sales Rank "),

selectInput("select", label = h3("Select Category"), 
          choices = list("toy_display_on_website" = 1, "toy_display_on_website" = 2, "toy_display_on_website" = 3), 
          selected = 1),

hr(),
#fluidRow(column(3, verbatimTextOutput("value"))),

  numericInput("num", label = h3("Sales Rank "), value ="100"),
  hr(),
  fluidRow(column(3, verbatimTextOutput("value")))
))

(in server.r, I obtained a dataframe 'juvo' with col names 'asin' 'AvgQuantity' 'AvgRank1' 'Catgory1')

shinyServer(function(input, output) {

juvosub<-reactive({juvo[juvo$Catgory1==input$select,]})
m1 <- reactive({glm.nb(as.formula(paste(log10(as.numeric(juvosub()$AvgQuantity))," ~ ",paste(log10(as.numeric(juvosub()$AvgQuantity)),collapse="+"))), 
                     data = juvosub())})
juvoPredict<-reactive({data.frame(AvgRank1=input$num)})
result<-reactive({predict(m1(),juvoPredict(),type = "response") })
output$value <- renderPrint({result()})

})

As referred, I would like to subset juvo data frame by input$select, and then build a model by 'AvgQuantity' ~ 'AvgRank1'. With the built model, input$num is the rank, and I would like to get quantity as output.

But the error messages are 'invalid term in model formula' or 'cannot coerce class ""reactivevalues"" to a data.frame' Thanks.

1

1 Answers

0
votes

Can you provide an example data set? Otherwise it's very difficult to answer your question.

That being said, you should read more about how to use reactive expressions (which are functions, not objects). You're trying to pass a reactive expression to the data argument of your model and the object argument of predict(), which is likely your first error. Similarly, the second error likely also results from providing a reactive expression instead of values to your data.frame argument.