0

I made a select table with button in R shiny. I want to add a submit actionButton to get the select value from DT::renderDataTable though observeEvent() function.

I have tried

observeEvent(input$submit,{
data <- data.frame(output$sel)}

This is not work. Is it possible to get the value of callback? The value can been see in the UI, but how can i get in server?

library(shiny)
library(DT)
library(googlesheets4)

c1 <-c("Time in free flow (mins)",
       "Time slowed down by other traffic (mins)",
       "Percentage of total time spent with other vehicles close behind",
       "Curviness",
       "Running costs",
       "Toll cost") 

c2 <- c('50','10','25%','Moderate','$12.00','$0.00')

c3 <- c('38','5','31%','extreame','$10.50','$3.00')

c4 <- c('62','8','19%','Almost straight','$9.00','$0.50')

c5 <- c('56','15','12%','Moderate','$13.50','$0.00')

t <- cbind(c1,c2,c3,c4,c5)


shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    DT::dataTableOutput('foo'),
    
    #tt <- textOutput('sel'),
    textOutput('sel'),
    actionButton("submit", "submit")
  ),
  server = function(input, output, session) {
    m = matrix(
      as.character(1:4), nrow = 1, ncol = 4, byrow = TRUE,
      dimnames = list(' ', LETTERS[1:4])
    )
    for (i in seq_len(nrow(m))) {
      m[i, ] = sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        ' ', m[i, ]
      )
    }
    m <- cbind('Chioce',m)
    
    m<-rbind(t,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(' ', function(i) input[[i]]))
    })
   
    observeEvent(input$submit,{

    })})

Dou
  • 71
  • 5

1 Answers1

1

You can get the selected row indices (and work forward from there) like this:


ui <- fluidPage(
    verbatimTextOutput('log'),
    DTOutput('myTable')
)

server <- function(input, output) { 
    output$myTable <- renderDataTable(iris)
    output$log <- renderPrint({
        row_indices <- input$myTable_rows_selected
        iris[row_indices,]
    })

}

shinyApp(ui, server)

edit You can set server input values via client side javascript. In your case:

## [...] 
    for (i in seq_len(nrow(m))) {
    m[i, ] = sprintf(
            '<input type="radio" name="%s" value="%s"
            // set input$selected to m[i,] when clicked:
            onClick = "Shiny.setInputValue(\'selected\', %s)" />',
            ' ', m[i, ], m[i,]
    )
    }

## now input$selected will be updated upon selection of either radio input
## [...]

see: https://shiny.rstudio.com/articles/communicating-with-js.html

I_O
  • 4,983
  • 2
  • 2
  • 15
  • Thank you for the answer. Forgive me for the inaccurate expression of the question. What I'm trying to do is save the selected selection. For exmaple, save the verbatimTextOutput() function shows value. – Dou Nov 16 '22 at 13:42
  • "Save" like making it available for download? This might be helpful: https://stackoverflow.com/questions/48493829/download-filtered-data-from-renderdatatable-in-shiny – I_O Nov 16 '22 at 14:19
  • It's not downloading. In your example, when I select a row, its value will appear in TextOutput().I want to get the value that the user selected. Instead of displaying it in the ui, it's stored on the server. – Dou Nov 16 '22 at 14:43
  • If we get this value on the server, we can store it in the database. – Dou Nov 16 '22 at 14:47
  • The result of `input$myTable_rows_selected` is a vector of indices of the rows selected from the outgoing (server-side) data frame by the client (*regardless of client-side sorting/filtering*). You can thus filter the server-side data frame by the incoming row index and save the remaining row set as desired (writing it to a text file, database ...) – I_O Nov 16 '22 at 14:57
  • Yes, It worked in your exmaple. But I can not find this kind of input in my code. Because I use some JS code to add radio buttons in table. And I don't konw much about JS code. So I can't find an explicit input. – Dou Nov 16 '22 at 15:15