R 鼠标悬停上的动态图自定义标记失败

R 鼠标悬停上的动态图自定义标记失败,r,graph,dygraphs,R,Graph,Dygraphs,我试图根据x轴上的鼠标悬停点,将图形顶部的文本设置为自定义值。我认为我正确地遵循了动态图网站上的。有人能用我下面的例子解释一下如何正确操作吗 错误: Error in dygraph(df, { : object 'axes' not found 我的代码: library(quantmod) data<-getSymbols("USD/EUR",src="oanda",env=NULL) df<-as.data.frame(data) df2<-data.frame(ro

我试图根据x轴上的鼠标悬停点,将图形顶部的文本设置为自定义值。我认为我正确地遵循了动态图网站上的。有人能用我下面的例子解释一下如何正确操作吗

错误:

Error in dygraph(df, { : object 'axes' not found
我的代码:

library(quantmod)
data<-getSymbols("USD/EUR",src="oanda",env=NULL)
df<-as.data.frame(data)

df2<-data.frame(row.names(df))
df2$output<-sample(0:10,length(row.names(df)),replace=T)

library(dygraphs)
#dygraph(df)

dygraph(df, {
    axes:{
        x:{
            valueFormatter: function(dte){
                #how to return df2$output with the same date as the date from df that just got passed in? 
                return(???)
            }
        }   
    }   
})
库(quantmod)

数据我希望这对您有所帮助。必须说,它需要R和JavaScript的知识

首先,回答你的第一个问题。就语法而言,R和JavaScript之间有很大的区别

您提到的代码是一个JavaScript代码。我说的是:

{
    axes:{
        x:{
            valueFormatter: function(dte){
            #how to return df2$output with the same date as the date from df that just got passed in? 
                return(???)
            }
        }   
    }   
}
这段代码不能直接在R中使用。这就是为什么会出现错误

第一个问题的解决方案:

如果您在
dygraphs
包上执行一些搜索工作,您将找到一个函数
dyAxis
,该函数用于操作轴的详细信息

在这种情况下,您可以直接将与axisLabelFormattervalueFormatter相关的Javascript代码作为字符串传递

第二个问题的解决方案:

关于第二个问题,您可以尝试以下代码:

## 1:nrow(df) is column binded for a reason. Later, it will be used for finding the Date and the Output to be selected
g <- dygraph(data.frame(1:nrow(df),df$USD.EUR))

## Create the JavaScript Array for Date and Output and access it using the index specified earlier
dyAxis(g,'x',axisLabelFormatter = paste0('function(x){
    var dates = [',paste(paste0("'",df2[['row.names.df.']],"'"),collapse=','),'];

    return dates[x - 1];
}'),valueFormatter = paste0('function(x){
    var out = [',paste(df2$output,collapse=','),'];

    return out[x - 1];
}'))
##1:nrow(df)列绑定是有原因的。稍后,它将用于查找日期和要选择的输出

这正是我所需要的,但是我在第二个问题的解决上遇到了麻烦。是否可以将当前日期传递给返回正确答案的函数?我会写函数,只是不知道如何将日期传递给自定义函数?我用一个简单的函数更新了我的帖子。如果没有那么好,我只是想确定一下。你能用一个例子详细说明这个函数到底做了什么吗?我在我的问题中添加了更多的文本。这有帮助吗?谢谢,我会努力的,一旦我完成了就把它贴出来。
## 1:nrow(df) is column binded for a reason. Later, it will be used for finding the Date and the Output to be selected
g <- dygraph(data.frame(1:nrow(df),df$USD.EUR))

## Create the JavaScript Array for Date and Output and access it using the index specified earlier
dyAxis(g,'x',axisLabelFormatter = paste0('function(x){
    var dates = [',paste(paste0("'",df2[['row.names.df.']],"'"),collapse=','),'];

    return dates[x - 1];
}'),valueFormatter = paste0('function(x){
    var out = [',paste(df2$output,collapse=','),'];

    return out[x - 1];
}'))