Sven
Sven

Reputation: 83

Creating reactive ActionButtons in a Shiny app

library(shiny)

# ui ##########################

ui <- fluidPage(

   fileInput("csv", label="",
      multiple = TRUE,
      accept = c("text/csv", ".csv")),

   tags$hr(),

   actionButton("show.tbl", "SHOW TABLE"),

   tableOutput("my_tbl")
   )


# server #########################

server <- function(input, output) {

   tbl <- data.frame(Example = LETTERS[1:10]) # will be reactive in reality

   output$my_tbl <- renderTable({
      if(input$show.tbl == 1)
         tbl
    })
 }

# app ######################

shinyApp(ui, server)

As a result I want to have this column with two buttons in each line. The first button shall filter the input file and show the resulting table. The second button shall do a complex validation algorithm.

How can I reactively add actionButtons to the tableOutput?

A solution will be revolutionary in my shiny universe. thx

Upvotes: 0

Views: 78

Answers (1)

Ika8
Ika8

Reputation: 391

Workround using DT:

library(shiny)
library(DT)

ui <- fluidPage(
    verbatimTextOutput(outputId = 'vb'),
    dataTableOutput(outputId = 'table')
)

server <- function(input, output, session) {

    yourData <- reactive({
        return(mtcars)
    })

    output$vb <- renderPrint({
        req(input$table_rows_selected, cancelOutput = TRUE)
        row_id <- input$table_rows_selected

        row_selected <- yourData()[row_id,]

        return(row_selected)
    })

    output$table <- renderDataTable({

        datatable(
            data = yourData(),
            selection = 'single'
        )
    })
}

shinyApp(ui, server)

This is not an actionButton for every row but you can observe the row selection event and do something with that... In this example I just print the row but you can do whatever you want..

Hope it helps.

Upvotes: 1

Related Questions