I'm trying to add a "remove row" button in each row of a data table. I know that there are several answered questions with this topic, but I just can't make them work with my code. I took ideas from here and here and adapted them to my code but the problem is that the buttons aren't showing in the table and I have no way to know if the rows are actually being deleted.
Here's my code and a csv file with example data
library(shiny)
library(dplyr)
library(DT)
library(shinyjs)
library(shinythemes)
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
ui <- fluidPage(
useShinyjs(),
theme = shinytheme("flatly"),
tabsetPanel(
id = "tabs",
tabPanel("Section 1",
sidebarLayout(
sidebarPanel(
titlePanel("Select Variables"),
uiOutput('fondo'),
uiOutput('reg'),
uiOutput('seguro'),
uiOutput('prod_sap'),
hr(),
actionButton("addbutton","Add")
),
mainPanel(
titlePanel("Preview"),
DT::dataTableOutput('courseTable'),
)
)
)
)
)
server <- function(input, output, session) {
fondo_edo <- reactive ({
read.csv("E:/Input Fondo-Edo-Reg_example.csv")
})
output$fondo <- renderUI({
times <- input$addbutton
fondos_todos <- as.vector(unique(fondo_edo()$FONDO))
div(id= letters[(times %% length(letters)) + 1],
selectInput("fondo_selec","Fondo:", choices=fondos_todos,selectize = T))
})
fondo_edo1 <- reactive({
subset(fondo_edo(), FONDO %in% input$fondo_selec)
})
output$reg <- renderUI({
reg_todos <- as.vector( unique(fondo_edo1()$REGIÓN) )
selectInput("reg_selec","Región:", choices=reg_todos, selectize = F)
})
output$seguro <- renderUI({
times <- input$addbutton
div(id=letters[(times %% length(letters))+1],
selectInput("seguro_selec","Seguro agricultura protegida:", choices=c("","Cosecha_Esp", "Inversión", "Planta"), selectize = F))
})
output$prod_sap <- renderUI({
times <- input$addbutton
div(id=letters[(times %% length(letters))+1],
conditionalPanel("input.seguro_selec == 'Inversión'",
selectInput("prod_sap_selec","Nombre producto SAP:", choices= "Tradicional")),
conditionalPanel("input.seguro_selec != 'Inversión'",
selectInput("prod_sap_selec2","Nombre producto SAP:",choices = c("","Establecimiento", "Mantenimiento", "Producción"), selectize = F)))
})
values <- reactiveValues()
values$df <- data.frame("Fondo" = numeric(0), "Región"= numeric(0),
"Seguro agricultura protegida"= numeric(0), " " = numeric(0), check.names = F)
proxyTable <- DT::dataTableProxy("tab")
newEntry <- observe({
if(input$addbutton > 0) {
newLine <- isolate(c(input$fondo_selec, input$reg_selec,
ifelse(input$seguro_selec=="Planta", paste0(input$seguro_selec,"/",input$prod_sap_selec2),
input$seguro_selec),
shinyInput(actionButton, input$addbutton,
'button_', label = "Remove",
onclick = sprintf('Shiny.onInputChange(\"remove_button\", this.id)'))))
isolate(values$df[nrow(values$df) + 1,] <-newLine)
}
})
observeEvent(input$remove_button, {
myTable <- values$df
s <- as.numeric(strsplit(input$remove_button, "_")[[1]][2])
myTable <- filter(myTable, row_number() != s)
replaceData(proxyTable, myTable, resetPaging = FALSE)
values$df <- myTable
})
output$courseTable <- DT::renderDataTable({values$df})
}
runApp(shinyApp(ui,server))
Thank you so much in advance