3

Quick question: How is it possible to use/get the selection of a gvisTable in shiny?

I can achieve this with the DT package like this:

library(DT)
library(shiny)

server <- function(input, output) {
  output$dt <- renderDataTable({
    datatable(cbind(c(1,2,3,4,5),c(5,4,3,2,1)))
  })

  output$dtselect <- renderText({

    input$dt_rows_selected

  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      "Selected Rows from Datatable in Text Output"
    ),
    mainPanel(dataTableOutput("dt"),
              textOutput("dtselect"))
  )
)

shinyApp(ui = ui, server = server)

Is it possible to get the same selection with gvis? I googled a lot but could not find somebody reproducing the same in shiny.

Sebastian
  • 2,430
  • 4
  • 23
  • 40

2 Answers2

3

You can add a listenerto the options and bind it to a variable called text as I did

rm(list = ls())
library(shiny)
library(googleVis)

mydata <- as.data.frame(cbind(c(1,2,3,4,5),c(5,4,3,2,1)))

server <- function(input, output) {
  output$myTable <- renderGvis({
    gvisTable(mydata, chartid = "mydata", 
              options = list(gvis.listener.jscode = "var text = data.getValue(chart.getSelection()[0].row,0);Shiny.onInputChange('text', text.toString());"))})
  output$dtselect <- renderText({input$text})
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      "Selected Rows from Datatable in Text Output"
    ),
    mainPanel(htmlOutput("myTable"),textOutput("dtselect"))
  )
)

shinyApp(ui = ui, server = server)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
1

Variant to handle multiple selection (as told here )

library(googleVis)
library(shiny)
mydata <- as.data.frame(cbind(c(1,2,3,4,5),c(5,4,3,2,1)))
shinyApp(
  ui = fluidPage(
      htmlOutput("myTable")
    )
  ,
  server = function(input,output){
    observe({
      print(input$r_select)
    })

    output$myTable <- renderGvis({
     gt= gvisTable(mydata,chartid="mydata")
     jsInsert ="
      google.visualization.events.addListener(chart, 'select', selectHandler);
      var selectedRows = new Array(); 
      function selectHandler() {  
        var selection = chart.getSelection(); 
        for (var idx in selection){ 
            var item = selection[idx]; 
            if (item) { 
              i = selectedRows.indexOf(item.row); 
              if (i == -1){ 
                  selectedRows.push(item.row); 
                  data.setProperty(item.row, 0,'style','background-color:#d6e9f8;'); 
                  data.setProperty(item.row, 1,'style','background-color:#d6e9f8;'); 
            } else { 
             selectedRows.splice(i,1); 
             data.setProperty(item.row,0,'style',null); 
             data.setProperty(item.row,1,'style',null); 
            }  
          } 
        } 
      chart.setSelection(null);  
      Shiny.onInputChange('r_select',selectedRows);      
         chart.draw(data,options);         
    }
    chart.draw(data,options);
     "
     gt$html$chart[['jsDrawChart']] <- gsub("chart.draw\\(data,options\\);", jsInsert, gt$html$chart[['jsDrawChart']])
     gt
    }) 
  }
)

Print values of selected rows in observe. Indexing start from 0

Batanichek
  • 7,761
  • 31
  • 49