Sebastian Sebastian - 3 months ago 23
R Question

Use selection from gvisTable in shiny

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.

Answer

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