Hi!
I've never quite got to grips with using proxyTable. Although I have used it in Shiny Apps before - I find it quite hard to debug when something goes wrong.
In the code below I'm trying to create a button to incrementally increase a value by 1. I can redraw the table each time - although proxy table is better for users.
My Values are being altered as required - but replacedata() in this instance doesn't work and returns a blank output - 0 matching records. Any idea whats going on?
library(dplyr)
library(data.table)
library(dplyr)
library(DT)
values <- data.table(Country = c("England","Scotland","Wales"),Level = c(4,5,6))
ui <- fluidPage(
tags$style("#TabBU { white-space:pre; }"),
DT::dataTableOutput('TabBU')
)
server <- function(input, output) {
getPlusButton <- function(n, idS = "", lab = "Pit") {
if (stringr::str_length(idS) > 0) idS <- paste0(idS, "-")
ret <- shinyInput(actionButton, n,
'button_', label = ">>>",icon = icon("icon-plus-sign-alt"),
onclick = sprintf('Shiny.onInputChange(\"%splus_button_%s\", this.id)' ,idS, lab))
return (ret)
}
shinyInput <- function(FUN, n, id, ses, ...) {
as.character(FUN(paste0(id, n), ...))
}
values = values %>%
mutate(id = 1:nrow(values)) %>%
rowwise() %>%
mutate(Increase = getPlusButton(id, idS = "", lab = "Tab1")) %>%
select(id,everything())
output$TabBU <- renderDT(
head(values),
escape = FALSE)
proxyTable <<- dataTableProxy('TabBU')
observeEvent(input$plus_button_Tab1, {
i <- as.numeric(strsplit(input$plus_button_Tab1, "_")[[1]][2])
j = which( colnames(values)=="Level" )
v = as.numeric(values[i, j]) + 1
values[i, j] <<- DT::coerceValue(v, values[i, j])
print(values)
#Fine up until this point - then replace data breaks the datatable
replaceData(proxyTable, values, resetPaging = FALSE, rownames = FALSE) # important
})
}
shinyApp(ui, server)