使用选择框通过 R 中的串扰在 R 绘图中选择默认值,使用静态 html 不闪亮 [英] Selecting a default value in an R plotly plot using a selectize box via crosstalk in R, using static html not shiny

查看:44
本文介绍了使用选择框通过 R 中的串扰在 R 绘图中选择默认值,使用静态 html 不闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rmarkdown html 文档中,如何为可用于绘图的串扰::filter_select 下拉列表选择默认值?例如,在下面的示例中,在编织 RMD 时仅选择组a".

In an Rmarkdown html document, how does one select a default value for a crosstalk::filter_select dropdown that will work with plotly plots? E.g., in the example below, to have just group 'a' selected when the RMD is knitted.

我知道对于下面的示例,使用绘图按钮会更容易,但是当有超过 4-5 个左右的选择时,绘图下拉菜单/按钮占用太多空间/非常难看.也希望避免运行闪亮的服务器,这个想法是为了速度目的让所有东西都在客户端运行.

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. Also hoping to avoid running a shiny server, the idea is to have everything running client side for speed purposes.

串扰中有一个 PR 增加了默认选择";filter_select 函数的参数,但该版本不适用于 plotly (https://github.com/rstudio/crosstalk/pull/70).我想最简单的方法是在文档中添加 javascript 来操作串扰对象,但一些实验还没有进行得很远.

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). 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.

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)


```

推荐答案

您可以使用 javascript 直接操作串扰 filter_select 输出的选择框,技巧是在加载时触发它,如下所示:

You can directly manipulate the selectize boxes that crosstalk filter_select ouputs using javascript, the trick is triggering it on load like so:

```{js}
function filter_default() {
    document.getElementById("filter").getElementsByClassName("selectized")[0].selectize.setValue("a", false);
}
window.onload = filter_default;
```

这篇关于使用选择框通过 R 中的串扰在 R 绘图中选择默认值,使用静态 html 不闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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