我的shiny
应用程序只有一张桌子(我用的是reactable
).
它必须允许多个 Select (以下载选定的行)、触发模式窗口的Click事件(与所单击的行是否被选中无关)以及最后一列上的DoubleClick事件.
我的工作示例执行以下操作:
- 允许多个 Select 和下载,
- 单击行时的模式(列‘.select’除外) 但是(3)DoubleClick事件不起作用.
下面是DoubleClick事件独立工作的另一个示例.
在第一个示例中,我如何才能使DoubleClick事件工作呢?
(第一个工作示例)
library(shiny)
library(tidyverse)
library(reactable)
ui <- fluidPage(
downloadButton("dload", "Download (*.csv)"),
fluidRow(
column(12, align = 'center',reactableOutput("tbl"))
)
)
server <- function(input, output, session) {
data = reactive({mtcars %>% tibble::rownames_to_column('car')})
output$tbl = renderReactable(
reactable(
data(),
columns = list(
carb = colDef(
sticky = "right",
sortable = FALSE,
cell = function(value, index, name){
icon("globe",
ondblclick = sprintf("window.alert('double clicked cell %s in column %s')", index, name)
)
})
),
selection = "multiple",
compact = T,
highlight = T,
onClick = JS("function(rowInfo, colInfo, column) {
Shiny.setInputValue('qwert', {id:rowInfo.id, nonce:Math.random()})
if (column.id !== 'carb') {
return
}
}"),
rowStyle = list(cursor = "pointer")
)
)
observeEvent(input$qwert, {
print(input$qwert)
id <- input$qwert$id
row_nr <- as.numeric(id)+1
showModal(
modalDialog(
size = 'l',
easyClose = T,
title = data()$car[row_nr],
"some text"
)
)
})
sel = reactive({getReactableState("tbl","selected")})
output$dload <- downloadHandler(
filename = function() {
paste("d"," ", Sys.time(), ".csv", sep = "")
},
content = function(file) {
write.csv(data()[sel(),],file)# write.csv(dd(), file, row.names = FALSE)
}
)
}
shinyApp(ui = ui, server = server)
(我的第二个工作示例--DoubleClick事件)
library(shiny)
library(reactable)
ui <- fillPage(
reactableOutput("t")
)
server <- function(input, output) {
data <- cbind(
MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price")],
d = NA
)
output$t <- renderReactable({
reactable(
data,
selection = "multiple",
columns = list(
d = colDef(
sortable = FALSE,
cell = function(value, index, name){
icon("globe", class = "cor", style = "font-size: 14px;",
ondblclick = sprintf("window.alert('double clicked cell %s in column %s')", index, name)
)
})
)
)})
}
shinyApp(ui,server)