Fel*_*xST 5 javascript r shiny flexdashboard r-plotly
在 Rmarkdown html 文档中,如何为 crosstalk::filter_select 下拉列表选择一个默认值,该下拉列表将与绘图一起使用?例如,在下面的示例中,在编织 RMD 时仅选择组“a”。
我知道对于下面的 reprex 示例,使用 plotly 按钮会更容易,但是当有超过 4-5 个左右的选择时,plotly 下拉菜单/按钮占用太多空间/非常难看。也希望避免运行闪亮的服务器,这个想法是为了速度目的让所有东西都在客户端运行。
串扰中有一个 PR 为 filter_select 函数添加了“默认选择”参数,但该版本不适用于 plotly ( https://github.com/rstudio/crosstalk/pull/70 )。我想最简单的方法是在文档中添加 javascript 来操作串扰对象,但是一些实验还没有进行到很远。
Reprex rmd:
---
output:
html_document
---
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(plotly)
# example data
dat <- tibble::tribble(~filterBy, ~x, ~y,
"a", 1, 1,
"b", 2, 1,
"a", 1, 2,
"b", 2, 2,
"a", 1, 3,
"b", 2, 3,
"a", 1, 2,
"b", 2, 3,
"c", 3, 1,
"c", 3, 2,
"c", 3, 3
)
# initializing a crosstalk shared data object
plotdat <- highlight_key(dat)
# Filter dropdown
question_filter <- crosstalk::filter_select(
"filter", "Select a group to examine",
plotdat, ~filterBy, multiple = F
)
# Plotting:
plot <- plot_ly( plotdat,
x = ~x, y = ~y, text = ~filterBy, mode = "markers+text",
textposition = "top", hoverinfo = "x+y"
)
# Just putting things together for easy display:
shiny::tags$div(class = 'flexbox',
question_filter,
shiny::tags$br(),
plot)
```
Run Code Online (Sandbox Code Playgroud)
您可以filter_select使用 javascript直接操作串扰输出的选择框,诀窍是在加载时触发它,如下所示:
```{js}
function filter_default() {
document.getElementById("filter").getElementsByClassName("selectized")[0].selectize.setValue("a", false);
}
window.onload = filter_default;
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
942 次 |
| 最近记录: |