1
votes

I am trying to develop a sort of form through a shiny app, the idea is that the user fills a specific field (a zoning of medical-development priority) about many geographical places (french districts). Thus I think DT is the best option. I don't want to make the column editable because I want the user to choose between 4 specific values (the idea of the form is to reduce the free text to be cleaned afterward).

I started to work with the example of YiHui https://yihui.shinyapps.io/DT-radio/ As I work for french users I tried to add an option to customize language to french, but the entire logic broke, I don't know why.

Here is a reprex : (simply comment the line with language = list(... to make it work).

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {
    m = matrix(
      as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE,
      dimnames = list(month.abb, LETTERS[1:5])
    )
    for (i in seq_len(nrow(m))) {
      m[i, ] = sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month.abb[i], m[i, ]
      )
    }
    m
    output$foo = DT::renderDataTable(
      m, escape = FALSE, selection = 'none', server = FALSE,
      options = list(dom = 'tirp', paging = FALSE, ordering = FALSE
                     ,language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/French.json')
                     ),
      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]]))
    })
  }
)

Here is my sessionInfo()

with R 3.6.0

sessioninfo::session_info() - Session info ------------------------------------------------------------------ setting value
version R version 3.6.0 (2019-04-26) os Windows 10 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate French_France.1252
ctype French_France.1252
tz Europe/Paris
date 2019-06-26

  • Packages ---------------------------------------------------------------------- package * version date lib source
    assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
    cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.0)
    crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
    crosstalk 1.0.0 2016-12-21 [1] CRAN (R 3.6.0)
    digest 0.6.19 2019-05-20 [1] CRAN (R 3.6.0)
    DT * 0.7.1 2019-06-26 [1] Github (rstudio/DT@c6fd864) htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.0)
    htmlwidgets 1.3 2018-09-30 [1] CRAN (R 3.6.0)
    httpuv 1.5.1 2019-04-05 [1] CRAN (R 3.6.0)
    jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0)
    later 0.8.0 2019-02-11 [1] CRAN (R 3.6.0)
    magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
    mime 0.7 2019-06-11 [1] CRAN (R 3.6.0)
    packrat 0.5.0 2018-11-14 [1] CRAN (R 3.6.0)
    promises 1.0.1 2018-04-13 [1] CRAN (R 3.6.0)
    R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.0)
    Rcpp 1.0.1 2019-03-17 [1] CRAN (R 3.6.0)
    rlang 0.3.4 2019-04-07 [1] CRAN (R 3.6.0)
    rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0)
    sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
    shiny * 1.3.2 2019-04-22 [1] CRAN (R 3.6.0)
    sourcetools 0.1.7 2018-04-25 [1] CRAN (R 3.6.0)
    withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
    xtable 1.8-4 2019-04-21 [1] CRAN (R 3.6.0)
    yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)

I also tried with R 3.5.2 but I have the save issue.

When I click some radio-buttons I should see :

List of 12
 $ Jan: chr "1"
 $ Feb: chr "2"
 $ Mar: NULL
 $ Apr: NULL
 $ May: NULL
 $ Jun: NULL
 $ Jul: NULL
 $ Aug: NULL
 $ Sep: NULL
 $ Oct: NULL
 $ Nov: NULL
 $ Dec: NULL

Instead it says NULL, I can't find any explicit error.

List of 12
 $ Jan: NULL
 $ Feb: NULL
 $ Mar: NULL
 $ Apr: NULL
 $ May: NULL
 $ Jun: NULL
 $ Jul: NULL
 $ Aug: NULL
 $ Sep: NULL
 $ Oct: NULL
 $ Nov: NULL
 $ Dec: NULL

What is the problem with the language option in DT ?

1

1 Answers

1
votes

I can't explain but this works by adding a setTimeout:

  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());
                setTimeout(function(){
                  Shiny.bindAll(table.table().node());
                },0);")

EDIT

The above worked for me but I set dom = ftirp. That doesn't work with tirp. Here is another solution:

output$foo = DT::renderDataTable(
  m, escape = FALSE, selection = 'none', server = FALSE,
  options = list(dom = 'tirp', paging = FALSE, ordering = FALSE,
                 initComplete = JS("function(settings,json){
                                     var table = settings.oInstance.api();
                                     Shiny.unbindAll(table.table().node());
                                     Shiny.bindAll(table.table().node());}")
                 ,language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/French.json')
  ),
  callback = JS("table.rows().every(function(i, tab, row) {
                  var $this = $(this.node());
                  $this.attr('id', this.data()[0]);
                  $this.addClass('shiny-input-radiogroup');
                });")
)