我正在从行的每个单元格中寻找不同的弹出窗口。 Florian 提供了一个例子,位于 闪亮的数据表:在新窗口中弹出有关所选行的数据
我们可以为不同的单元格设置不同的弹出窗口吗?目前它是行级别。例如 mpg 中的某些链接和光盘中的某个链接等... 下面提供了他提供的代码解决方案
mymtcars = head(mtcars)
for_pop_up = 1:6
app <- shinyApp(
ui = fluidPage(
DT::dataTableOutput("mydatatable")
),
server = shinyServer(function(input, output, session) {
mycars = head(mtcars)
output$mydatatable = DT::renderDataTable(mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'))
observeEvent(input$mydatatable_rows_selected,
{
showModal(modalDialog(
title = "You have selected a row!",
mycars[input$mydatatable_rows_selected,]
))
})
})
这是一个 JavaScript 方式:
library(shiny)
library(DT)
js <- c(
"table.on('click', 'tbody td', function() {",
" var indices = table.cell(this).index();",
" var i = indices.row;",
" var j = indices.column;",
" Shiny.setInputValue('click', {row: i, column: j}, {priority: 'event'});",
"});"
)
shinyApp(
ui = fluidPage(
br(),
DTOutput("mydatatable")
),
server = function(input, output, session) {
mycars <- head(mtcars)
output$mydatatable <- renderDT({
datatable(
mycars, selection = 'single',
rownames = FALSE, options = list(dom = 't'),
callback = JS(js)
)
})
observeEvent(input$click, {
i <- input$click$row + 1
j <- input$click$col + 1
showModal(modalDialog(
title = "You have clicked on a cell!",
mycars[i, j]
))
})
}
)