Created
June 21, 2019 03:42
-
-
Save geebioso/8f6e18a54e983105c073ebda850227a5 to your computer and use it in GitHub Desktop.
Delete and auto-populate rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(shinyjs) | |
library(glue) | |
ui <- fluidPage( | |
actionButton('delete_all', label = "Delete All"), | |
actionButton('auto_populate', label = "Auto Populate"), | |
tagList( | |
shinyalert::useShinyalert(), | |
shinyjs::useShinyjs(), | |
fluidRow( | |
column(2, h5("Variable Name")) | |
), | |
tags$div(id = "RowPlaceholder") | |
) | |
) | |
server <- function(input, output, session) { | |
current_rows <- reactiveVal() | |
num <- reactiveVal(0) | |
insert_row <- function(num, varname_value = NULL) { | |
num(num()+1) | |
current_rows( c(current_rows(), num()) ) | |
row_id <- glue::glue("row_{num()}") | |
varname_id <- glue::glue("var_name_{num()}") | |
delete_btn_id <- glue::glue("delete_{num()}") | |
row <- fluidRow( | |
id = row_id, | |
column(2, textInput(varname_id, label = NULL, | |
value = varname_value, width = "100%")), | |
fluidRow( | |
shiny::actionButton(delete_btn_id, label = NULL, | |
icon = shiny::icon("trash-alt"), width = '40px') | |
) | |
) | |
insertUI( | |
selector = glue::glue("#{'RowPlaceholder'}"), | |
where = "beforeEnd", | |
ui = row, | |
immediate = TRUE | |
) | |
observeEvent(input[[delete_btn_id]],{ | |
delete_row_number <- stringi::stri_split_fixed( | |
delete_btn_id, "delete_", omit_empty=TRUE, simplify=TRUE) | |
delete_row_number <- as.numeric(delete_row_number) | |
delete_row(delete_row_number) | |
}, once = TRUE, ignoreInit = TRUE, priority = 2) | |
} | |
delete_row <- function(delete_row_number) { | |
row_id <- glue::glue("row_{delete_row_number}") | |
delete_btn_id <- glue::glue("delete_{delete_row_number}") | |
removeUI(selector = glue::glue("#{row_id}"), immediate = TRUE) | |
shinyjs::runjs(glue::glue("Shiny.onInputChange(‘{delete_btn_id}’, null)")) | |
current_rows( | |
current_rows()[-match(delete_row_number, current_rows())] | |
) | |
} | |
observeEvent(input$delete_all, { | |
lapply(current_rows(), function(row_number){ | |
delete_id <- glue::glue('delete_{row_number}') | |
delete_btn_to_click <- glue::glue('{delete_id}') | |
shinyjs::click(delete_btn_to_click) | |
}) | |
num(0) | |
}) | |
observeEvent(input$auto_populate,{ | |
lapply(current_rows(), function(row_number){ | |
delete_id <- glue::glue('delete_{row_number}') | |
delete_btn_to_click <- glue::glue('{delete_id}') | |
shinyjs::click(delete_btn_to_click) | |
}) | |
if (length(current_rows()) == 0 | is.null(current_rows())){ | |
if(isTruthy(input$auto_populate)){ | |
current_rows <- reactiveVal() | |
num(0) | |
instructions <- c("row1", "row2") | |
lapply(instructions, function(x){ | |
insert_row(num, varname_value = x) | |
num(num()+1) | |
}) | |
} | |
} | |
}) | |
} | |
shinyApp(ui, server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment