How to embed radios into DT form

455 views
Skip to first unread message

杨光垚

unread,
Jul 13, 2016, 1:00:40 AM7/13/16
to Shiny - Web Framework for R

I want to embed radios into DT form and make a questionnaire.


I want to select options through selecting radios. Radios are embedded into form through html code.


However, i don't know how to control selecting through embedded radios. So there is a bug like third picture below.





Yihui Xie

unread,
Jul 13, 2016, 10:20:50 AM7/13/16
to 杨光垚, Shiny - Web Framework for R
Could you post a minimal reproducible example?


Regards,
Yihui
> --
> You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e87047db-2a26-4e42-8585-c1e0edab3699%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

7716...@qq.com

unread,
Jul 31, 2016, 9:05:19 AM7/31/16
to Shiny - Web Framework for R, guangya...@gmail.com
library(shiny)
library(DT)
# ratio 选择行 ***
shinyApp(
    ui = fluidPage(
        
        h1('select rable row'),
        
        fluidRow(
            column(9,DT::dataTableOutput('tb1')),
            column(3, verbatimTextOutput('x1'))
        )
        
    ),
    
    server = function(input, output) {
        #定义table
        df_iris = iris[1:6,]
        #定义产生button的函数段
        shinyInput <- function(FUN, len, id, ...) {
            inputs <- character(len)
            for (i in seq_len(len)) {
                inputs[i] <- as.character(FUN(paste0(id, i), ...))
            }
            inputs
        }
        #对表格进行包装,增加button
        addRadioButtons <- paste0('<input type="radio" name="row',  '" value="', 1:nrow(df_iris), '">',"")
        df_iris_buttons = cbind(Pick=addRadioButtons,df_iris)
        #输出表格设置
        output$tb1 = DT::renderDataTable(
            df_iris_buttons , server = FALSE, escape = FALSE, selection = 'single'
        )
        #输出文字设置
        output$x1 = renderPrint({
            s = input$tb1_row_last_clicked
            if (length(s)) {
                cat('These rows were selected:\n\n')
                cat(s, sep = ', ')
            }
        })
    }
    
)


The minimal reproducible example is above.


When I use radio to choose row 1, then I click row 2 not through radio. Now I expect that row 1 is still chosen. However, row 2 is chosen as is show in the left box.

Overall, I want to use embedded radio to control row selection, but I don't know how to do that.

Thank you very much.

在 2016年7月13日星期三 UTC+8下午10:20:50,Yihui Xie写道:

Yihui Xie

unread,
Aug 7, 2016, 4:30:55 PM8/7/16
to 7716...@qq.com, Shiny - Web Framework for R, 杨光垚
I think you should disable the built-in row selection in this case.
See http://rstudio.github.io/DT/011-radio.html for an example.

Regards,
Yihui
> When I use radio to choose row 1, then I click row 2 not through radio. Now I expect that row 1 is still chosen. However, row 2 is chosen as is show in the left box.
>
> Overall, I want to use embedded radio to control row selection, but I don't know how to do that.
>
> Thank you very much.
>
> 在 2016年7月13日星期三 UTC+8下午10:20:50,Yihui Xie写道:
>>
>> Could you post a minimal reproducible example?
>>
>>
>> Regards,
>> Yihui
>>
>> On Wed, Jul 13, 2016 at 12:00 AM, 杨光垚 <guangya...@gmail.com> wrote:
>> >
>> > I want to embed radios into DT form and make a questionnaire.
>> >
>> >
>> > I want to select options through selecting radios. Radios are embedded into form through html code.
>> >
>> >
>> > However, i don't know how to control selecting through embedded radios. So there is a bug like third picture below.
>> >
>> >
>> >
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
>> > To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e87047db-2a26-4e42-8585-c1e0edab3699%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e4d7977f-47be-4e1b-b798-7ce26c853351%40googlegroups.com.

7716...@qq.com

unread,
Aug 12, 2016, 2:21:44 AM8/12/16
to Shiny - Web Framework for R, 7716...@qq.com, guangya...@gmail.com
Thank you very much for your code.
And I have one more question.

The question is: I must keep row name as same as radio name at each row in your code. But sometime I want to make these names different. Then what should I do?

The modified code is below and I just changed one line which is commented.

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, ]
            )
        }
        rownames(m)[1] = '1st'   # I've changed here !!!
        m
        output$foo = DT::renderDataTable(
            m, escape = FALSE, selection = 'none', server = 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]]))
        })
        }
)


Then it doesn't work.



I think i shold do some modification to the callback, but I don't know what to do?

Thank you very much !!!

在 2016年8月8日星期一 UTC+8上午4:30:55,Yihui Xie写道:

Yihui Xie

unread,
Aug 12, 2016, 5:55:23 PM8/12/16
to 7716...@qq.com, Shiny - Web Framework for R, 杨光垚
Of course. I was just using month.abb as an example (same thing for
LETTERS[1:5]). You can use any row names you want.

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


Regards,
Yihui
> To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/b3eb47ce-b4c8-42f4-b532-786b14302a9e%40googlegroups.com.

7716...@qq.com

unread,
Aug 12, 2016, 8:41:33 PM8/12/16
to Shiny - Web Framework for R, 7716...@qq.com, guangya...@gmail.com
Thanks for your patient answer. I have not made my question clear before.

I want that row name is different from the radio name.

For example, the row name is 'Jan', but I want the radio name to be ‘1st’ so that I can get the radio value through input[['1st']] and matain the row name 'Jan' in the table . 
That means in js I can't get the radio value through the row name (maybe right?).

I don't know if I've clarified my problem. Thank you very much again !!!

在 2016年8月13日星期六 UTC+8上午5:55:23,Yihui Xie写道:

Yihui Xie

unread,
Aug 12, 2016, 11:25:33 PM8/12/16
to 7716...@qq.com, Shiny - Web Framework for R, 杨光垚
Of course you can do that. The row names and radio IDs can be
completely independent. Whatever ID you use the radio, just make sure
you access its value with the correct ID in input[[]].

Regards,
Yihui

7716...@qq.com

unread,
Aug 12, 2016, 11:36:24 PM8/12/16
to Shiny - Web Framework for R, 7716...@qq.com, guangya...@gmail.com
Thanks for your reply. Then there comes the question.

Different row have different radio names, how can I make some settings in callback to let shiny know my radio names.

I think I should do something about $this.attr('id', this.data()[0]), but I don't know how to do that?

For example, month.abb contains row names. At the same time, I want to assign radio name to myname (same length as month.abb). How can I change code to make shiny know my radio name and get radio value through input[[myname]].

Thank you a lot !!!

在 2016年8月13日星期六 UTC+8上午11:25:33,Yihui Xie写道:

Yihui Xie

unread,
Aug 13, 2016, 12:22:12 AM8/13/16
to 7716...@qq.com, Shiny - Web Framework for R, 杨光垚
Good question. One way you can go is to add the IDs to a certain
column of the data, and hide this column, e.g. add the IDs to the
first column and hide it via the options

options = list(
columnDefs = list(
list(targets = 1, visible = FALSE)
)
)

Full reference: https://datatables.net/reference/option/columnDefs

Then $this.attr('id', this.data()[1]);

Regards,
Yihui

7716...@qq.com

unread,
Aug 13, 2016, 2:52:36 AM8/13/16
to Shiny - Web Framework for R, 7716...@qq.com, guangya...@gmail.com
Thank you very much !

You've helped me a lot and I've successfully solved my problem under your warmhearted help.

I'm going to study hard and learn from you !

Thank you a lot !!!




在 2016年8月13日星期六 UTC+8下午12:22:12,Yihui Xie写道:

Siddharth Dixit

unread,
Oct 14, 2018, 8:15:48 AM10/14/18
to Shiny - Web Framework for R
Hello, 
Can anyone of you help me in getting a data table with radio buttons (Same as you guys produced earlier) with radio buttons of one column checked by default?
Reply all
Reply to author
Forward
0 new messages