问题描述
在选项卡之间创建链接的解决方案在这里选项卡之间的R闪亮的生成链接确实不错,但不适用于DT包(对我来说).有人可以告诉我,与没有DT软件包的解决方案相比,使用DT库的示例代码在做什么?
Solution for creating links between tabs a have found here R shiny build links between tabs is really nice, but it's not working with DT package (for me..).Can anybody tell me, what am I doing wrong in my example code using DT library in compare to the solution without DT package?
library(shiny)
library(DT)
server <- function(input, output) {
output$iris_type <- DT::renderDataTable({
datatable(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),
escape = FALSE,
options = list(initComplete = JS(
'function(table) {
table.on("click.dt", "tr", function() {
Shiny.onInputChange("rows", table.row( this ).index());
tabs = $(".tabbable .nav.nav-tabs li a");
$(tabs[1]).click();
});
}')))
})
output$filtered_data <- DT::renderDataTable({
if(is.null(input$rows)){
iris
}else{
iris[iris$Species %in% unique(iris$Species)[as.integer(input$rows)+1], ]
}
})
}
ui <- shinyUI(fluidPage(
mainPanel(
tabsetPanel(
tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
)
)
))
shinyApp(ui = ui, server = server)
推荐答案
您可以尝试下面的代码.我更改了将选项卡切换到回调(具有表格作为参数)的功能,并在您的output$filtered_data
中,将iris
替换为datable(iris)
,因为您正在使用DT::renderDataTable
You could try the code below. I changed the function switching the tabs to the callback (which has table as an argument) and in your output$filtered_data
, replaced iris
by datable(iris)
since you are rendering with DT::renderDataTable
library(shiny)
library(DT)
server <- function(input, output) {
output$iris_type <- DT::renderDataTable({
datatable(data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>")),
escape = FALSE,
callback = JS(
'table.on("click.dt", "tr", function() {
tabs = $(".tabbable .nav.nav-tabs li a");
$(tabs[1]).click();})'))
})
output$filtered_data <- DT::renderDataTable({
selected <- input$iris_type_rows_selected
if(is.null(selected)){
datatable(iris)
} else {
datatable(iris[iris$Species %in% unique(iris$Species)[selected], ])
}
})
}
ui <- shinyUI(fluidPage(
mainPanel(
tabsetPanel(
tabPanel("Iris Type", DT::dataTableOutput("iris_type")),
tabPanel("Filtered Data", DT::dataTableOutput("filtered_data"))
)
)
))
shinyApp(ui = ui, server = server)
请注意,这需要 DT > = 0.0.62 .
Please note this requires DT >= 0.0.62.
这篇关于带有DT软件包的选项卡之间的R闪亮构建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!