簡體   English   中英

閃亮的DT,DataTable中的Actionbutton不起作用

[英]Actionbutton within DataTable doesn't work, Shiny DT

我正在嘗試使用導航面板創建此閃亮的應用程序。 navBar上的第一個選項卡將是一個摘要表,在其中我要使第一列的內容可單擊並導航至其詳細信息選項卡的內容。 我已經使文本成為超鏈接,但是我想知道如何真正使onClick導航起作用。

_______________________更新問題______________________

因此,我根據收到的建議進行了一些更新。 我只使用了actionLink()函數和ObserveEvent({updateNavPanel})組合

似乎主要的問題是DT表中的actionLink無法正常工作,但在外部卻可以正常工作。 也許我只是缺少一些回調函數來讓它識別DT中的按鈕?

下面是代碼:Summary1顯示有效的操作鏈接,Summary2顯示DT中不起作用的操作鏈接。

---
title: "Fruit Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r global, include=FALSE, echo=FALSE}

# import libraries

library(DT)
library(shiny)
library(tidyverse)
library(shinythemes)
library(shinydashboard)
```

```{r, echo = FALSE}

shinyApp(

  ui <- fluidPage(
    titlePanel("Fruit Dashboard"), 
    theme = shinytheme("united"),
    navlistPanel(id='nav', widths = c(2, 10),
              tabPanel('Summary1', actionLink('apple', 'go to apple')),
             tabPanel('Summary2', dataTableOutput('summary')),
              tabPanel("apple", dataTableOutput('apple')),
              tabPanel("orange", dataTableOutput('orange')),
              tabPanel("watermelon", dataTableOutput('watermelon'))
    )

  ),

  server <- function(input, output, session) {

    observeEvent(input$apple, {

    updateNavlistPanel(session, "nav", 'apple')

  })


    output$summary <- renderDataTable({

      data <- data.frame('Fruit' = c('apple', 'orange', 'watermelon'),
                     'Count' = c(3,4,5)) %>%
        mutate(Fruit = paste0("<a id='", Fruit, "' hrep='#' class='action-button'>", Fruit, "</a>" ))

      table <- datatable(data, escape = FALSE , selection = 'none')

      table
    })

    output$apple <- renderDataTable({

      data <- data.frame('Total#' = 3, 'Organic#'= 2, 'Conventional#'=1)

      table <- datatable(data, escape = FALSE)
      table
    })


    output$orange <- renderDataTable({

      data <- data.frame('Total#' = 4, 'Organic#'= 3, 'Conventional#'=1)

      table <- datatable(data, escape = FALSE)
      table
    })

    output$watermelon <- renderDataTable({

      data <- data.frame('Total#' = 5, 'Organic#'= 2, 'Conventional#'= 3)

      table <- datatable(data, escape = FALSE)
      table
    })
  }
)


```

從以下答案中獲得啟發: R閃亮:處理數據表中的操作按鈕

我想關鍵是在DT中創建actionLinks時添加on.click參數,以便單擊觸發事件。 並且on.click還可以為actionLink / button分配唯一的按鈕ID。 然后在observeEvent中,只需將表達式作為輸入$ selected_button。 請參閱下面的完整代碼:

---
title: "Fruit Dashboard"
output: 
flexdashboard::flex_dashboard:
   orientation: columns
   vertical_layout: fill
runtime: shiny
---

```{r global, include=FALSE, echo=FALSE}

# import libraries

library(DT)
library(shiny)
library(tidyverse)
library(shinythemes)
library(shinydashboard)

df <- data.frame('Fruit' = c('apple', 'orange', 'watermelon'),
                     'Count' = c(3,4,5))

shinyInput <- function(FUN, len, id, label, ...) {
  inputs <- character(len)

  for (i in seq_len(len)) {
    label <- df$Fruit[i]
    inputs[i] <- as.character(FUN(paste0(id, i),label=label, ...))
  }
  inputs
 }

```

```{r, echo = FALSE}

shinyApp(

  ui <- fluidPage(
    titlePanel("Fruit Dashboard"), 
    theme = shinytheme("united"),
    navlistPanel(id='nav', widths = c(2, 10),
              tabPanel('Summary2', dataTableOutput('summary')),
              tabPanel("apple", dataTableOutput('apple')),
              tabPanel("orange", dataTableOutput('orange')),
              tabPanel("watermelon", dataTableOutput('watermelon'))
    )

  ),

  server <- function(input, output, session) {

    output$summary <- renderDataTable({

      data <- df %>%
        mutate(Fruit = shinyInput(actionLink, nrow(df), 'button_', label = Fruit, onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ))

  table <- datatable(data, escape = FALSE , selection = 'none')

  table
    })

observeEvent(input$select_button, {
   selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])

  updateNavlistPanel(session, 'nav', df[selectedRow, 1])
})

output$apple <- renderDataTable({

  data <- data.frame('Total#' = 3, 'Organic#'= 2, 'Conventional#'=1)

  table <- datatable(data, escape = FALSE)
  table
})


output$orange <- renderDataTable({

  data <- data.frame('Total#' = 4, 'Organic#'= 3, 'Conventional#'=1)

  table <- datatable(data, escape = FALSE)
  table
})

output$watermelon <- renderDataTable({

  data <- data.frame('Total#' = 5, 'Organic#'= 2, 'Conventional#'= 3)

  table <- datatable(data, escape = FALSE)
  table
    })
  }
)


```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM