Using a selected DT column as an input to a plot in a non-embedded Shiny RMD

Viewed 50

I haven't been able to figure out or find any resources for how to build a plot from a user selected column in a shiny formatted RMD. I do not want to have an embedded shiny app in the rmd unless that's the only option.

Normally, I would set an output object like DT::dataTableOutput("my_datatable")
Then take the selected column in this way input$my_datatable_columns_selected and use that to plot. However, this functionality doesn't seem to exist unless I embed a shiny app within rmd.

Is this possible?

Below is a mini rmd with a selectable table.

Thanks!


    ---
    title: "Test"
    output:
        flexdashboard::flex_dashboard:
        theme: bootstrap
        orientation: rows
    # params:
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    
    library(shiny)
    library(flexdashboard)
    #library(data.table)
    library(DT)
    library(highcharter)
    library(crosstalk)
    
    ```
    
    Info {.sidebar data-width=300}
    -----------------------------------------------------------------------
    
    Example here:
    
    Row
    -----------------------------------------------------------------------
    
    ### Plot
    
    ```{r, echo=FALSE}
    print("PLOT OUTPUT from column selection")
    ```
    
    Row 
    -----------------------------------------------------------------------
    
    ### mtcars
    
    ```{r, echo=FALSE}
    
    DT::renderDataTable(mtcars,
                        selection = list(mode = "single", target = "column"),
                        options = list(scrollX = TRUE))
    
    ```

1 Answers

You do need to declare the output, and it does work. You need to add the table for the selection to be made from, then designate the render to an object.

If you want to determine what column was selected, that's just like a Shiny app, too. (BTW, I changed the knitr option to echo=F. That's why my chunk options differ.)

### mtcars

```{r yayTables}

DT::dataTableOutput('mtTbl')

output$mtTbl = DT::renderDataTable(
  mtcars, selection = list(mode = "single", target = 'column'),
  options = list(scrollX = TRUE))

```

Column that was selected:

```{r showMe}

renderPrint(input$mtTbl_columns_selected)

```

enter image description here

Related