R shiny app - 在 DT 中编辑值并更新
R shiny app - Edit value in DT and update
我有一个闪亮的应用程序,其中一项功能是允许用户编辑 table 中的值,当单击 运行 时,它将使用用户输入作为值函数,然后在相同的 table 中更新结果。下面是当前 table 和预期 table 的示例。因此,在第一个 table 中,如果用户更改通道 A 和 C 的电流值并单击 运行,它应该将自身更新为 table 预期输出中反映的值。
所以我的问题是,是否可以将 editable DT 值作为函数的输入。
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
actionButton("opt_run", "Run"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
df <- data.table(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
expdf <- data.table(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
})
}
)
我不确定您是否希望将它们全局存储。我会给你一个全球版本,这样你就可以把它保存在某个地方,数据库或磁盘上:
您可以使用 input$x1_cell_edit
访问单元格值,请注意,我正在按 F5
刷新页面以检查这些值是否已保存
library(shiny)
library(DT)
options(stringsAsFactors = F)
df <- data.frame(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
expdf <- data.frame(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
observeEvent(input$x1_cell_edit, {
df[input$x1_cell_edit$row,input$x1_cell_edit$col] <<- input$x1_cell_edit$value
})
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
observeEvent(input$x2_cell_edit, {
expdf[input$x2_cell_edit$row,input$x2_cell_edit$col] <<- input$x2_cell_edit$value
})
})
}
)
我有一个闪亮的应用程序,其中一项功能是允许用户编辑 table 中的值,当单击 运行 时,它将使用用户输入作为值函数,然后在相同的 table 中更新结果。下面是当前 table 和预期 table 的示例。因此,在第一个 table 中,如果用户更改通道 A 和 C 的电流值并单击 运行,它应该将自身更新为 table 预期输出中反映的值。
所以我的问题是,是否可以将 editable DT 值作为函数的输入。
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
actionButton("opt_run", "Run"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
df <- data.table(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
expdf <- data.table(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
})
}
)
我不确定您是否希望将它们全局存储。我会给你一个全球版本,这样你就可以把它保存在某个地方,数据库或磁盘上:
您可以使用 input$x1_cell_edit
访问单元格值,请注意,我正在按 F5
刷新页面以检查这些值是否已保存
library(shiny)
library(DT)
options(stringsAsFactors = F)
df <- data.frame(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
expdf <- data.frame(Channel = c("A", "B","C"),
Current = c("3000", "3000","5000"),
Modified = c("3500", "3500","6000"),
New_Membership = c("650", "650","1100"))
shinyApp(
ui = fluidPage(
DT::dataTableOutput("x1"),
tags$h1("Expected Output"),
DT::dataTableOutput("x2")
),
server = function(input, output, session) {
output$x1 = renderDT(df, selection = 'none', editable = TRUE)
observeEvent(input$x1_cell_edit, {
df[input$x1_cell_edit$row,input$x1_cell_edit$col] <<- input$x1_cell_edit$value
})
output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
observeEvent(input$x2_cell_edit, {
expdf[input$x2_cell_edit$row,input$x2_cell_edit$col] <<- input$x2_cell_edit$value
})
})
}
)