[英]Get clicked DT indices in shiny app using flexdashboard
I would like to replicate the server side table section here: https://yihui.shinyapps.io/DT-rows/ .我想在这里复制服务器端表部分: https://yihui.shinyapps.io/DT-rows/ 。 I am using flexdashboard
though, and I'm not sure how to achieve the same results.虽然我正在使用flexdashboard
,但我不确定如何获得相同的结果。
I would like to know the row number of the clicked DT row so that I can create a graph displaying the information in that row.我想知道单击的 DT 行的行号,以便我可以创建一个图表来显示该行中的信息。
Below is an example of what I would like to do, but using the mtcars
data set.下面是我想做的一个例子,但是使用了mtcars
数据集。 The first row of data is plotted in the example, but I would like this to change based on the row that is selected.示例中绘制了第一行数据,但我希望根据所选行进行更改。 I am comfortable with reactives, I just need some help on how to capture the row number of the selected row in the DT!我对反应式很满意,我只需要一些关于如何捕获 DT 中所选行的行号的帮助!
---
title: "test"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
theme: bootstrap
---
```{r global, include = FALSE}
library(shiny)
library(flexdashboard)
library(tidyverse)
library(DT)
```
Column
-------
### Table
```{r}
renderDT(
head(mtcars, 10),
rownames = FALSE,
filter='top',
class = 'cell-border stripe',
escape = FALSE,
options = list(
pageLength = 25,
paging = FALSE,
searchHighlight = TRUE,
autoWidth = FALSE,
scroller = TRUE,
scrollX = TRUE,
scrollY = "700px",
fixedColumns = FALSE),
selection = 'single'
)
```
Column
---------
### Graph
```{r}
mt1 <- mtcars[1, ]
mt1 <- pivot_longer(mt1, everything())
ggplot(mt1) +
geom_col(aes(x = name, y = value))
```
You need to assign the rendered objects to output
variables, then you can access the selected rows via input$<name>_rows_selected
.您需要将渲染对象分配给output
变量,然后您可以通过input$<name>_rows_selected
访问选定的行。 For the syntax, see here .有关语法,请参见此处。
---
title: "test"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
theme: bootstrap
---
```{r global, include = FALSE}
library(shiny)
library(flexdashboard)
library(tidyverse)
library(DT)
```
Column
-------
### Table
```{r}
DTOutput("table")
output$table <- renderDT({
datatable(head(mtcars, 10),
rownames = FALSE,
filter='top',
class = 'cell-border stripe',
escape = FALSE,
options = list(
pageLength = 25,
paging = FALSE,
searchHighlight = TRUE,
autoWidth = FALSE,
scroller = TRUE,
scrollX = TRUE,
scrollY = "700px",
fixedColumns = FALSE),
selection = 'single')
})
```
Column
---------
### Graph
```{r}
mt1 <- mtcars[1, ]
mt1 <- pivot_longer(mt1, everything())
ggplot(mt1) +
geom_col(aes(x = name, y = value))
```
### Selected Rows
```{r}
renderPrint({
s = input$table_rows_selected
if (length(s)) {
cat('These rows were selected:\n\n')
cat(s, sep = ', ')
}
})
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.