R使用事件触发选项和Javascript代码的gvisTable预选 [英] R gvisTable pre-selection using event trigger option and Javascript code

查看:109
本文介绍了R使用事件触发选项和Javascript代码的gvisTable预选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中,我正在设置gvisTable的输出选择,以便突出显示特定的行或列。

In R I am looking to set the selection of the output for a gvisTable so that a specific row or column is highlighted.

例如,我有以下代码:

For example I have the following code:

a <- as.data.frame(matrix(1:100, nrow=10))

plot(gvisTable(a))

点击第4行,它是否会突出显示。

and only until clicking, on say row 4, does it highlight.

有没有办法在运行绘图函数时预选第4行,即它会自动突出显示第4行。

Is there a way to preselect row 4 upon running the plot function, i.e. it automatically highlights row 4.

查看googleVis的小插曲后,似乎有一个事件侦听器和事件触发器,它们可以在选项中插入一些javascript代码,以便更改输出。这是通过使用选项gvis.listener.jscode完成的。是否有触发器触发事件​​的等价物。我认为javascript代码需要使用像 setSelection()之类的东西,但我的Javascript知识最多是有限的,所以我不太确定如何整合它。

Having looked in the vignette for googleVis, there appears to be an event listener and and event trigger which can have some javascript code inserted into the options so that the output is altered. This was done by using the option gvis.listener.jscode. Is there an an equivalent for the trigger to fire an event. I think the javascript code would need to use something like setSelection() but my Javascript knowledge is limited at best and so I am not too sure how to integrate it.

ps可以通过运行找到eventlistener

p.s the eventlistener can be found by running

demo(EventListener)


推荐答案

我唯一能找到的方法是注入通过调用 gvisTable()创建的一些自定义JavaScript。您将无法使用EventListener作为您提供的示例 - 原因是,当前实现的eventListener只能在单击表时执行。您需要在此之前执行您的代码。它看起来像插入它的最佳位置,然后是在返回的HTML的 jsDrawChart 部分。这是图表对象的JS表示组合的地方。在>绘制图表之后,您可以对该对象进行额外调用。

The only way I can figure out to do it is to inject some custom JavaScript into the HTML created by the call to gvisTable(). You won't be able to use the EventListener as the example you provided does -- the reason being that the eventListener, as currently implemented, only gets executed when the table is clicked. You need your code to execute before then. It looks like the best place to insert it, then, is in the jsDrawChart section of the returned HTML. This is where the JS representation of the chart object gets assembled. After drawing the chart, you can then make additional calls to the object.

快速查看<一个href =https://google-developers.appspot.com/chart/interactive/docs/gallery/table =nofollow>谷歌图表文档表格对象显示你有一个很好的 setSelection()函数可以在Javascript中使用,假设您使用正确的参数格式。

A quick look at the Google Charts documentation for the table object shows that you have a nice setSelection() function available to you in Javascript, assuming you use the proper parameter format.

我做了以下操作:

I did the following:

mytab <- (gvisTable(a,chartid="myTable"))
#you just want to add the following line of JS before the close bracket in the jsDrawChart function.
# chart.setSelection([{'row':2, 'column':null}]);

#in my case, that turned out to be the following:
mytab$html$chart["jsDrawChart"] <- "\n// jsDrawChart\nfunction drawChartmyTable() {\n  var data = gvisDatamyTable();\n  var options = {};\noptions[\"allowHtml\"] = true;\n\n     var chart = new google.visualization.Table(\n       document.getElementById('myTable')\n     );\n     chart.draw(data,options);\n  chart.setSelection([{'row':2, 'column':null}]);   \n\n}\n  \n"

您当然可以通过使用RegEx自动在右括号之前插入额外的JS行,或者修改GoogleVis函数的源代码,使其更加健壮。

You could certainly make that a bit more robust by automatically inserting the additional line of JS before the close bracket using RegEx or modifying the source of the GoogleVis function.

但是一旦我添加了该行的JS,我现在可以运行 plot(mytab)并让它自动选择我想要的行(请记住它是0索引的)。

But once I've added that line of JS, I'm now able to run plot(mytab) and have it automatically select the row I want (keep in mind that it's 0-indexed).

这篇关于R使用事件触发选项和Javascript代码的gvisTable预选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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