Shiny R 中的 eval(substitute(expr), envir, enclos) 错误
Error in eval(substitute(expr), envir, enclos) in Shiny R
我在尝试 运行 我正在构建的 Shiny 应用程序时收到以下错误。错误是:
Listening on http://........
Error in eval(substitute(expr), envir, enclos) :
incorrect length (0), expecting: 202
我一直在根据 movie-explorer 示例应用程序对我的应用程序的基础进行建模。数据通过 CSV 输入,是一个 202 行长的数据帧。
更新
通过调试器 运行ning 后,我发现导致错误的实际表达式是在 %>% 函数中找到的。执行以下两行代码后出现错误:
env[["_lhs"]] <- eval(lhs, parent, parent)
result <- withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
此时代码中的变量值为:
lhs = 公司
parent = 环境
env = 环境
显然,代码需要我的数据帧,但收到的是一个空集。原因不明。
更新结束
具体问题:我做错了什么,我该如何解决?
我的 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")
})
解决方案:在设置过滤器的代码中,引用所有小写变量名 (input$dive),而不是最初出现在初始数据框中的大写变量名(输入$潜水)。所以把 input$Dive 改成 input$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)
}
我在尝试 运行 我正在构建的 Shiny 应用程序时收到以下错误。错误是:
Listening on http://........
Error in eval(substitute(expr), envir, enclos) :
incorrect length (0), expecting: 202
我一直在根据 movie-explorer 示例应用程序对我的应用程序的基础进行建模。数据通过 CSV 输入,是一个 202 行长的数据帧。
更新
通过调试器 运行ning 后,我发现导致错误的实际表达式是在 %>% 函数中找到的。执行以下两行代码后出现错误:
env[["_lhs"]] <- eval(lhs, parent, parent)
result <- withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
此时代码中的变量值为:
lhs = 公司
parent = 环境
env = 环境
显然,代码需要我的数据帧,但收到的是一个空集。原因不明。
更新结束
具体问题:我做错了什么,我该如何解决?
我的 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")
})
解决方案:在设置过滤器的代码中,引用所有小写变量名 (input$dive),而不是最初出现在初始数据框中的大写变量名(输入$潜水)。所以把 input$Dive 改成 input$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)
}