r - (闪亮)修改用户过滤的表并看到它反映在主表中
问题描述
我依赖以下代码,以便用户可以修改闪亮表。我有一个很大的数据库,在用户修改表之前我希望他能够通过“材料”进行过滤,然后他可以进行相应的修改,尤其是“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
解决方案
尝试这个
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])
})
}
)
推荐阅读
- project-reactor - 如何编写函数
, 通量 > 使用 opencsv - android - Android Q暗模式将可绘制表单的颜色从白色变为灰色
- php - 如何在不重复的情况下重定向到随机 HTML 页面?
- c# - 使用 HTTPClient 取消 PayPal 订阅会返回“Result = “{Not yet computed}”
- javascript - 上传文件时重复数据谷歌应用脚本和javascript
- javascript - 试图为deckgl六边形层创建一个控制面板(半径,上百分位......)
- algorithm - 矩阵中的邻居 - 算法
- r - 使用 lfe 和 broom 包时出现错误“felm 类的对象没有整洁的方法”
- c# - 如何从字符串方法中获取异步值
- javascript - 在 Tampermonkey 中获取 @require 文件的元数据?