eval中的错误(expr,envir,enclos) - 矛盾? [英] Error in eval(expr, envir, enclos) - contradiction?

查看:770
本文介绍了eval中的错误(expr,envir,enclos) - 矛盾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑为代码和特定问题提供更完整的示例 我正在编写一个函数来生成股票价格的时间序列图。但是,我收到以下错误:



eval(expr,envir,enclos)中的错误:找不到对象'df1234'

下面是该函数的一个例子:

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

#...代码未显示...
g
}

以及示例数据:

  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)#产生错误
g < ggplot(spy,aes(x = as.Date(Date,format =%Y-%m-%d),y = spy [,3],
color = brewer.pal(12,Set3)[1]))+ geom_point(size = 1)
g + geom_point(aes(x = as.Date(Date),y = spy [,4],
color = brewer.pal(12,Set3)[2]),size = 1)
##不会产生错误

正如您所看到的,代码是相同的。如果对ggplot()的调用是INSIDE函数,但是如果对ggplot()的调用不在函数中,则会出现错误。

任何人都有任何想法为何看似矛盾?


解决方案

我不确定这是否是你想要的,但它可能有帮助。我修改了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) 
$ b 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(color = brewer.pal(12,Set3)[1],size = 1)

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

plot.prices(间谍)

这里是没有使用的代码 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(color ='green',fill ='green',size = 1)

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

plot.prices(间谍)


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

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.

Anyone have any idea why the seeming contradiction?

解决方案

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

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:

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)

这篇关于eval中的错误(expr,envir,enclos) - 矛盾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆