问题标签 [htmlwidgets]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
142 浏览

javascript - 在 JavaScript 中检索 R 对象属性

我有一个包含 100 个观察值的双变量数据集。我使用了六边形分箱,最终得到了 26 个六边形分箱。为了保存 26 个六边形 bin 中的每一个中的 100 个观测值的行,我使用了base::attrR 中的函数。在下面的代码中,这是在以下位置完成的:

我正在尝试创建Plotly六边形分箱的交互式 R 对象,以便如果用户单击给定的六边形箱,他们将获得分组到该箱中的 100 个观察值的行。我已经完成了这个目标的一部分。我的 MWE 如下:

当我运行此代码并在 Web 浏览器中打开它时,我获得了一个如下所示的交互式绘图(绿色框不在绘图中;它被叠加用于解释目的):

在此处输入图像描述

如果我点击绿色框内的六边形,正确hexID的 40 和counts3 将打印到控制台。此时,我想获取放入那个六边形 bin 的 3 行原始数据框。

我知道如何在 R 中通过使用该函数onRender()htmlwidgets包的函数之外执行此操作base::attr。例如,我可以执行以下操作:

并收到以下正确的 3 个数据点,这些数据点被放入我点击的那个 bin 中:

我正在处理比这个 MWE 更大的数据集。出于这个原因,我使用该base:attr函数的目的是防止更大的数据帧四处飘荡。但是,我不确定如何转换函数的base::attr功能,以便我可以访问onRender()JavaScript 代码中单击的六边形 bin 中出现的适当数据点行。我确实将该pS$data对象包含在onRender()JavaScript 代码中,但仍然卡住了。

任何建议将不胜感激!

0 投票
1 回答
704 浏览

javascript - 以交互方式将点添加到 plotly R 而不重绘背景图

下面的 MWE 过于简化了我的真正目标。但是,我有一个需要很长时间才能绘制的背景图像(在下面的示例中,它只是 32 个 mtcars 数据集值的散点图)。用户可以从我的背景图像中单击某些点,这将导致绘制新点。我的目标是在顶层简单地重新绘制这些新点,而无需重新绘制背景图像散点图以节省时间和计算量。

我的 MWE 如下:

当用户单击 32 个黑点中的任何一个时,会在单击的黑点周围绘制四个彩色红点。这主要是有效的,如果您单击 32 个黑色数据点中的任何一个,您应该会看到围绕它绘制的四个彩色点。然而,我留下了几个问题:

1)如何改进这一点,使四个彩色点不被线连接?

2) 我怎样才能使 size 和 fillColor 真正起作用?当我改变它们的值时,我看不到它们有效果。

3) 这是使交互和绘图尽可能快的合理语法吗?我很确定背景图像没有以有效的方式重绘,但我很想听到关于它的确认,因为我是这种语法的新手。如果我要在顶层添加 100 个新点,这仍然有效吗?如果没有,我将很高兴听到有关改进语法的建议。

谢谢你。

0 投票
0 回答
1923 浏览

r - 串扰和情节 - ggplotly 不接受共享数据对象

我一直在尝试通过使用 plotly 和 crosstalk 库来生成交互式链接可视化。如下例所示,似乎可以将 ggplot2 传递给“共享数据框”对象,以便使用 ggplotly() 生成链接视图。当我尝试运行此示例时,我收到以下错误消息:

错误:ggplot2 不知道如何处理 SharedData/R6 类的数据

我正在使用当前版本的 ggplot2 (v. 2.2.1) 和 plotly (4.5.6)。什么会导致此错误?

谢谢帮助!

我试图复制的例子可以在这里找到:

https://cpsievert.github.io/plotly_book/linking-animated-views.html

0 投票
1 回答
87 浏览

javascript - 在 JavaScript 中检索 R 对象属性 - 第 2 部分

我之前发布了一个类似的问题(在 JavaScript 中检索 R 对象属性)。在较早的帖子中,我过度简化了我的 MWE,因此不幸的是,我得到的答案并不真正适用于我的真正问题。在这里,我展示了为什么我可能需要在 JavaScript 中检索 R 对象属性(除非有另一个我不知道的选项)。

我有一个包含 100 个观察值的 5 变量数据集。我使用六边形分箱并创建了一个散点图矩阵。10 个散点图中的每一个都有 12-18 个六边形。为了保存所有 10 个散点图的每个六边形箱中的 100 个观察值的行,我使用了 R 中的 base::attr 函数。在下面的代码中,这是在以下位置完成的:

我正在尝试创建六边形分箱的交互式 R Plotly 对象,以便如果用户单击给定的六边形箱(无论哪个散点图),他们将获得分组到该箱中的 100 个观察值的行。我已经完成了这个目标的一部分。我的 MWE 如下:

这将创建一个图像,如下所示:

六边形分箱的散点图矩阵

例如,如果我点击绿色框中突出显示的六边形,我可以确定它出现在哪个子图中(“myX”和“myY”),点击的六边形的 ID(“hexID”),以及被分箱到该六边形中的观察点(“计数”)。对于这个特定的六边形,myX=5,myY=2,hexID=39,counts=1。因此,用户只需在第五行和第二列的散点图中单击 ID39 的六边形,应该有 1 个数据点被分箱。

如果我离开 onRender() 函数,只需在 R 中输入以下代码:

然后,我可以获得数据框的行,该行包含被合并到单击的六边形中的一个观察值:

我的问题只是在最后一步。我无法弄清楚如何从 onRender() 函数中使用 base::attr() 函数来获取“obsns”对象。这个问题是否有任何解决方法,或者我应该考虑采取其他可能的方法?感谢您的任何想法/建议!

0 投票
1 回答
3136 浏览

r - 看起来源/目标不是零索引networkd3

我正在尝试使用网络流量数据制作交互式网站流量图。

使用包。

它只是一个simpleNetwork,但是一直给我这个警告并强制关闭 RStudio。

警告消息:看起来源/目标不是零索引。这在 Javascript 中是必需的,因此您的绘图可能无法呈现。

我已经研究了几个小时,但似乎无法找到它是如何工作的。我知道 javascript 从 0 读取,r 从 1 读取。这就是它不起作用的原因。

0 投票
1 回答
610 浏览

r - 将 fig.width 传递给 taglist

rgl部件响应 knitr 代码块选项中指定的图形宽度和高度,例如

给出了一个小图(我的屏幕上为 192 x 192)。但是,如果我放入rglwidget()一个可浏览的 tagList,它不会:

这给出了两个全尺寸的小部件。调试 Javascript 显示每个都被初始化为 960 x 500,而不是第一个示例中的 192 x 192。

有没有办法说我希望将宽度和高度值传递到 tagList 中的小部件中?

PS这无关rgl; leaflet有同样的问题

0 投票
1 回答
229 浏览

r - 在 R 中的 onRender() htmlWidgets 上更改散点图中的线条粗细和不透明度

我希望使用 R 包 htmlwidgets 的 onRender() 函数制作一个绘图,用户可以在其中单击一个点并绘制一条线。我现在的关键是在它的默认厚度和可能的默认不透明度处绘制一条灰线。

然而,我一直坚持改变线条的粗细(并且可能改变线条的不透明度,尽管它可能正在工作并且我看不到它,因为线条太细了)。我希望线条非常粗且相当透明。我尝试了几种线宽和不透明度的参数和方法(其中一些在下面注释掉了),但似乎它们并没有什么区别。有什么想法我可能会错过吗?谢谢你。

0 投票
1 回答
39 浏览

r - Removing old trace and adding new trace each time user interacts in onRender htmlwidgets

I am aiming to make a plot using the onRender() function in the R package htmlwidgets in which a user can click on a point and a thick gray semi-transparent line is drawn. I have the crux of it working as is shown in the code below.

The problem that remains for me is the fact that each time the user clicks on a point, the opacity of the gray line becomes darker and darker. That is not my intention. In my real application, the gray line line thickness can change as well depending on other parameters not included in this MWE. As a result, in terms of this MWE, I would like to determine a way that, when a user clicks on a point, the old gray line is removed and the new gray line is drawn.

After some research, it seems that my best bet is to use the Plotly.deleteTraces(el.id, trace1) function. I tried to insert that command in two places (currently commented out in the code above). However, in both of these locations, that command seems to prevent any gray line from being drawn. I am unsure why this is happening and how to resolve it. As a result, any information on why this happens and how I can resolve it would be very helpful! Thank you.

Note: This is somewhat of a continuation of a previous post (Changing line thickness and opacity in scatterplot on onRender() htmlWidgets in R).

0 投票
1 回答
996 浏览

r - RadialNetwork()的R networkD3颜色节点笔划

包包含一些非常好的函数,用于创建明显的网络。不同的函数有不同的参数,所以这个问题专门针对radialNetwork() 函数。是否可以根据给定的向量使节点笔划的颜色发生变化?

下面的代码创建了一个网络图,但将节点笔划的参数更改为包含不同颜色的向量不起作用。

在此处输入图像描述

0 投票
1 回答
221 浏览

r - Line width related to x and y values (rather than pixel size) - htmlwidgets plotly R

I am aiming to make a plot using the onRender() function from the htmlwidgets package in which a user can click on a point and a gray line is drawn. I am trying to make the thickness of the gray line set to a variable.

I have a tentative MWE working as shown below. However, I currently have a problem. Basically, the width of the line changes depending on the resizing of the image and/or browser. This creates a problem because some of the dots change from being inside or outside the gray line depending on the changing line width (as a result of the image or browser resizing). However, whether the dots are inside or outside the gray line has a meaning that I need to keep consistent.

As a result, I am trying to designate the thickness of the line as a value that is relative to the x and y axis values (and not to the pixel values on the screen that can change size). I am hoping this would create a consistent gray line width that does not change relative to the dots even if the user resized the image or browser. Is this something can be accomplished in the onRender() function?

Note: This question is an additional tweak to a previous question I asked (Changing line thickness and opacity in scatterplot on onRender() htmlWidgets in R).