[英]Selecting a default value in an R plotly plot using a selectize box via crosstalk in R, using static html not shiny
In an Rmarkdown html document, how does one select a default value for a crosstalk::filter_select dropdown that will work with plotly plots?在 Rmarkdown html 文档中,如何为 crosstalk::filter_select 下拉列表选择一个默认值,该下拉列表将与绘图一起使用? Eg, in the example below, to have just group 'a' selected when the RMD is knitted.
例如,在下面的示例中,在编织 RMD 时仅选择组“a”。
I know that for the reprex below example using plotly buttons would be easier, but when there are more than 4-5 or so choices the plotly dropdowns/buttons take up too much room/are quite ugly.我知道对于下面的示例,使用绘图按钮会更容易,但是当有超过 4-5 个左右的选择时,绘图下拉菜单/按钮占用太多空间/非常难看。 Also hoping to avoid running a shiny server, the idea is to have everything running client side for speed purposes.
也希望避免运行闪亮的服务器,这个想法是为了速度目的让所有东西都在客户端运行。
There is a PR in crosstalk that adds a "default choice" argument to the filter_select function, but that version doesn't work with plotly ( https://github.com/rstudio/crosstalk/pull/70 ).串扰中有一个 PR 为 filter_select 函数添加了“默认选择”参数,但该版本不适用于 plotly ( https://github.com/rstudio/crosstalk/pull/70 )。 I would guess the easiest way would be to add javascript to the doc to manipulate the crosstalk object, but a few experiments haven't gotten very far yet.
我想最简单的方法是在文档中添加 javascript 来操纵串扰对象,但一些实验还没有进行得很远。
Reprex rmd: 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)
```
You can directly manipulate the selectize boxes that crosstalk filter_select
ouputs using javascript, the trick is triggering it on load like so:您可以直接操纵selectize的箱子,串扰
filter_select
使用JavaScript。OUPUTS,诀窍是触发它的负荷,象这样:
```{js}
function filter_default() {
document.getElementById("filter").getElementsByClassName("selectized")[0].selectize.setValue("a", false);
}
window.onload = filter_default;
```
Just to complement the accepted answer, which did function in the RStudio viewer in my case, but not in Chrome/Edge/IE/Firefox: the jQuery event Document.ready solved the problem ( idea from this thread )只是为了补充接受的答案,在我的例子中它在 RStudio 查看器中起作用,但在 Chrome/Edge/IE/Firefox 中不起作用:jQuery 事件 Document.ready 解决了这个问题( 来自这个线程的想法)
$(document).ready(function() {
document.getElementById("filter").getElementsByClassName("selectized")[0].selectize.setValue("a", false);
});
I can not seem to get it work.我似乎无法让它工作。
Here is a minimal example of这是一个最小的例子
https://github.com/sebapehl/crosstalk_preselect/blob/main/crosstalk_preselect.Rmd https://github.com/sebapehl/crosstalk_preselect/blob/main/crosstalk_preselect.Rmd
---
title: "crosstalk preselect"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
library(plotly)
library(crosstalk)
library(tidyverse)
data(cars)
lw <- cars
shared_lw <- SharedData$new(lw)
```
```{r}
bscols(widths = c(3, NA),
list(
filter_checkbox("dist", "dist", shared_lw, ~dist)
),
plot_ly(data = shared_lw) %>%
add_markers(x = ~dist, y = ~speed)
)
```
```{js}
function filter_default() {
document.getElementById("dist").getElementsByClassName("selectized")[0].selectize.setValue("24", false);
}
window.onload = filter_default;
```
rendering to渲染到
https://raw.githack.com/sebapehl/crosstalk_preselect/main/crosstalk_preselect.html https://raw.githack.com/sebapehl/crosstalk_preselect/main/crosstalk_preselect.html
which does not select "2".不选择“2”。 What am i doing wrong here?
我在这里做错了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.