R 在数据库中更新子集数据表

R 在数据库中更新子集数据表,r,shiny,R,Shiny,我的闪亮应用程序加载的数据必须在使用前进行验证和更正 但我无法让这些变化持续下去。例如,在MWE中,将梨的数量从18更改为12不会更新data.table indata.dt 如何保持编辑并传播到第二个选项卡 MWE: ##加载库 库(数据表) 图书馆(闪亮) 图书馆(DT) ##模拟加载的数据 indata.dt这应该可以,我还添加了一个通知,因此它是一个int。因为您使用的是数据表,所以我们需要注意如何为其分配变量,所以我使用了:= ## Load libraries library(dat

我的闪亮应用程序加载的数据必须在使用前进行验证和更正

但我无法让这些变化持续下去。例如,在MWE中,将梨的数量从18更改为12不会更新data.table indata.dt

如何保持编辑并传播到第二个选项卡

MWE:

##加载库
库(数据表)
图书馆(闪亮)
图书馆(DT)
##模拟加载的数据

indata.dt这应该可以,我还添加了一个通知,因此它是一个
int
。因为您使用的是
数据表
,所以我们需要注意如何为其分配变量,所以我使用了
:=

## Load libraries
library(data.table)
library(shiny)
library(DT)
## Simulate loaded data
indata.dt <- data.table(Category=c("Fruits", "Fruits", "Fruits", "Vegetables", "Vegetables"),
                        Item=c("Apple", "Pear", "Orange", "Cucumber", "Tomato"),
                        Count=c(17L, 18L, 23L, 5L, 8L))
## UI
ui <- fluidPage(
    titlePanel("GreensApp"),
    tabsetPanel(type = "tabs",
                tabPanel("Define Items",
                         sidebarLayout(
                             sidebarPanel(
                                 selectInput(inputId="selectedCategory", label="Choose a category:",
                                             choices=sort(unique(indata.dt$Category)),
                                             multiple=FALSE
                                 )
                             ),
                             mainPanel(
                                 DT::dataTableOutput("table1")
                             )
                         )
                ),
                tabPanel("See the updated table",
                         DT::dataTableOutput("table2")
                )
    )
)

server <- function(input, output, session) {
    v <- reactiveValues()
    v$indata.dt <- indata.dt

    observeEvent(input$selectedCategory,{
        req(input$selectedCategory)
        v$indata.dt2 <- v$indata.dt[Category==input$selectedCategory, list(Item, Count)]
    })

    output$table1 <- DT::renderDataTable({
        DT::datatable(v$indata.dt2, selection="single", rownames=FALSE, editable=list(target="cell"))
    })
    output$table2 <- DT::renderDataTable({
        DT::datatable(v$indata.dt2, selection="single", rownames=FALSE)
    })
    observeEvent(input$table1_cell_edit, {
        cell <- input$table1_cell_edit
        value <- as.integer(cell$value)
        if(is.na(value)){
            value <- 0
            showNotification("Needs to be an integer, reseting to zero", duration = 5,type = 'warning')
        }

        v$indata.dt2[cell$row,Count := value]
        v$indata.dt[cell$row,Count := value]
    })
}
# Run
shinyApp(ui = ui, server = server)
##加载库
库(数据表)
图书馆(闪亮)
图书馆(DT)
##模拟加载的数据
indata.dt
## Load libraries
library(data.table)
library(shiny)
library(DT)
## Simulate loaded data
indata.dt <- data.table(Category=c("Fruits", "Fruits", "Fruits", "Vegetables", "Vegetables"),
                        Item=c("Apple", "Pear", "Orange", "Cucumber", "Tomato"),
                        Count=c(17L, 18L, 23L, 5L, 8L))
## UI
ui <- fluidPage(
    titlePanel("GreensApp"),
    tabsetPanel(type = "tabs",
                tabPanel("Define Items",
                         sidebarLayout(
                             sidebarPanel(
                                 selectInput(inputId="selectedCategory", label="Choose a category:",
                                             choices=sort(unique(indata.dt$Category)),
                                             multiple=FALSE
                                 )
                             ),
                             mainPanel(
                                 DT::dataTableOutput("table1")
                             )
                         )
                ),
                tabPanel("See the updated table",
                         DT::dataTableOutput("table2")
                )
    )
)

server <- function(input, output, session) {
    v <- reactiveValues()
    v$indata.dt <- indata.dt

    observeEvent(input$selectedCategory,{
        req(input$selectedCategory)
        v$indata.dt2 <- v$indata.dt[Category==input$selectedCategory, list(Item, Count)]
    })

    output$table1 <- DT::renderDataTable({
        DT::datatable(v$indata.dt2, selection="single", rownames=FALSE, editable=list(target="cell"))
    })
    output$table2 <- DT::renderDataTable({
        DT::datatable(v$indata.dt2, selection="single", rownames=FALSE)
    })
    observeEvent(input$table1_cell_edit, {
        cell <- input$table1_cell_edit
        value <- as.integer(cell$value)
        if(is.na(value)){
            value <- 0
            showNotification("Needs to be an integer, reseting to zero", duration = 5,type = 'warning')
        }

        v$indata.dt2[cell$row,Count := value]
        v$indata.dt[cell$row,Count := value]
    })
}
# Run
shinyApp(ui = ui, server = server)