Chen Jin Yan
Chen Jin Yan

Reputation: 109

Error "invalid model formula in ExtractVars" in shiny

This is my shiny UI image: enter image description here

global.R code:

library(shiny)
library(gsheet)
library(ggvis)
library(ggplot2)

urlA <- "https://docs.google.com/spreadsheets/d/1lVVGg2iRLtOaizrkaJHtPCyEnQiKAnJPyNj1kSSPXvY/edit?usp=sharing"
urlB <- "Another URL"
urlC <- "Another URL"
urlD <- "Another URL"
urlE <- "Another URL"

p_A <- gsheet2tbl(urlA)
p_B <- gsheet2tbl(urlB)
p_C <- gsheet2tbl(urlC)
p_D <- gsheet2tbl(urlD)
p_E <- gsheet2tbl(urlE)

paintingList <- list(p_A, p_B, p_C, p_D, p_E)


A_date <- split(p_A, format(as.Date(p_A$date), "%Y-%m"))
B_date <- split(p_B, format(as.Date(p_B$date), "%Y-%m"))
C_date <- split(p_C, format(as.Date(p_C$date), "%Y-%m"))
D_date <- split(p_D, format(as.Date(p_D$date), "%Y-%m"))
E_date <- split(p_E, format(as.Date(p_E$date), "%Y-%m"))

server.R code:

shinyServer(function(input, output) {
  productInput <- reactive({
  switch(input$painting,
         "A" = p_A,
         "B" = p_B,
         "C" = p_C,
         "D" = p_D,
         "E" = p_E)
})

output$paintDate <- renderUI({
  if (input$painting == "A"){
    selectInput("pdate", "Select a date", names(split(p_A, format(as.Date(p_A$date), "%Y-%m"))))
} else if(input$painting == "B") {
    selectInput("pdate", "Select a date", names(split(p_B, format(as.Date(p_B$date), "%Y-%m"))))
} else if(input$painting == "C"){
    selectInput("pdate", "Select a date", names(split(p_C, format(as.Date(p_C$date), "%Y-%m"))))
} else if(input$painting == "D"){
    selectInput("pdate", "Select a date", names(split(p_D, format(as.Date(p_D$date), "%Y-%m"))))
} else if(input$painting == "E"){
    selectInput("pdate", "Select a date", names(split(p_E, format(as.Date(p_E$date), "%Y-%m"))))
} else {
    return()
}

})

output$paintplot <- renderPlot({
  validate(need(input$pdate, ""))
  plot(as.formula(paste("date ~ ", input$pdate)), data = A_date)
})

ui.R code:

shinyUI(fluidPage(
includeCSS("css/main.css"),
navbarPage("Plot",
           tabPanel("ggPlot",
                    sidebarLayout(
                      sidebarPanel(
                        #add choices
                        selectInput("painting", "Select a option:",
                                    choices = c("A", "B", "C", "D", "E")),
                        uiOutput("paintDate")
                      ),

                      mainPanel(
                          plotOutput("paintplot"))
                      )
                   )
           ))
))

The error message:

Warning: Error in terms.formula: invalid model formula in ExtractVars

I want to select the data in a column and the display plot.

How to show my selection in plot?

Reference example: http://shiny.rstudio.com/gallery/unicode-characters.html

Please help me. Thanks.

Upvotes: 2

Views: 2415

Answers (1)

Mike Wise
Mike Wise

Reputation: 22807

The error message is caused by the your formula for your plot. I am not sure what you are really after, but after looking at your data, I changed the plot command around a bit and got something reasonable looking to work.

So this is the new server code:

server.R

shinyServer(function(input, output) {
  productInput <- reactive({
    switch(input$painting,
           "A" = p_A,
           "B" = p_B,
           "C" = p_C,
           "D" = p_D,
           "E" = p_E)
  })

  output$paintDate <- renderUI({
    if (input$painting == "A"){
      selectInput("pdate", "Select a date", names(split(p_A, format(as.Date(p_A$date), "%Y-%m"))))
    } else if(input$painting == "B") {
      selectInput("pdate", "Select a date", names(split(p_B, format(as.Date(p_B$date), "%Y-%m"))))
    } else if(input$painting == "C"){
      selectInput("pdate", "Select a date", names(split(p_C, format(as.Date(p_C$date), "%Y-%m"))))
    } else if(input$painting == "D"){
      selectInput("pdate", "Select a date", names(split(p_D, format(as.Date(p_D$date), "%Y-%m"))))
    } else if(input$painting == "E"){
      selectInput("pdate", "Select a date", names(split(p_E, format(as.Date(p_E$date), "%Y-%m"))))
    } else {
      return()
    }

  })

  output$paintplot <- renderPlot({
    validate(need(input$pdate, ""))
    df <- data.frame(A_date[[input$pdate]])
    plot(as.Date(df$date),df$pop)
  })

})

And it yields this plot:

enter image description here

Other than that I changed all the urlB, urlC, etc. in global.R to be the same as urlA so as to get things to work so I could test it.

Upvotes: 1

Related Questions