R语言 当用户点击表格中的单元格时,让onClcik在Shiny应用程序中工作

6jygbczu  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在尝试构建一个Shiny应用程序,并获取onClick函数来输出用户单击的单元格行/列。
如何修复代码以显示单元格行/列?
闪亮应用

library(reactable)
library(shiny)
library(htmlwidgets)

iris = iris

ui <- fluidPage(

  reactable::reactableOutput("irisTABLE"),
  textOutput("'cellDATA")
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$irisTABLE = renderReactable({
    reactable(iris,
              onClick = JS("
                                function(rowInfo, colInfo) {
                                Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.row.CapRate, { priority: 'event' })
                                }
                               ")
    )
  })

  output$cellDATA = renderText({
    paste0("The cell selected is: ", input$cell_data)
  })
}

# Run the application
shinyApp(ui = ui, server = server)
vuktfyat

vuktfyat1#

output$cellDATA更改为
if (!is.null(input$cell_data)) { row_col <- strsplit(input$cell_data, "-") paste0("The cell selected is in row ", row_col[[1]][2], " and column ", row_col[[1]][1]) }

library(reactable)
library(shiny)

iris = iris

ui <- fluidPage(
  reactable::reactableOutput("irisTABLE"),
  textOutput("cellDATA")
)

server <- function(input, output) {
  
  output$irisTABLE = renderReactable({
    reactable(iris,
              onClick = JS("
                function(rowInfo, colInfo, e) {
                  Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.index)
                }
              ")
    )
  })
  
  output$cellDATA = renderText({
    if (!is.null(input$cell_data)) {
      row_col <- strsplit(input$cell_data, "-")
      paste0("The cell selected is in row ", row_col[[1]][2], " and column ", row_col[[1]][1])
    }
  })
}

shinyApp(ui = ui, server = server)

相关问题