2021年3月23日星期二

How to keep edited values in an output datatable using shiny

I am building a shiny app to add all input variables to a datatable. I also have another button used to enable a second tabPanel and shows an editable datatable. I would like to edit the table in the second tabPanel and update the datatable. So, when I continue to add more values it can keep the updates that have been done.

library("shiny")  library("dplyr")  library("DT")    ui <- fluidPage(    tabsetPanel(      id = "idTabset",      tabPanel(        value = "input_data",        title = "Inpute Data",        fluidRow(          column(2, textInput(inputId   = 'item1', label = 'Item 1')),          column(2, textInput(inputId   = 'item2', label = 'Item 2')),          column(2, textInput(inputId   = 'item3', label = 'Item 3')),          column(2, selectInput(inputId = 'item4', label = 'Gender', choices = c("M", "F"))),          column(2, textInput(inputId   = 'item5', label = 'Item 4')),          column(2, textInput(inputId   = 'item6', label = 'Item 5'))        ),        fluidRow(          column(2, textInput(inputId   = 'item7',  label = 'Item 7')),          column(2, textInput(inputId   = 'item8',  label = 'Item 8')),          column(2, textInput(inputId   = 'item9',  label = 'Item 9')),          column(2, textInput(inputId   = 'item10', label = 'Item 10')),          column(2, textInput(inputId   = 'item11', label = 'Item 11')),          column(2, selectInput(inputId = 'item12', label = 'Question', choices = c("Y", "N"))),        ),        fluidRow(          column(2, textInput(inputId   = 'item13', label = 'Item 13')),          column(2, textInput(inputId   = 'item14', label = 'Item 14')),          column(2, textInput(inputId   = 'item15', label = 'Item 15')),          column(2, textInput(inputId   = 'item16', label = 'Item 16')),          column(2, textInput(inputId   = 'item17', label = 'Item 17')),          column(2, textInput(inputId   = 'item18', label = 'Item 18')),        ),        actionButton('save_data', 'Add'),        actionButton('review_data', 'Save and Review')      ),      tabPanel(        value = "input_tbl",        title = "Raw Data",        DT::dataTableOutput('user_tbl'),        actionButton('submit_tbl', 'Upload')      )    )  )    server <- function(input, output, session) {    hideTab(inputId = "idTabset", target = "input_tbl")        user_tbl <- tibble(`Item 1`  = as.character(),                          `Item 2`  = as.character(),                          `Item 3`  = as.character(),                          Gender    = as.character(),                          `Item 5`  = as.character(),                           `Item 6`  = as.numeric(),                          `Item 7`  = as.numeric(),                           `Item 8`  = as.numeric(),                          `Item 9`  = as.numeric(),                           `Item 10` = as.numeric(),                           `Item 11` = as.numeric(),                           Question  = as.character(),                          `Item 13` = as.numeric(),                          `Item 14` = as.numeric(),                           `Item 15` = as.numeric(),                           `Item 16` = as.numeric(),                           `Item 17` = as.numeric(),                           `Item 18` = as.numeric())        user_data <- reactive({      tibble(`Item 1`  = as.character(input$item1),             `Item 2`  = as.character(input$item2),             `Item 3`  = as.character(input$item3),             Gender    = as.character(input$item4),             `Item 5`  = as.character(input$item5),             `Item 6`  = as.numeric(input$item6),             `Item 7`  = as.numeric(input$item7),             `Item 8`  = as.numeric(input$item8),             `Item 9`  = as.numeric(input$item9),             `Item 10` = as.numeric(input$item10),             `Item 11` = as.numeric(input$item11),             Question  = as.character(input$item12),             `Item 13` = as.numeric(input$item13),             `Item 14` = as.numeric(input$item14),             `Item 15` = as.numeric(input$item15),             `Item 16` = as.numeric(input$item16),             `Item 17` = as.numeric(input$item17),             `Item 18` = as.numeric(input$item18))    })            observeEvent(input$save_data, {      user_tbl <<- user_tbl %>%        bind_rows(user_data())            output$user_tbl = DT::renderDataTable({        DT::datatable(user_tbl, editable = TRUE)      })          sapply(c('item1','item2', 'item3','item5','item6',               'item7','item8','item9', 'item10','item11',               'item13','item14','item15','item16','item17','item18'),             function(x) {updateTextInput(session, x, value = "")})    })        observeEvent(input$review_data, {      showTab(inputId = "idTabset", target = "input_tbl")    })      }    shinyApp(ui = ui, server = server)    

Does anyone know how to keep the updated values?

Thank you.

https://stackoverflow.com/questions/66773398/how-to-keep-edited-values-in-an-output-datatable-using-shiny March 24, 2021 at 09:08AM

没有评论:

发表评论