Maria Suprun
Maria Suprun

Reputation: 51

RShiny: DT select rows and assign to group

I would like to give users an option to assign different rows (subjects) to groups.

Ideally, one can highlight rows, then write a group name in the "Assign to group: " field and it is saved. Then they can select a new set of rows and add those to a different group; and so on until all desired rows are assigned.

Here is what I have so far. I can't figure out how to save the results before selecting a new set of rows..

library(shiny)
library(DT)


ui <- fluidPage(

  fluidRow(
    column(6, 
           DT::dataTableOutput('x1'),
           textInput("assignGroup","Assign to group: ")),
    column(6, DT::dataTableOutput('x2'))
  )

)

server <- shinyServer(function(input, output, session) {

  output$x1 = DT::renderDataTable(cars, server = FALSE)

  output$x2 = DT::renderDataTable({
    s <- input$x1_rows_selected
    temp <- cars
    temp$Experiment <- as.character("")
    temp[s,"Experiment"] <- input$assignGroup
    temp

  }, server = FALSE)  

})


shinyApp(ui, server)

Thanks and hope this is somewhat clear!!!

Upvotes: 0

Views: 494

Answers (1)

Mohit Sahasrabudhe
Mohit Sahasrabudhe

Reputation: 138

You can observe 'input$x1_rows_selected' and then edit the table on the event. To display live changes to the table, you can add a reactive table.

Hope this code works out for you.

library(shiny)
library(DT)


ui <- fluidPage(

  fluidRow(
    column(6, 
           DT::dataTableOutput('x1'),
           textInput("assignGroup","Assign to group: ")
    ),
    column(6, DT::dataTableOutput('x2'))
  )

)

server <- shinyServer(function(input, output, session) {


  values <- reactiveValues(
    df_data = cars,
    temp = {
      cars[,c("Experiment")]<-NA
      cars
    }
  )

  output$x1 = DT::renderDataTable(values$df_data, server = FALSE)

  observeEvent(input$x1_rows_selected,
               {
                 s<-input$x1_rows_selected
                 values$temp[s,"Experiment"]<-input$assignGroup
               }
  )

  output$x2 = DT::renderDataTable(
    values$temp, server = FALSE)  

})


shinyApp(ui, server)

Upvotes: 2

Related Questions