简体   繁体   English

在Shiny应用程序中创建反应式ActionButton

[英]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? 如何将actionButtons反应性地添加到tableOutput?

A solution will be revolutionary in my shiny universe. 解决方案将在我闪亮的宇宙中带来革命性的变化。 thx 谢谢

Workround using DT: 使用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.. 这不是每个行都有一个actionButton,但是您可以观察行选择事件并对此进行操作...在此示例中,我只是打印行,但是您可以执行任何操作。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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