【问题标题】:Error in eval(expr, envir, enclos) : could not find function "eval"eval 中的错误(expr、envir、enclos):找不到函数“eval”
【发布时间】:2023-03-29 23:22:01
【问题描述】:

我正在处理 Kaggle 数字识别器问题。当我尝试给定的代码时,我得到了错误。

eval(expr, envir, enclos) 中的错误:找不到函数“eval”

library(ggplot2)
library(proto)
library(readr)
train <- data.frame(read_csv("../input/train.csv"))

labels   <- train[,1]
features <- train[,-1]

rowsToPlot <- sample(1:nrow(train), 49)

rowToMatrix <- function(row) {
   intensity <- as.numeric(row)/max(as.numeric(row))
   return(t(matrix((rgb(intensity, intensity, intensity)), 28, 28)))
}

geom_digit <- function (digits, labels) GeomRasterDigit$new(geom_params = 
list(digits=digits),stat = "identity", position = "identity", data = NULL, 
inherit.aes = TRUE)

我在运行以下段时遇到错误。

GeomRasterDigit <- proto(ggplot2:::GeomRaster, expr={
draw_groups <- function(., data, scales, coordinates, digits, ...) {
bounds <- coord_transform(coordinates, data.frame(x = c(-Inf, Inf), y = c(
- Inf, Inf)), scales)
x_rng <- range(bounds$x, na.rm = TRUE)
y_rng <- range(bounds$y, na.rm = TRUE)
rasterGrob(as.raster(rowToMatrix(digits[data$rows,])), x_rng[1], y_rng[1], 
diff(x_rng), diff(y_rng),default.units = "native", just =c("left","bottom"),
interpolate = FALSE)
}
})

完整代码的链接: https://www.kaggle.com/benhamner/digit-recognizer/example-handwritten-digits/code

【问题讨论】:

  • 可能是这段代码和最新版本的ggplot2不兼容...
  • 有没有办法解决这个问题?
  • 只需 proto(ggplot2::GeomRaster) 重现相同的错误。

标签: r ggplot2 kaggle ggproto


【解决方案1】:

看看github上最新的ggplot2codeggproto 现在替换 proto 以及其他更改。

下面的代码应该可以正常工作。

 GeomRasterDigit <- ggproto(ggplot2:::GeomRaster, expr={
 draw_groups <- function(., data, scales, coordinates, digits, ...) {
 bounds <- coord_transform(coordinates, data.frame(x = c(-Inf, Inf), y = c(
 - Inf, Inf)), scales)
 x_rng <- range(bounds$x, na.rm = TRUE) 
 y_rng <- range(bounds$y, na.rm = TRUE)
 rasterGrob(as.raster(rowToMatrix(digits[data$rows,])), x_rng[1], y_rng[1], 
 diff(x_rng), diff(y_rng),default.units = "native", just =c("left","bottom"),
 interpolate = FALSE)
 }
 })

有一个关于ggprotovignette 很好读。

【讨论】: