rPlot tooltip problems

2019-05-04 18:57发布

I have a simple example using tooltips with rCharts that doesn't seem to work:

set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100))
rPlot(y ~ x, data = test, 
      type = 'point',
      tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}") 

An empty page comes up. The plot is there if I remove the tooltip option. I'm using rCharts_0.4.1, R Under development on x86_64-apple-darwin10.8.0 (64-bit) and version 31.0.1650.63 of Chrome.

Bonus question! Can tooltips contain variables in the data set but not used in x, y, etc? I have a large data set and I'd like to annotate the data points with an ID variable that has a unique value per row.

Thanks,

Max

标签: r rcharts
2条回答
别忘想泡老子
2楼-- · 2019-05-04 19:58

Rcharts 0.4.2

I can't point out where I've seen this before, but the best I can offer is that the current instruction is to wrap your js function like so:

"#!function(item) { return item.x }!#"

set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test, 
        type = 'point',
        tooltip = "#!function(item){ return 'x: ' + item.x + 
        ' y: ' + item.y + ' id: ' + item.id }!#")

I could never get the tips on different lines. You can see the problem by typing p$show('inline') and including line breaks where you want them when creating the tooltip. Mustache is converting the linebreaks, which means they disappear in the resulting JS function and cause the tooltip function to span several lines - causing an error. I've tried to escape the newline character and append each string containing a newline character with .replace('\\n', '\n'), but that obviously results in the same problem.

For the time being, your best bet is to make the plot as shown here, type p$save('filepath.html') and manually add the line breaks. Otherwise, the question is how to pass a literal newline character to mustache.

查看更多
不美不萌又怎样
3楼-- · 2019-05-04 19:59

Your code works/worked using rCharts 0.3.51 (Chrome 31.0.1650.63 m, Win7 x64):

set.seed(1)
require(rCharts)
test <- data.frame(x = rnorm(100), y = rnorm(100), 
                   name=replicate(100, paste(sample(letters, 5, rep=T), collapse="")))
rPlot(y ~ x, data = test, 
      type = 'point',
      tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}")

You should be able to reference all columns in test - as I did with name.

enter image description here

查看更多
登录 后发表回答