更新Shiny中的子集数据表

克里斯

我的Shiny-app加载的数据在使用前必须经过验证和纠正。

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

编辑如何保留,并传播到第二个选项卡?

MWE:

## 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
server <- function(input, output) {
  filterData <- reactive({
    indata.dt[Category==input$selectedCategory, list(Item, Count)]
  })
  output$table1 <- DT::renderDataTable({
    DT::datatable(filterData(), selection="single", rownames=FALSE, editable=list(target="cell"))
  })
  output$table2 <- DT::renderDataTable({
    DT::datatable(filterData(), selection="single", rownames=FALSE)
  })
  observeEvent(input$table1_cell_edit, {
    cell <- input$table1_cell_edit
    indata.dt[cell$row, cell$col] <- cell$value
  })
}
# Run
shinyApp(ui = ui, server = server)

通过编辑水果盘点,选择蔬菜,然后再次水果,可以观察到问题。新计数返回到原始值。

猪排

这应该可以,我还添加了一个通知,使其为int因为您正在与data.table我们一起工作,所以我们需要小心如何为它分配变量,所以我:=改用了

## 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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章