为仪表板中显示的 rshiny 数据表中的每一行添加一个文本框

问题描述 投票:0回答:1

尝试在数据表的每一行中添加文本框,显示类似于按钮的闪亮仪表板,如下例所示。每行都将具有与从按钮获取的值类似的唯一值。我无法跟踪类似于按钮输入的文本。下面的示例代码摘自 处理数据表中的操作按钮

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)' ),
            TextInput = shinyInput(textInput, 5, 'text_', label = "Write" ),
            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 dt
1个回答
0
投票

您可以使用reactable和reactable.extra请参阅reactable.extra文档以获取自定义输入

使用您的示例创建了示例代码片段;

library(shiny)
library(reactable)
library(reactable.extras)

shinyApp(
  ui = fluidPage(
    reactable.extras::reactable_extras_dependency(),
    reactableOutput("react"),
    hr(),
    textOutput("button_text"),
    textOutput("text")
  ),
  server = function(input, output) {
    output$react <- renderReactable({
      # preparing the test data
      df <- data.frame(
        Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
        Motivation = c(62, 73, 3, 99, 52),
        Actions = c('Fire'),
        stringsAsFactors = FALSE,
        Text = c(""),
        row.names = 1:5
      )
      reactable(
        df,
        columns = list(
          Actions = colDef(
            cell = button_extra("button", class = "button-extra")
          ),
          Text = colDef(
            cell = text_extra(
              "text"
            )
          )
        )
      )
    })
    output$button_text <- renderText({
      req(input$button)
      values <- input$button
      paste0(
        "Button: ",
        string_list(values)
      )
    })

    output$text <- renderText({
      req(input$text)
      values <- input$text
      paste0(
        "Dropdown: ",
        string_list(values)
      )
    })
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.