【问题标题】:How to excract data from edited datatable in shiny如何从闪亮的编辑数据表中提取数据
【发布时间】:2020-12-24 13:08:26
【问题描述】:

我想创建一个闪亮的应用程序,用户必须在其中编辑数据表。 有代码包含可复现的例子:


library(shiny)
library(dplyr)
library(DT)
 
 
line<-c(1,1,1,1,1)
op<-c(155,155,155,156,156)
batch<-c(1,2,3,1,2)
voile<-c(1,NA,NA,NA,NA)
depot<-c(2,NA,2,NA,NA)
 
boe<-data.frame(line,op,batch)
 
ui <- fluidPage(
   
    # Application title
    titlePanel("test dust"),
   
    actionButton("refresh", label = "refresh"),
   
    DT::dataTableOutput("mytable"),
   
    actionButton("save", label = "save"),
 
)
 
# Define server logic required to draw a histogram
server <- function(input, output) {
   
    DTdust<- eventReactive(input$refresh, {
        DTdust <-data.frame(line,op,batch,voile,depot)
    })
   
    merged<-reactive({
        merged<-merge(boe,DTdust(),all.x = TRUE)
        })
   
    mergedfiltred<-reactive({
        mergedfiltred<- filter(merged(),is.na(voile)|is.na(depot) )
    })
   
    output$mytable = DT::renderDataTable( mergedfiltred(),editable = list(target = 'cell',
    disable = list(columns = c(1:3))),selection = 'none'
    )                                                                          
}
 
# Run the application
shinyApp(ui = ui, server = server)

我希望这样工作——>

当用户点击刷新按钮时。读取 Dtdust.csv(此处为模拟),然后将其与 boe.csv(也模拟)合并为过滤器,以仅获取没有结果的行,用于 voile 和 depot col。 并显示这个合并的过滤 ino 可编辑数据表。

这部分有效。

在我想从已编辑的数据表中提取数据以对其进行一些处理之后(提取完成的行,在 dtdust 上 rbind 并保存为 dtdust.csv。但我认为没关系。) 我无法提取已编辑的数据表。 我看到了一些使用经典数据框的示例,但它不适用于响应式数据框。

我是初学者,所以如果你能对你的答案发表很多评论,我可以学习如何而不只是 ctrl+c ctrl+v 你的代码:)

谢谢

【问题讨论】:

    标签: r shiny datatables


    【解决方案1】:

    您需要定义一个reactiveValues 数据框。然后,每当通过mytable_cell_edit 修改任何单元格时,您都需要通过observeEvent 对其进行更新。更新后的数据框现在在服务器端可用,其中一部分现在打印在第二个表中。您可以使用DF1$data 进行进一步分析或子集。完整的更新代码如下。

    library(shiny)
    library(dplyr)
    library(DT)
    
    line<-c(1,1,1,1,1)
    op<-c(155,155,155,156,156)
    batch<-c(1,2,3,1,2)
    voile<-c(1,NA,NA,NA,NA)
    depot<-c(2,NA,2,NA,NA)
    
    boe<-data.frame(line,op,batch)
    
    ui <- fluidPage(
      
      # Application title
      titlePanel("test dust"),
      
      actionButton("refresh", label = "refresh"),
      
      DTOutput("mytable"), DTOutput("tb2"),
      
      actionButton("save", label = "save"),
      
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      DF1 <- reactiveValues(data=NULL)
      
      DTdust<- eventReactive(input$refresh, {
        req(input$refresh)
        DTdust <-data.frame(line,op,batch,voile,depot)
      })
      
      merged<-reactive({
        req(DTdust())
        merged<-merge(boe,DTdust(),all.x = TRUE)
      })
      
      mergedfiltred<-reactive({
        mergedfiltred <- filter(merged(),is.na(voile)|is.na(depot) )
        DF1$data <- mergedfiltred
        mergedfiltred
      })
      
      output$mytable = renderDT( 
        mergedfiltred(),
        editable = list(target = 'cell', disable = list(columns = c(1:3))), selection = 'none'
      )
      
      observeEvent(input$mytable_cell_edit, {
        info = input$mytable_cell_edit
        str(info)
        i = info$row
        j = info$col  
        v = info$value
        
        DF1$data[i, j] <<- DT::coerceValue(v, DF1$data[i, j])
      })
      
      output$tb2 <- renderDT({
        df2 <- DF1$data[,2:5]
        plen <- nrow(df2)
        datatable(df2, class = 'cell-border stripe',
                  options = list(dom = 't', pageLength = plen, initComplete = JS(
                    "function(settings, json) {",
                    "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
                    "}")))
        
      })
      
    }
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      您好,感谢您的解决方案@YBS。 在这里询问半小时后,我终于自己找到了解决方案......(我之前已经转了几个小时)。 这就是我要做的:

      
          output$x2 = DT::renderDataTable({
              req(dat$x2)
              DT::datatable(dat$x2)
          })
          dat <- reactiveValues()
         
          # update edited data
          observeEvent(input$mytable_cell_edit, {
              data_table <- dat$x2
              data_table[input$mytable_cell_edit$row, input$mytable_cell_edit$col] <- as.numeric(input$mytable_cell_edit$value)
              dat$x2 <- data_table
          })
      
      

      祝你有美好的一天

      【讨论】:

        猜你喜欢
        • 2019-08-01
        • 2019-10-25
        • 1970-01-01
        • 2020-11-05
        • 2015-05-22
        • 1970-01-01
        • 2019-09-25
        • 2020-02-23
        • 2016-11-19
        相关资源
        最近更新 更多