Search code examples
rshinyshiny-serverggvis

Error in eval(substitute(expr), envir, enclos) in Shiny R


I'm receiving the following error when trying to run a Shiny App I'm building. The error is:

Listening on http://........
Error in eval(substitute(expr), envir, enclos) : 
  incorrect length (0), expecting: 202  

I've been modeling the base of my app after the movie-explorer example App. The data is fed in via CSV and is a 202 lines long dataframe.

UPDATE
After running through debugger I've found that the actual expression that causes the error is found within the %>% function. The error occurs after the following two lines of code are executed:

env[["_lhs"]] <- eval(lhs, parent, parent)
result <- withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))

At this point in the code the variable values are:

lhs = companies
parent = Environment
env = Environment

Obviously, the code is expecting my dataframe but is receiving an empty set. Reason is unknown.

END UPDATE

SPECIFIC QUESTION: What am I doing wrong and how do I fix it?

my server.R file looks like:

library(shiny)
library(dplyr)
library(ggvis)

all_dat = read.csv("data/company_data.csv")

shinyServer(function(input, output, session) {

companies <- reactive({
# Filter the clicks, views, opens
clicks <- input$Clicks
pageviews <- input$Pageviews
opens <- input$Opens
engage_value <- input$Engage_Value
viewspermsg <- input$views_per_msg
clickspermsg <- input$clicks_per_msg
openspermsg <- input$opens_per_msg

# Apply Filters
d <- all_dat %>%
  filter(
    Clicks >= clicks,
    Pageviews >= pageviews,
    Opens >= opens,
    Engage_Value >= engage_value,
    views_per_msg >= viewspermsg,
    clicks_per_msg >= clickspermsg,
    opens_per_msg >= openspermsg
    ) %>%
  arrange(Clicks)

# Optional: filter by Dive
if (input$Dive != "All") {
  size <- paste0("%", input$Dive, "%")
  d <- d %>% filter(Dive %like% dive)
}
# Optional: filter by Dive Family
if (input$Family != "All") {
  family <- paste0("%", input$Family, "%")
  d <- d %>% filter(Family %like% family)
}
# Optional: filter by Industry
if (input$Industry != "All") {
  industry <- paste0("%", input$Industry, "%")
  d <- d %>% filter(Industry %like% industry)
}
# Optional: filter by Dive Family
if (input$Size != "All") {
  size <- paste0("%", input$Size, "%")
  d <- d %>% filter(Size %like% size)
}

d <- as.data.frame(d)

d$Has_International <- character(nrow(d))
d$Has_International[d$Oscars == 0] <- "No"
d$Has_International[d$Oscars >= 1] <- "Yes"
#I don't know if I need this.
d
  })

  company_tooltip <- function(x) {
if (is.null(x)) return(NULL)
if (is.null(x$Unnamed..0)) return(NULL)

all_dat <- isolate(companies())
company <- all_dat[all_dat$Unnamed..0 == x$Unnamed..0, ]

paste0("<b>", company$Company, "</b><br>",
  company$Industry, "<br>",
  company$Size, " employees", "<br>",
  company$Company_Type, "<br>",
  round(company$Percent_New, digits=2), " % New Readers"
)
  }

  #reactive labels and graph aspects
  vis <- reactive({
    # Labels for axes
    xvar_name <- names(axis_vars)[axis_vars == input$xvar]
    yvar_name <- names(axis_vars)[axis_vars == input$yvar]

# Normally we could do something like props(x = ~BoxOffice, y = ~Reviews),
# but since the inputs are strings, we need to do a little more work.
xvar <- prop("x", as.symbol(input$xvar))
yvar <- prop("y", as.symbol(input$yvar))

companies %>%
  ggvis(x = xvar, y = yvar) %>%
  layer_points(size := 50, size.hover := 200,
               fillOpacity := 0.2, fillOpacity.hover := 0.5,
               stroke = ~Has_International, key := ~Unnamed..0) %>%
  add_tooltip(company_tooltip, "hover") %>%
  add_axis("x", title = xvar_name) %>%
  add_axis("y", title = yvar_name) %>%
  add_legend("stroke", title = "International Presence", values = c("Yes", "No")) %>%
  scale_nominal("stroke", domain = c("Yes", "No"),
                range = c("orange", "#aaa")) %>%
  set_options(width = 500, height = 500)
  })

  vis %>% bind_shiny("plot1")

})

Solution

  • Solution: In the code where filters are setup, reference all lowercase variable names (input$dive), not the uppercase variable name as it originally appears in the initial data frame (input$Dive). So change input$Dive to input$dive.

    The wrong way

    # Optional: filter by Dive
    if (input$Dive != "All") {
      size <- paste0("%", input$Dive, "%")
      d <- d %>% filter(Dive %like% dive)
    }
    

    The right way.

    # Optional: filter by Dive
    if (input$dive != "All") {
      size <- paste0("%", input$dive, "%")
      d <- d %>% filter(Dive %like% dive)
    }