ドキュメントでplotly
グラフを変更しようとしています。これは原則として問題なく動作しますが、ドロップダウンを起動時に最初の要素に設定したい. ただし、この変更はグラフを更新しません。ページ全体がレンダリングされたら同じコードを実行するか、遅延を使用すると機能します。crosstalk
Rmarkdown
JavaScript
したがって、更新JavaScript
が実行された時点では、plotly
グラフはまだレンダリングされていないと思います。解決策はsetTimeout
機能しますが、これはハックだと感じます。plotly/crosstalk
では、入力が変更されたことを通知する標準的な方法は何ですか?
gif では、起動時にプロットが完全な範囲を示していることがわかりますが、それに応じてグラフを変更してb
からa
更新した後でのみです。グラフが起動時にすでに正しくフィルター処理されていることを望みます。
コード
---
title: "Delay Plotly"
date: "1 2 2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
```
```{r libs}
library(crosstalk)
library(plotly)
```
```{r gen-data}
set.seed(1)
mdat <- data.frame(x = rep(LETTERS[1:6], 6),
y = sample(100, 36, TRUE),
fill = factor(rep(rep(1:3, each = 6), 2)),
grp = rep(letters[1:2], each = 18),
id = paste0("id", 1:36),
stringsAsFactors = FALSE)
sd <- SharedData$new(mdat, key = ~ id)
```
```{js}
$(function() {
// set dropdown to first element
// setTimeout(function() { // uncomment for workaround
$('.selectized').each(function(idx, el) {
const sel = this.selectize;
const options = Object.keys(sel.options);
sel.setValue(options[0]);
});
// }, 1000); // uncomment for workaround
})
```
```{r}
bscols(
filter_select("grp",
"Group:",
sd,
~ grp, multiple = FALSE),
plot_ly(sd) %>%
add_trace(x = ~ x, y = ~ y, type = "bar", color = ~ fill,
marker = list(line = list(color = "white", width = 1)) ## add border to see the 2 groups
) %>%
layout(barmode = "stack"))
```