【发布时间】:2018-10-28 15:16:37
【问题描述】:
我想做的是在 html 中过滤后根据 (DT-) 表的输出更新绘图。
例如 - 这是在 html 中为maz 过滤的表格的屏幕截图:
我希望散点图更新为仅显示过滤表中显示的值。
这可能吗?我知道我可以使用shiny web app 来实现这样的目标,但是是否可以在 html 中嵌入一些闪亮的代码来实现这一目标? (我使用 Shiny/html 的经验非常有限,因此将不胜感激任何指针/想法)。
我正在使用 R-markdown(和 here is a link to the html produced):
---
title: "Filter interative plots from table results"
date: "`r format(Sys.time(), '%B %e, %Y')`"
output:
html_notebook:
theme: flatly
toc: yes
toc_float: yes
number_sections: true
df_print: paged
html_document:
theme: flatly
toc: yes
toc_float: yes
number_sections: true
df_print: paged
---
```{r setup, include=FALSE, cache=TRUE}
library(DT)
library(plotly)
library(stringr)
data(mtcars)
```
# Clean data
## Car names and models are now a string: "brand_model" in column 'car'
```{r include=FALSE}
mtcars$car <- rownames(mtcars)
mtcars$car <- stringr::str_replace(mtcars$car, ' ', '_')
rownames(mtcars) <- NULL
```
# Interactive table using DT
```{r rows.print=10}
DT::datatable(mtcars,
filter = list(position = "top"),
selection="none", #turn off row selection
options = list(columnDefs = list(list(visible=FALSE, targets=2)),
searchHighlight=TRUE,
pagingType= "simple",
pageLength = 10, #default length of the above options
server = TRUE, #enable server side processing for better performance
processing = FALSE)) %>%
formatStyle(columns = 'qsec',
background = styleColorBar(range(mtcars$qsec), 'lightblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
```
# Plot disp against mpg using plotly
```{r fig.width=8, fig.height=8}
p <- plot_ly(data = mtcars,
x = ~disp,
y = ~mpg,
type = 'scatter',
mode = 'markers',
text = ~paste("Car: ", car, "\n",
"Mpg: ", mpg, "\n"),
color = ~mpg,
colors = "Spectral",
size = ~-disp
)
p
```
【问题讨论】:
-
(p.s. 如果有人对我如何最好地共享 .Rmd 和 .html 文件以更好地理解问题有任何建议,我很想听听他们的意见!)
-
遗憾的是不能直接完成。问题是 DT - 过滤器不会转化为闪亮。但是您可以使用闪亮的过滤器。将在一小时内上传我的版本。
-
@5th - 这将非常有帮助!
-
好的,这里的防火墙阻止了我。那得等到我回家了。
-
原来我错了,你可以直接做。我更改了您的代码并将其上传到下面作为答案。这样看起来更容易
标签: css r shiny plotly r-plotly