繁体   English   中英

使用 flexdashboard 在 shiny 应用程序中获取点击的 DT 索引

[英]Get clicked DT indices in shiny app using flexdashboard

我想在这里复制服务器端表部分: https://yihui.shinyapps.io/DT-rows/ 虽然我正在使用flexdashboard ,但我不确定如何获得相同的结果。

我想知道单击的 DT 行的行号,以便我可以创建一个图表来显示该行中的信息。

下面是我想做的一个例子,但是使用了mtcars数据集。 示例中绘制了第一行数据,但我希望根据所选行进行更改。 我对反应式很满意,我只需要一些关于如何捕获 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))

    ```

您需要将渲染对象分配给output变量,然后您可以通过input$<name>_rows_selected访问选定的行。 有关语法,请参见此处


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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM