首页 > 解决方案 > (闪亮)修改用户过滤的表并看到它反映在主表中

问题描述

我依赖以下代码,以便用户可以修改闪亮表。我有一个很大的数据库,在用户修改表之前我希望他能够通过“材料”进行过滤,然后他可以进行相应的修改,尤其是“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

标签: rshinyshiny-serverdt

解决方案


尝试这个

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])
    })
  }
)

输出


推荐阅读