eval(expr, envir, enclos) -矛盾?

时间:2021-08-19 08:19:09

Edited to give a fuller example of code and specific issue

I'm writing a function to produce time series plots of stock prices. However, I'm getting the following error

我正在写一个函数来生成股票价格的时间序列图。但是,我得到了以下错误。

Error in eval(expr, envir, enclos) : object 'df1234' not found

eval(expr, envir, enclos)中的错误:object 'df1234'没有找到。

Here's an example of the function:

这里有一个函数的例子:

plot.prices <- function(df1234) {
  require(ggplot2)
  g <- ggplot(df1234, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= df1234[, 3], 
              colour= brewer.pal(12,"Set3")[1])) + geom_point(size=1)
  g + geom_point(aes(x= date, y = df1234[, 4], 
                 colour= brewer.pal(12,"Set3")[2]), size=1)

  # ... code not shown...
  g
}

And example data:

和示例数据:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

plot.prices(spy) # produces error
g <- ggplot(spy, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= spy[, 3], 
              colour= brewer.pal(12,"Set3")[1])) + geom_point(size=1)
  g + geom_point(aes(x= as.Date(Date), y = spy[, 4], 
                 colour= brewer.pal(12,"Set3")[2]), size=1)
## does not produce error

As you can see, the code is identical. I get an error if the call to ggplot() is INSIDE the function but not if the call to ggplot() is OUTSIDE the function.

正如您所看到的,代码是相同的。如果调用ggplot()是在函数内部,但如果调用ggplot()函数在函数之外,则会出现错误。

Anyone have any idea why the seeming contradiction?

2 个解决方案

#1


1  

I am not sure whether this is what you want, but it might help. I modified agstudy's code:

我不确定这是否是你想要的,但可能会有所帮助。我修改agstudy的代码:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

library(ggplot2)
library(RColorBrewer)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= brewer.pal(12,"Set3")[1], size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= brewer.pal(12,"Set3")[2], size=1)
   gg
 }

 plot.prices(spy)

Here is code without using brewer.pal:

这里是不使用brewer.pal的代码。

library(ggplot2)

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= 'green', fill='green', size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= 'black', fill='black', size=1)
   gg
 }

 plot.prices(spy)

#2


10  

The error occur because you use df[, 7] in gglpot2, use column name Adj.Close will fix the problem.

错误发生是因为在gglpot2中使用了df[, 7],使用列名称Adj.Close将解决这个问题。

 g <- ggplot(df, aes(x= as.Date(Date, format= "%Y-%m-%d"),
                  y= Adj.Close)) + geom_point(size=1)

In fact the error , it is a scoping error. aes can't find the df environnement. It tries to look for it the global scope .

实际上,它是一个范围错误。aes找不到df环境。它试图寻找全球范围。

if you you want to use use indexing calls , you can use aes_string for example , and manipulate strings not expressions

如果您希望使用使用索引调用,可以使用aes_string,并操纵字符串而不是表达式。

plot.prices <- function(df) {
  require(ggplot2)

  df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

  g <- ggplot(df, aes_string(x= 'Date',
                      y= colnames(df)[7])) + geom_point(size=1)

  # ... code not shown...
  g
}

eval(expr, envir, enclos) -矛盾?

#1


1  

I am not sure whether this is what you want, but it might help. I modified agstudy's code:

我不确定这是否是你想要的,但可能会有所帮助。我修改agstudy的代码:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

library(ggplot2)
library(RColorBrewer)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= brewer.pal(12,"Set3")[1], size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= brewer.pal(12,"Set3")[2], size=1)
   gg
 }

 plot.prices(spy)

Here is code without using brewer.pal:

这里是不使用brewer.pal的代码。

library(ggplot2)

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= 'green', fill='green', size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= 'black', fill='black', size=1)
   gg
 }

 plot.prices(spy)

#2


10  

The error occur because you use df[, 7] in gglpot2, use column name Adj.Close will fix the problem.

错误发生是因为在gglpot2中使用了df[, 7],使用列名称Adj.Close将解决这个问题。

 g <- ggplot(df, aes(x= as.Date(Date, format= "%Y-%m-%d"),
                  y= Adj.Close)) + geom_point(size=1)

In fact the error , it is a scoping error. aes can't find the df environnement. It tries to look for it the global scope .

实际上,它是一个范围错误。aes找不到df环境。它试图寻找全球范围。

if you you want to use use indexing calls , you can use aes_string for example , and manipulate strings not expressions

如果您希望使用使用索引调用,可以使用aes_string,并操纵字符串而不是表达式。

plot.prices <- function(df) {
  require(ggplot2)

  df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

  g <- ggplot(df, aes_string(x= 'Date',
                      y= colnames(df)[7])) + geom_point(size=1)

  # ... code not shown...
  g
}

eval(expr, envir, enclos) -矛盾?