(闪亮)修改用户过滤的表并看到它反映在主表中

Posted

技术标签:

【中文标题】(闪亮)修改用户过滤的表并看到它反映在主表中【英文标题】:(Shiny) Modify table filtered by the user and see it reflected in the main table 【发布时间】:2021-11-17 07:39:39 【问题描述】:

我依靠以下Code 以便用户可以修改闪亮的表。 我有一个很大的数据库,在用户修改表之前我希望他能够通过“材料”进行过滤,然后他可以进行相应的修改,尤其是“stockobj”列,然后我希望看到过滤后的表以及带有先前所做修改的主表。 如图所示:[App][https://i.stack.imgur.com/2ecpK.png]

我的代码:

library(DT)

df<- tibble(material=c(12345,12345,12345,12345,12345, 67891,67891,67891,67891,67891),
            centro=c("H01", "H02", "H03", "H04","H05","H01", "H02", "H03",  "H04","H05" ),
            rotaSem= c(0.66,0.55,0.43,0.45, 0.33, 0.34,0.78, 0.31,0.89,0.87),
            stockobj=c(1,2,1,1,3,1,1,1,2,1))


shinyApp(
  ui = fluidPage(
    titlePanel("My app"),
    selectInput("mate", "Select material", choices = unique(df$material)),
    h3("Edit table"),
    DTOutput('x1'),
    h3("Main table"),
    DTOutput("x2")
  ),
  server = function(input, output, session) 
    x = df

  
    categ<-reactive(
      x %>% filter(material==input$mate)
    )
    
    output$x1 <- renderDT(categ(), selection = 'none', rownames = F, editable = T)
    output$x2 <- renderDT(
      x
    )
    
    
    proxy = dataTableProxy('x1')
    
    observeEvent(input$x1_cell_edit, 
      
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col + 1  # column index offset by 1
      v = info$value
      x[i, j] <<- DT::coerceValue(v, x[i, j])
      replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
    )
  
)```

I have been looking for a solution for several days but I have not been able to. The problem is that to modify the table I have to modify the "reactive" and I don't think it is possible. Any ideas?


  [1]: https://i.stack.imgur.com/2ecpK.png

【问题讨论】:

请使用reactiveValues 对象而不是反应对象。 【参考方案1】:

试试这个

library(DT)
library(dplyr)

df<- data.frame(material=c(12345,12345,12345,12345,12345, 67891,67891,67891,67891,67891),
            centro=c("H01", "H02", "H03", "H04","H05","H01", "H02", "H03",  "H04","H05" ),
            rotaSem= c(0.66,0.55,0.43,0.45, 0.33, 0.34,0.78, 0.31,0.89,0.87),
            stockobj=c(1,2,1,1,3,1,1,1,2,1))

shinyApp(
  ui = fluidPage(
    titlePanel("My app"),
    selectInput("mate", "Select material", choices = unique(df$material)),
    h3("Edit table"),
    DTOutput('x1'),
    h3("Main table"),
    DTOutput("x2")
  ),
  server = function(input, output, session) 
    rv <- reactiveValues(df=df,dfa=NULL,dfb=NULL)

    observeEvent(input$mate, 
      rv$dfa <- rv$df %>% dplyr::filter(material %in% input$mate)
      rv$dfb <- rv$df %>% dplyr::filter(!(material %in% input$mate))
    )
    
    observe(
      rv$df <- rbind(rv$dfa,rv$dfb)
      df1 <- rv$df
      newchoices <- unique(df1$material)
      selected <- input$mate
      updateSelectInput(inputId = 'mate', choices=newchoices, selected=selected)
    )

    output$x1 <- renderDT(rv$dfa, selection = 'none', rownames = F, editable = T)
    output$x2 <- renderDT( rv$df )

    observeEvent(input$x1_cell_edit, 

      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col + 1  # column index offset by 1
      v = info$value
      rv$dfa[i, j] <<- DT::coerceValue(v, rv$dfa[i, j])
    )
  
)

【讨论】:

嗨,当我运行脚本时出现以下错误:``` DT::coerceValue(v, rv$df[i, j]) 中的警告:不支持数据类型: tbl_df、tbl、data.frame 警告:错误:分配的数据DT::coerceValue(v, rv$df[i, j]) 必须与现有数据兼容。 ``` 我使用的是 DT 版本 0.19,我没有收到任何错误。我将发布输出的图像。 更新我的 DT 版本,现在它可以工作了。但修改后的值仅显示在 EDIT TABLE 中,我希望它也能在下表(MAIN TABLE)中更新。有什么想法吗? 请尝试更新后的代码。 它可以工作,但是当我更改上表中的过滤器时,下表中的值会重置为原始值。

以上是关于(闪亮)修改用户过滤的表并看到它反映在主表中的主要内容,如果未能解决你的问题,请参考以下文章

小议Oracle外键约束修改行为(一)

将数据添加到主表上的多个记录的链接表中

RestKit 多对多关系在连接表中保存新行,在主表中保存空值

多个分区子查询的性能

使用子选择选择 FROM 表并修改生成的表名

如何在 SQL Azure 中设置外键?