【问题标题】:Unique row DT::renderDataTable() in Shiny R trigger queryShiny R 触发器查询中的唯一行 DT::renderDataTable()
【发布时间】:2018-01-31 23:51:33
【问题描述】:

您好,我是闪亮和数据反应性的新手。我希望您能提供帮助,我在观察函数中嵌入了一个查询,该查询响应来自 DT::renderDataTable() 中唯一行选择的输入。当用户选择行时,向量会增加,例如选择第 1 行,然后选择第 8 行,返回的向量是 [1] 1 8。然后这些值通过子集例程输入到查询中,以获取作为文本字符串的 id。即与 id "ch10" 相关的字符 8。 postgres 查询一次只能取 1 个值,但是我想在绘制所有内容之前将所有选择 rbind 放入数据框中。我可以用 for 循环来做到这一点,但这不是很有效而且很耗时。此外,每次它迭代和更新绘图时,它都会再次循环遍历所有向量并重新绘制所有选择以及新添加的内容。我需要做的是将新的向量行添加到数据框中并验证 id 是否已经在数据框中,如果已经存在则拒绝,或者如果它们不存在则添加然后绘图,这可以在观察函数中完成吗?任何帮助将不胜感激。这是观察函数的主要代码:

       ids <- input$x3_rows_selected
       dat1 =  isolate(paste0(dat[ids, , drop = FALSE][[1]], sep = ""))

    if (dat1[1]=='')
       return(NULL)

      result = list()
    for (i in 1:length(dat1)){
      dat2           = dbGetQuery(con2, paste0("select * from    getsession('",dat1[i],"'),  sep=""))
      dat2$sessionid = dat1[i]
      result[[i]]    = dat2

    }

    big_data = do.call(rbind, result)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是一个让您走上正轨的潜在解决方案。我稍微简化了它,使用selectizeInput 进行行选择,并使用简单的子集语句而不是 sql 查询,但想法/实现非常相似。

    基本上可以加两个reactiveVals;一个用于选定的行,一个用于表。然后,您可以创建一个 observeEvent 以在输入发生变化时进行调整。正如您在此解决方案中看到的那样,行仅在被选中时获取一次,如果进行了新选择,则observeEvent 会添加新行。这使我们不必经常通过 for 循环。

    library(shiny)
    
    mtcars$id = seq(nrow(mtcars)) # add unique id
    
    ui <- fluidPage(
      selectizeInput('input1','Select rows:',choices=seq(nrow(mtcars)),multiple=T),
      tableOutput('result')
    )
    
    server <- function(input,output)
    {
      # initiate table with right column headers
      my_table <- reactiveVal(mtcars[0,]) 
      # initiate out selection
      selection <- reactiveVal()  
    
      observeEvent(input$input1, ignoreNULL = F,
                   {
                     # if any from input1 not yet in our selection
                     if(any(!input$input1 %in% selection()))
                     {
                       to_add = input$input1[!input$input1 %in% selection()] # which id to add?
                       selection(c(selection(),to_add)) # update the reactiveVal
                       df_to_add = mtcars[mtcars$id==to_add,] # your sql query here
                       my_table(rbind(my_table(),df_to_add)) # update the table reactiveVal
                     }
                     else
                     {
                       # if there are rows selected that the user has deselected ...
                       if(any(!selection() %in% input$input1 ))
                       {
                         selection(input$input1) # set selection to the user input
                         # remove the deselected rows from the table
                         my_table(my_table()[my_table()$id %in% input$input1,] ) 
                       }
                     }
                   })
    
      output$result <- renderTable({my_table()})
    
    }
    shinyApp(ui,server)
    

    希望这会有所帮助!

    【讨论】:

    • 感谢 Florian,此代码在我的示例中效果更好。我可以在 observeEvent 进程中嵌入一个传单代理还是最好放在一个观察函数中?
    • 嗨@emperorfish,您可以为此使用observeEvent。事实上documentation page 甚至使用observeEvent 来说明它的工作原理。如果此答案解决了您的问题,请accept the answer 将您的问题标记为已解决?谢谢!
    猜你喜欢
    • 2015-09-13
    • 2018-08-11
    • 2018-10-07
    • 2015-10-07
    • 2018-08-07
    • 2016-10-18
    • 2018-10-08
    • 2016-01-02
    • 2019-05-08
    相关资源
    最近更新 更多