Skip to content

Instantly share code, notes, and snippets.

@mcguinlu
Last active November 24, 2020 17:55
Show Gist options
  • Save mcguinlu/ae0a40ce14e3d5c99c5aaaa7c4a25d17 to your computer and use it in GitHub Desktop.
Save mcguinlu/ae0a40ce14e3d5c99c5aaaa7c4a25d17 to your computer and use it in GitHub Desktop.
library(shiny)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
DT::dataTableOutput("mytable")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues()
# Import data from fileInput(inputID = "data")
rv$data <- mtcars
# Define basic table
output$mytable <- DT::renderDataTable(
rv$data,
editable = list(target = 'cell'),
server = TRUE,
rownames = FALSE,
escape = FALSE,
options = list(
dom = 't',
ordering = F,
columnDefs = list(list(
className = 'dt-center', targets = "_all"
)),
paging = FALSE
)
)
# Define table proxy
proxy = dataTableProxy('mytable')
# Update reactive dataset on cell edit
observeEvent(input$mytable_cell_edit, {
info <- input$mytable_cell_edit
i <- info$row # Define edited row
j <- info$col + 1L # Define edited column (column index offset by 1)
v <- info$value # Define value of edit
# Pass edited value to appropriate cell of data stored in rv$data
rv$data[i, j] <- coerceValue(v, rv$data[i, j])
# Replace data in table with updated data stored in rv$data
replaceData(proxy,
rv$data,
resetPaging = FALSE,
rownames = FALSE) # important
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment