0
votes

I am having trouble accessing radio button input values when they're imbedded in a DataTable in shiny. I am basing my code of off this example: Radio Buttons in DT

It accesses the values on the first pass and displays them underneath as seen in the following screenshot:
First pass screenshot

Once I change an input that reactively re-renders the table, the values from the radio buttons are no longer displayed and are instead stuck on the first pass values. This can be seen in the following screenshot. Second pass screenshot

After the first pass, the inputs are no longer updated to reflect the radio button selection. The original inputs seem to freeze and I can't find a way to access the current button values.

Thank you in advance for the help!

Here is the code for the above example:

library(shiny)
library(DT)
library(data.table)
shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    selectInput('questions','Pick',c(1,2)),
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {
    
    myData = reactiveValues(m = NULL)
    temp = month.abb
    
    observeEvent(input$questions,{
      m = data.table(
        
        
        month1 = temp[1:5],
        A = '1',
        B = '2',
        C = '3',
        QWE = runif(5)
      )
      m[, A := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, A]
      )]
      m[, B := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, B]
      )]
      m[, C := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, C]
      )]
      
      myData$m = m
    })
    
    
    
    output$foo = DT::renderDataTable({
      m = myData$m
      print(m)
      
    }
    , escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    callback = JS("table.rows().every(function(i, tab, row) {
                  var $this = $(this.node());
                  $this.attr('id', this.data()[0]);
                  $this.addClass('shiny-input-radiogroup');
  });
                  Shiny.unbindAll(table.table().node());
                  Shiny.bindAll(table.table().node());")
    )
    output$sel = renderPrint({
      str(sapply(month.abb, function(i) input[[i]]))
    })
    }
)
1

1 Answers

0
votes

That is because you are resetting m inside the observeEvent(). If you define it outside the observer, it works as expected. Try this

  server = function(input, output, session) {
    
    myData <- reactiveValues(m = NULL)
    temp <- month.abb
    m <- data.table(
      month1 = temp[1:5],
      A = '1',
      B = '2',
      C = '3',
      QWE = runif(5)
    )
    
    observeEvent(input$questions,{
     
      m[, A := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, A]
      )]
      m[, B := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, B]
      )]
      m[, C := sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month1, m[, C]
      )]
      
      myData$m = m
    })
    
    
    
    output$foo = DT::renderDataTable({
      m = myData$m
      print(m)
      
    }
    , escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE, # editable=TRUE,
    options = list(dom = 't', paging = FALSE, ordering = FALSE),
    callback = JS("table.rows().every(function(i, tab, row) {
                  var $this = $(this.node());
                  $this.attr('id', this.data()[0]);
                  $this.addClass('shiny-input-radiogroup');
     });
                  Shiny.unbindAll(table.table().node());
                  Shiny.bindAll(table.table().node());")
    )
    
    
    output$sel = renderPrint({
      str(sapply(month.abb, function(i) input[[i]]))
    })
  }