在 R 中编辑数据表的特定列



我正在尝试使用 editable=TRUE 在 r 中编辑 DataTable。 但是,我只想编辑表的某些特定列,而不是所有列。这可能吗? 我使用的代码是:

library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput('population_table'),
textOutput("text")
)
server <- function(input, output, session) {
data = head(iris)
y <- reactive({
input$population_table_cell_edit
data
})
output$population_table = DT::renderDataTable(data, selection = 'none', 
editable = TRUE)
proxy = dataTableProxy('population_table')
observeEvent(input$population_table_cell_edit, {
info = input$population_table_cell_edit
str(info) #print 
i = info$row
j = info$col
v = info$value
data[i, j] <<- DT::coerceValue(v, data[i, j])
replaceData(proxy, data, resetPaging = FALSE)
})
output$text <- renderText({
y()[1, 1]
})
}
shinyApp(ui,server)

也许有点晚了,但我尝试了相同的方法,并且仅通过创建 if 语句找到了解决该问题的方法,例如,如果您只想更改第 3 列:

if(j == 3){
data[i, j] <<- DT::coerceValue(v, data[i, j])
replaceData(proxy, data, resetPaging = FALSE)
}else{}

至少它对我有用,但我宁愿以不同的方式处理它。

最新更新