R Shiny:处理数据表中的操作按钮 [英] R Shiny: Handle Action Buttons in Data Table

查看:109
本文介绍了R Shiny:处理数据表中的操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数据表的 R Shiny 应用程序.一列包含具有唯一 ID 的操作按钮.我想处理对这些按钮的点击,但不幸的是,我的事件处理代码(一个简单的打印语句)从未执行过.请参阅此独立示例 (app.R):

I have an R Shiny App with a Data Table. One column contains action buttons with a unique ID. I'd like to handle clicks on those buttons, but unfortunately, my event handling code (a simple print statement) is never executed. See this self-contained example (app.R):

library(shiny)
library(DT)

ui <- shinyUI(
    fluidPage(
        title = "DataTable with Buttons",
        fluidRow(
            column(
                width = 8,
                dataTableOutput("employees")
            )
        )
    )
)

server <- shinyServer(function(input, output) {
    df <- data.frame(
        name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
        motivation = c(62, 73, 3, 99, 52),
        stringsAsFactors = FALSE
    )
    fireButtons <- list()
    fireButtonIds <- list()
    for (r in rownames(df)) {
        id <- paste("fire_", r, sep = "")
        fireButtonIds[[r]] <- id
        button <- actionButton(id, label = "Fire")
        fireButtons[[r]] <- as.character(button)
    }
    df$actions <- fireButtons
    dt <- datatable(df, colnames = c("#", "Name", "Motivation", "Actions"))
    output$employees <- renderDataTable(dt)


    for (id in fireButtonIds) {
        # binding doesn't work
        # - is the path wrong?
        # - is it because the button is really a string, not an object?
        observeEvent(input$employees$x$data$actions[[id]], {
            print(paste("click on", i))
        })
    }
})

shinyApp(ui = ui, server = server)

我看到两个可能的问题:

I see two possible problems:

  1. 我使用的路径 (input$employees$x$data$actions[[id]]) 是错误的
  2. 我使用的路径是正确的,但是它没有指向可以实际处理的东西,即它只是一个 HTML 字符串而不是一个按钮对象.
  1. The path I'm using (input$employees$x$data$actions[[id]]) is just wrong
  2. The path I'm using is correct, but it doesn't point to something that could actually be handled, i.e. it's just a HTML string and not a button object.

或者也许有更好的方法将按钮放在数据表中......?

Or maybe there's a much better approch to put buttons inside a data table...?

推荐答案

这是否完成了您想要做的事情?

Does this accomplish what you're trying to do?

library(shiny)
library(DT)

shinyApp(
  ui <- fluidPage(
    DT::dataTableOutput("data"),
    textOutput('myText')
  ),

  server <- function(input, output) {

    myValue <- reactiveValues(employee = '')

    shinyInput <- function(FUN, len, id, ...) {
      inputs <- character(len)
      for (i in seq_len(len)) {
        inputs[i] <- as.character(FUN(paste0(id, i), ...))
      }
      inputs
    }

    df <- reactiveValues(data = data.frame(

      Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
      Motivation = c(62, 73, 3, 99, 52),
      Actions = shinyInput(actionButton, 5, 'button_', label = "Fire", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
      stringsAsFactors = FALSE,
      row.names = 1:5
    ))


    output$data <- DT::renderDataTable(
      df$data, server = FALSE, escape = FALSE, selection = 'none'
    )

    observeEvent(input$select_button, {
      selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
      myValue$employee <<- paste('click on ',df$data[selectedRow,1])
    })


    output$myText <- renderText({

      myValue$employee

    })

  }
)

这篇关于R Shiny:处理数据表中的操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆