Reputation: 1089
I have built a shiny App which allow user to update datatable whith a form. I have some trouble with a feature which allow user to delete a row in the datable by clicking on a actionLink into the rendered datatable.
It works properly but I manage some bug. When all the datatable where deleted once, and I put new entries the first new entries is non-deletable without deleted another row first.
To be clear here is the step to show the bug:
I don't understand why, I think it's come from the reactive values but I put observ event on the only two event possible to appear.
Here is a reproducible example to see the bug:
library(shiny)
library(DT)
library(shinydashboard)
library(shinyjs)
# ----- function which create the button into the table
shinyInput <- function(FUN, len, id, ...) {
inputs <- len
for (i in seq(len)) {
inputs[i] <- as.character(FUN(paste0(id, len[i]), ...))
}
inputs
}
# ----- character form vector
fields<-c("text")
ui<-shinyUI(bootstrapPage(
shinyjs::useShinyjs(),
title = "Update form",
fluidRow(
sidebarPanel(width=2,
title = "Submit form", id = "submitTab", value = "submitTab",
textInput("text", "Text Input", ""),
actionButton("submit", "Add", class = "btn-primary",icon=icon("table"))
# verbatimTextOutput("test")
),
mainPanel(dataTableOutput("data_table")))
))
server<-shinyServer(function(input, output) {
# ----- create the reactive value
v<-reactiveValues(data=NULL)
# ----- when Add button is clicked
observeEvent(input$submit, {
dat <- sapply(fields, function(x) input[[x]])
dat<-data.frame(t(dat),stringsAsFactors=F)
if(!(is.null(v$data)) && (input$text%in%v$data$text==F)) {
v$data <- rbind(v$data[,-2], dat)
} else if(!is.null(v$data) && (input$text%in%v$data$text==T)) {
indice<-which(v$data$text==input$text)
v$data[indice,-2] <- dat
} else {
v$data<-dat
}
v$data<-data.frame(v$data[,-2],Delete = shinyInput(actionLink, rownames(v$data), 'button_',class="btn btn-delete",icon=icon("minus-circle"),label="",onclick = 'Shiny.onInputChange(\"select_button\", this.id)' ))
})
# ----- When Delete table button is clicked
observeEvent(input$select_button, {
# dat<-v$data
selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
# dat <- dat[rownames(dat) != selectedRow, ]
v$data<-v$data[rownames(v$data)!=selectedRow,]
v$data<-data.frame(v$data[,-2],Delete = shinyInput(actionLink, rownames(v$data), 'button_',class="btn btn-delete",icon=icon("minus-circle"),label="",onclick = 'Shiny.onInputChange(\"select_button\", this.id)' ))
})
# ----- Render the data table
output$data_table <- renderDataTable(server = TRUE,escape=F,extensions = 'TableTools',options = list(pageLength = 10),{
if (is.null(v$data)) return()
v$data
})
})
shinyApp(ui,server)
Upvotes: 1
Views: 1477
Reputation: 13856
Hi I think the issue for step 4 is that the value of input$select_button
have not changed, pasting the time at this.id
seems to fixed it. Look at the code below (I've made some other change) :
library(shiny)
library(DT)
library(shinydashboard)
library(shinyjs)
# ----- function which create the button into the table
shinyInput <- function(FUN, len, id, ...) {
inputs <- len
for (i in seq(len)) {
inputs[i] <- as.character(FUN(paste0(id, len[i]), ...))
}
inputs
}
# ----- character form vector
fields<-c("text")
ui<-shinyUI(bootstrapPage(
shinyjs::useShinyjs(),
title = "Update form",
fluidRow(
sidebarPanel(width=2,
title = "Submit form", id = "submitTab", value = "submitTab",
textInput("text", "Text Input", ""),
actionButton("submit", "Add", class = "btn-primary",icon=icon("table"))
# verbatimTextOutput("test")
),
mainPanel(dataTableOutput("data_table"), verbatimTextOutput("test")))
))
server<-shinyServer(function(input, output) {
# ----- create the reactive value
v<-reactiveValues(data=NULL)
# ----- when Add button is clicked
observeEvent(input$submit, {
dat <- sapply(fields, function(x) input[[x]])
dat<-data.frame(V1 = dat,stringsAsFactors=F)
if(!(is.null(v$data)) && (!input$text %in% v$data$text)) {
v$data <- rbind(data.frame(V1 = as.character(v$data[,1])), dat)
rownames(v$data) <- seq_len(nrow(v$data))
} else if(!is.null(v$data) && (input$text %in% v$data$text)) {
indice<-which(v$data$text==input$text)
v$data[indice,-2] <- dat
} else {
v$data<-dat
}
v$data<-data.frame(V1 = v$data[,-2],
Delete = shinyInput(actionLink,
rownames(v$data),
'button_',
class="btn btn-delete",
icon=icon("minus-circle"),
label="",
onclick = 'Shiny.onInputChange(\"select_button\", (this.id + \"@\" + Date()))' ))
})
# ----- When Delete table button is clicked
observeEvent(input$select_button, {
# dat<-v$data
input_button <- gsub(pattern = "@.*", replacement = "", x = input$select_button)
selectedRow <- as.numeric(strsplit(input_button, "_")[[1]][2])
# dat <- dat[rownames(dat) != selectedRow, ]
v$data <- v$data[!rownames(v$data) %in% selectedRow,]
if (nrow(v$data) > 0) {
v$data<-data.frame(V1 = v$data[,-2],
Delete = shinyInput(actionLink,
rownames(v$data),
'button_',
class="btn btn-delete",
icon=icon("minus-circle"),
label="",
onclick = 'Shiny.onInputChange(\"select_button\", (this.id + \"@\" + Date()))' ))
}
})
output$test <- renderPrint({input$select_button})
# ----- Render the data table
output$data_table <- renderDataTable(server = TRUE,escape=F,extensions = 'TableTools',options = list(pageLength = 10),{
if (is.null(v$data)) return()
v$data
})
})
shinyApp(ui,server)
Upvotes: 2