单击DT数据表行时,将自动打开ShinyDashBoard左侧边栏

时间:2019-05-06 23:02:54

标签: r shiny shinydashboard shinyjs

当单击DT数据表行时,无法弄清楚告诉滑块进行更新(先关闭再打开)(可能绑定和取消绑定)的逻辑。

我评论了: tab specific sidebar in shinydashboard

Open new tab on table row click

Automatic rightSidebar popup when menuItem is clicked in shinydashboardPlus

但是,当单击数据表行时,无法进行绑定和取消绑定。

ui.R

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinythemes)
library(leaflet)
library(DT)
library(shinyjs)
ui <- dashboardPage(
    dashboardHeader(title = "Basic List"),

    dashboardSidebar(div(id = 'tab1_sidebar',
                         sidebarMenu(
                             textInput("text", "Text input:")
                         ))),
    dashboardBody(useShinyjs(),
                  theme = shinytheme("spacelab"),
                  fluidRow(
                      HTML("<div class='col-sm-12' style='min-width: 400px !important;'>"),
                      column(dataTableOutput(outputId = "table"), width = 12)
                  ))
)

server.R

library(shiny)
library(leaflet)
library(DT)
csvBase <-
    read.csv(
        'https://docs.google.com/spreadsheets/d/e/2PACX-1vSfiMFwfucJl7CXEufaGcZjRS8B6cRZgsHngkqx5moe5edBGtyCc-cQW4Oe_7kdDC0zcdXRBAbWA0GA/pub?output=csv',
        header = TRUE,
        sep = ",",
        stringsAsFactors = F
    )
shinyServer(function(input, output, session) {
    addClass(selector = "body", class = "sidebar-collapse")
    selected_row <- reactive({
        if (!is.null(input$rows)) {
            shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
        } else {
            shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
        }
    })
    output$table <- DT::renderDataTable(
        DT::datatable({
            data <- csvBase
        },
        class = "display nowrap compact",
        selection = "single",
        filter = "top",
        editable = TRUE,
        rownames = FALSE,
        extensions = c('Buttons', 'KeyTable'),
        options = list(
            keys = TRUE,
            stateSave = TRUE,
            searchHighlight = TRUE,
            scrollX = TRUE,
            dom = 'Blfrtip',
            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
            columnDefs = list(list(
                visible = FALSE, targets = c()
            ))
        ))
    )
})

运行该应用程序时,边栏将自动关闭。

请注意,该应用已启用键盘扩展。

我试图将焦点设置到数据表中的第一个单元格,然后UPON将鼠标移到上方或下方的行中,边栏(如果打开)关闭然后打开。

“打开/关闭”的原因是最终用侧重于单个数据表行的数据填充侧边栏文本输入。

任何建议都值得赞赏。

0 个答案:

没有答案