I am using a query to build a report in R shiny and then storing that result in a data frame.
data1 <- dbGetQuery(q_r,sql)
Then I use this dataframe to create new columns on the fly (these columns do not exist in the table I'm querying from since I only have read access and cannot write back to the database)
data1['R/Y/G'] <- " "
data1['R'] <- " "
data1['Y'] <- " "
data1['G'] <- " "
data1['tcolor'] <- " "
Then I save it to a local rds file
saveRDS(data1, 'data.rds')
Then I create an editable Datatable (https://yihui.shinyapps.io/DT-edit/) and save it back to the same rds file after it has been edited.
The thing is, whenever the query reruns, the values of the columns created using R ('R/Y/G', 'R', 'Y', 'G' and 'tcolor') lose all their values. How do I make sure that even after the query reruns, the columns created using R('R/Y/G', 'R', 'Y', 'G' and 'tcolor') retain their values? Do I use multiple files?
Here's the rest of the code :
dt_output = function(title, id) {
fluidRow(column(
12, h1(paste0(title)),
hr(), DTOutput(id)
))
}
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
renderDT(data,selection = 'none', server = server, editable = editable, ...)
}
ui = fluidPage(
downloadButton("mcp_csv", "Download as CSV", class="but"),
dt_output('Report', 'x9')
)
server = function(input, output, session) {
d1 = readRDS('data.rds')
d9 = d1
rv <- reactiveValues()
observe({
rv$d9 <- d9
})
dt_d9=datatable(isolate(d9), editable = 'cell', rownames = FALSE, extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = I('colvis'))) %>% formatStyle(
'R/Y/G', 'tcolor',
backgroundColor = styleEqual(c(0,1,2), c('green', 'yellow', 'red')),fontWeight = 'bold'
)
output$x9 = render_dt(dt_d9)
proxy = dataTableProxy('x9')
observe({
DT::replaceData(proxy, rv$d9, rownames = FALSE, resetPaging = FALSE)
})
observeEvent(input$x9_cell_edit, {
rv$d9 <<- editData(rv$d9, input$x9_cell_edit, 'x9', rownames = FALSE)
d9 <- rv$d9
d9$tcolor <- ifelse(d9$R > 2500000, 2,
ifelse(d9$Y > 2000000 & d9$Y <= 2500000, 1,
ifelse(d9$G <= 2000000, 0)))
rv$d9 <<- d9
saveRDS(d9, 'data.rds')
})
Thanks