Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve custom edits in cells prior to reactive update of table #287

Closed
sbihorel opened this issue Jan 1, 2019 · 2 comments
Closed

Preserve custom edits in cells prior to reactive update of table #287

sbihorel opened this issue Jan 1, 2019 · 2 comments
Labels

Comments

@sbihorel
Copy link

@sbihorel sbihorel commented Jan 1, 2019

Hi,

Let's assume a rhandsontable which default content depends on a selectInput. The table is editable. Using "vanilla" coding (as illustrated below), if users edit the content of the table, then change the selection of the selectInput field, all their edits are lost.
In the example below, the table can take 2 different default contents. These default content only have one row is common ('Row1').
I am trying to find a way to preserve custom edits made in the Value column when (prior to?) the table content resets. The content of the Value column would ideally be preserved for Type's that match between the previous and new content.

The commented section represents my attempt at achieving this goal. However, uncommenting it someone breaks the reactivity chain.

Thanks in advance for your help

require(shiny)
require(rhandsontable)

server <- function(input, output, session){
  
  rTable_content <- reactive({
    if (input$select == 'A'){
      DF <- data.frame(
        'Type' = paste0('Row', 1:5),
        'Value' = rep('', 5),
        stringsAsFactors = FALSE
      )
    } else {
      DF <- data.frame(
        'Type' = paste0('Row', c(1,12:15)),
        'Value' = rep('', 5),
        stringsAsFactors = FALSE
      )
    }
    
    # # Try to keep previously entered custom value for match Type's
    # if (length(input$rTable) > 0){
    #   oDF <- hot_to_r(input$rTable)
    #   DF <- merge(DF, oDF, by = 'Type')
    #   DF$Value <- ifelse(is.na(DF[,3]), DF[,2], DF[,3])
    #   DF <- DF[, 1:2]
    # }
    
    DF
  })
  
  output$rTable <- renderRHandsontable({
    rhandsontable(
      data = rTable_content(),
      rowHeaders = NULL,
      contextMenu = FALSE,
      width = 600,
      height = 300
    )
  })
  
  output$textOuput <- renderText({
    paste(
      unlist(hot_to_r(input$rTable)),
      collapse = '; '
    )
  })  
}

ui <- function(){
  fluidPage(
    fluidRow(
      column(
        width = 12,
        selectInput(
          inputId = 'select',
          label = 'Pick one!',
          choices = LETTERS[1:2],
          selected = 'A'
        ),
        rHandsontableOutput('rTable'),
        verbatimTextOutput('textOuput')
      )
    )
  )
}

shinyApp(ui, server)
@sbihorel
Copy link
Author

@sbihorel sbihorel commented Jan 25, 2019

Revisiting my post and the problem in general, I found the solution by actually correcting some mistakes in the original code. The code posted below works fine.

Updating title

require(shiny)
require(rhandsontable)

server <- function(input, output, session){
  
  rTable_content <- reactive(
    {
      if (input$select == 'A'){
        DF <- data.frame(
          'Type' = paste0('Row', 1:5),
          'Value' = rep('', 5),
          stringsAsFactors = FALSE
        )
      } else {
        DF <- data.frame(
          'Type' = paste0('Row', c(1,12:15)),
          'Value' = rep('', 5),
          stringsAsFactors = FALSE
        )
      }
      
      # Try to keep previously entered custom value for match Type's
      if (length(input$rTable) > 0){
        oDF <- hot_to_r(input$rTable)
        DF <- merge(DF, oDF, by = 'Type', all.x = TRUE)
        DF$Value <- ifelse(is.na(DF[,3]), DF[,2], DF[,3])
        DF <- DF[, c(1, ncol(DF))]
      }
      
      DF
    }
  )
  
  output$rTable <- renderRHandsontable({
    rhandsontable(
      data = rTable_content(),
      rowHeaders = NULL,
      contextMenu = FALSE,
      width = 600,
      height = 300
    )
  })
  
  output$textOuput <- renderText({
    paste(
      unlist(hot_to_r(input$rTable)),
      collapse = '; '
    )
  })  
}

ui <- function(){
  fluidPage(
    fluidRow(
      column(
        width = 12,
        selectInput(
          inputId = 'select',
          label = 'Pick one!',
          choices = LETTERS[1:2],
          selected = 'A'
        ),
        rHandsontableOutput('rTable'),
        verbatimTextOutput('textOuput')
      )
    )
  )
}

shinyApp(ui, server)

@sbihorel sbihorel changed the title Feasibility question Preserve custom edits in cells prior to reactive update of table Jan 25, 2019
@sbihorel
Copy link
Author

@sbihorel sbihorel commented Jan 25, 2019

Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants