【问题标题】:Capture rows selected from shiny table捕获从闪亮表中选择的行
【发布时间】:2021-10-13 13:49:53
【问题描述】:

有没有办法根据从第一个表中选择的行来捕获值。 现在,正在发生的事情仅基于行名称,值会弹出到第二个表中。但这些都是不正确的。请看下文

library(shiny)
library(DT)
library(dplyr)

asd <- data.frame(a = c(7,7,8,8,2,1,3,5,5,6), b = c("A","B","C","A","B","C","A","B","C","F"))
ui <- fluidPage(
  selectInput("sel","Slect",choices = asd$b),
  dataTableOutput("tab"),
  dataTableOutput("txt")
)

server <- function(input, output, session) {
  
  ## First table
  output$tab <- renderDataTable({
    asd <- asd %>% filter(b %in% input$sel)
    datatable(asd,selection = 'single')
  })
  
  
  
  ## Second table
  output$txt <- renderDataTable({
    datatable(asd[rownames(asd) == input$tab_rows_selected,])
  })
  
}

shinyApp(ui, server)

【问题讨论】:

  • 嗨@manu p,您能否在问题中同时标记rshiny 而不仅仅是shiny?谢谢。

标签: r shiny


【解决方案1】:

您的问题是您在 output$tab 中运行过滤器,而不是在 output$txt 中运行过滤器,因此索引不一样。您在选择中拉出“第二行”,因此它在 asd 中为您提供了“第二行”,但两者并不相同。

简单的解决方案:使它们相同。在您的 output$txt 中,添加一行,您用于在 output$tab 中过滤数据框的同一行

output$txt <- renderDataTable({
    asd <- asd %>% filter(b %in% input$sel)
    datatable(asd[rownames(asd) == input$tab_rows_selected,])
  })

这按预期工作。

编辑:如果您的用例更复杂,并且您需要知道在原始中选择了哪一行,则可以添加某种“ID”列并进行检查。 p>

【讨论】:

    猜你喜欢
    • 2021-05-27
    • 2017-10-03
    • 2014-09-02
    • 1970-01-01
    • 2018-10-01
    • 2018-10-17
    • 2016-10-18
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多