快速提问:如何使用/获取闪亮的gvisTable选择?
我可以用这样的DT包实现这个目的:
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)
是否有可能与gvis获得相同的选择?我google了很多,但找不到有人在闪亮的地方再现它。
答案 0 :(得分:3)
您可以向listener
添加options
并将其绑定到名为text
的变量,就像我一样
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)
答案 1 :(得分:1)
用于处理多项选择的变体(如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
})
}
)
在观察中打印所选行的值。 索引从0开始