【问题标题】:Shiny: Expand right hand sidebar by clicking on the datatableShiny:通过单击数据表展开右侧边栏
【发布时间】:2019-02-18 08:30:15
【问题描述】:

我创建了一个看起来像这样的闪亮应用:

## Library
library(shiny)
library(shinydashboardPlus)
library(shinyjs)
library(DT)

data <- cbind(
  seq(from = 1, to = 30, by = 1),
  sample(seq(from = 100, to = 300, by = 10), size = 30, replace = TRUE),
  sample(seq(from = 1, to = 100, by = 9), size = 30, replace = TRUE),
  sample(seq(from = 50, to = 60, by = 2), size = 30, replace = TRUE),
  sample(seq(from = 100, to = 130, by = 1), size = 30, replace = TRUE)
) %>% 
  as.data.frame()

names(data) <- c("Colimn 1", "Colimn 2", "Colimn 3", "Colimn 4", "Colimn 5")

## UI
ui <- dashboardPagePlus(
  dashboardHeaderPlus(
    enable_rightsidebar = TRUE,
    rightSidebarIcon = "angle-left"
  ),
  dashboardSidebar(),
  dashboardBody(dataTableOutput("dataTable")),
  rightSidebar()
)

## Server
server <- function(input, output) { 
  output$dataTable <- renderDataTable({
    datatable(data,
    rownames= FALSE,
    selection = "single",
    escape = FALSE,
    ## Get the name of the row on click
    callback = JS("table.on('click.dt', 'tr',
                  function() {
                  Shiny.onInputChange('rows', table.rows(this).data().toArray());
                  });")
    )
  })

  observeEvent(!is.null(input$rows),
               {
              ????
               })
}

shinyApp(ui, server)

我想要的是当用户点击数据表行时,右侧边栏会展开,其中包含有关该行的更多详细信息。

一旦用户点击数据表的任何一行,侧边栏应该打开,如果用户点击任何其他行,它应该保持打开状态。只有当顶部的按钮(已经存在)被按下以折叠它时,它才应该折叠。

我尝试在shinyjs::removeClass 周围玩,但我没能成功。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    使用library(shinyjs)将shinyjs::useShinyjs()添加到仪表板主体,并在服务器函数中添加:

    selected_row <- reactive({
        if(!is.null(input$rows)){
          shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
          data %>% filter(`Colimn 1`== input$rows[1])
        }
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2018-09-03
      • 1970-01-01
      • 2022-07-19
      • 2020-03-04
      相关资源
      最近更新 更多