在Shiny R中的eval(替换(expr),envir,enclos)出错

时间:2015-03-06 16:57:28

标签: r shiny shiny-server ggvis

我在尝试运行一个闪亮的应用程序时遇到以下错误。错误是:

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

在电影资源管理器示例App之后,我一直在为我的应用程序的基础建模。数据通过CSV输入,是一个202行长的数据帧。

更新
运行调试器后,我发现导致错误的实际表达式在%>%函数中找到。执行以下两行代码后发生错误:

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

在代码的这一点上,变量值是:

lhs =公司
parent =环境
env =环境

显然,代码期待我的数据帧但是收到一个空集。原因不明。

END UPDATE

具体问题:我做错了什么以及如何解决?

我的server.R文件如下:

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")

})

1 个答案:

答案 0 :(得分:1)

解决方案:在设置过滤器的代码中,引用所有小写变量名称(输入$ dive),而不是最初出现在初始数据框中的大写变量名称(输入$ Dive) 。因此,将输入$ Dive更改为输入$ dive。

错误的方式

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

正确的方式。

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