R shiny reactable运行函数仅onclick

uqjltbpv  于 2023-07-31  发布在  React
关注(0)|答案(1)|浏览(97)
library(shiny)
library(DT)
library(reactable)

ui <- {
  fluidPage(
    fluidRow(
      div(reactableOutput("atlas_cohort_tbl"),style = "font-size:80%")
    )
  )
}

server <- function(input, output, session) {
  
  
  
  output$atlas_cohort_tbl <- renderReactable({
    df <- mtcars[1:5,1:5]
    
    reactable(df,selection = "single",
              details = function(index) {
                cat(paste("Ran for ", index, "\n"))
                # run complex algorithm at real time
                paste("Details for row:", index)},
              onClick = c("expand"),
              # Give rows a pointer cursor to indicate that they're clickable
              rowStyle = list(cursor = "pointer"))
    
    
    
 
    
  })
  
}

shinyApp(ui, server)

字符串
我有一个带有reactable表的简单应用程序。我想做的是,当一行被单击时,用详细信息展开该行,但仅当该行被单击时才运行详细信息。当前,详细信息函数对所有行都运行。我只想为被单击的行运行,因为该表将有超过2000行,详细信息函数将是复杂和耗时的。我相信这可以用我不太熟悉的JS来完成。
另外,我喜欢datatable的行选择,而不是reactable。您会看到整行都高亮显示,我可以轻松地提取行选择。
这个工作流可以用datatable完成吗?基本上单击该行,该行将扩展详细信息,这些详细信息是使用该函数实时计算的。

r9f1avp5

r9f1avp51#

我不知道你想要什么。是否要使用groupBy?是否要在行详细信息中呈现函数生成的内容?
此应用程序是否回答了您的问题?

library(shiny)
library(reactable)

ui <- fluidPage(
  reactableOutput("rtbl")
)

server <- function(input, output, session) {
  
  dat <- MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price")]
  
  output[["rtbl"]] <- renderReactable({
    reactable(
      dat, 
      groupBy = "Manufacturer",
      onClick = JS("function(rowInfo, column) {
          Shiny.setInputValue('rowClicked', rowInfo.index + 1, { priority: 'event' })
        }"
      )
    )
  })
  
  observeEvent(input[["rowClicked"]], {
    cat("Run the function for row ", input[["rowClicked"]], "\n")
  })
  
}

shinyApp(ui, server)

字符串

相关问题