简体   繁体   English

Shiny R触发器查询中的唯一行DT :: renderDataTable()

[英]Unique row DT::renderDataTable() in Shiny R trigger query

Hi Im new to shiny and data reactivity. 嗨,我是崭新的,具有数据响应性。 I hope you can help, I have a query embedded within an observe function that responds to input from a unique row selection in DT::renderDataTable(). 我希望您能提供帮助,我将一个查询嵌入到一个观察函数中,该函数响应DT :: renderDataTable()中唯一行选择的输入。 As the user selects rows the vector increments, for example row 1 is selected and then then row 8 the vector returned is [1] 1 8. The values then feed into a query via a subset routine to get the id as a text string. 当用户选择行时,矢量递增,例如,选择了第1行,然后选择了第8行,则返回的矢量为[1] 18。然后,这些值通过子集例程输入到查询中,以获取id作为文本字符串。 ie character 8 related to id "ch10". 即与ID“ ch10”有关的字符8。 The postgres query can only take 1 value at a time however i want to get all of the selections rbind into a dataframe before plotting all together. postgres查询一次只能取1个值,但是我想在将所有选项都绘制在一起之前将所有选择rbind放入数据帧中。 I can do this with a for loop but this isnt very efficient and is time consuming. 我可以使用for循环来执行此操作,但这并不是非常有效且耗时。 Also each time it iterates and updates the plot its looping through all of the vectors again and re-plotting all the selections plus the new additions. 同样,每次迭代并更新绘图时,都会再次遍历所有矢量,然后重新绘图所有选择以及新添加的内容。 What I need to do is add the new vector rows to the dataframe and validate if the ids are already in the dataframe, reject if already there, or add if they are not there and then plot, can this be done in an observe function? 我需要做的是将新的向量行添加到数据框中,并验证ID是否已存在于数据框中,拒绝是否已存在,或者添加它们是否已存在然后进行绘图,这可以在观察函数中完成吗? Any help would be greatly appreciated. 任何帮助将不胜感激。 This is the main piece of code from the observe function: 这是来自观察功能的主要代码:

       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)

Here is a potential solution that put you on the right track. 这是一个潜在的解决方案,可让您步入正轨。 I simplified it slightly, using a selectizeInput for the row selection, and a simple subset statement instead of the sql query, but the idea/implementation is very similar. 我使用行选择的selectizeInput和一个简单的子集语句(而不是sql查询) selectizeInput进行了略微的简化,但是思想/实现非常相似。

Basically, you can add two reactiveVals ; 基本上,您可以添加两个reactiveVals one for the selected rows, and one for the table. 一个用于所选行,另一个用于表。 You can then create an observeEvent to adjust those whenever the input changes. 然后,您可以创建一个observeEvent来在输入更改时进行调整。 As you can see in this solution, rows are only fetched once when they are selected, and new rows are added by the observeEvent if new selections are made. 如您在此解决方案中所看到的,选择行时仅将它们提取一次,并且如果进行了新选择,则observeEvent将添加新行。 This prevents us from having to got through a for loop often. 这使我们不必经常通过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)

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM