我正在尝试使用导航面板创建这个 Shiny 的应用程序。 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 Shiny: Handle Action Buttons in Data Table
我猜关键是在DT里面创建actionLinks的时候加上on.click参数,让click触发事件。并且 on.click 还可以为 actionLink/button 分配唯一的按钮 id。然后在 observeEvent 中,简单地将表达式作为 input$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
})
}
)
```
关于javascript - DataTable 中的 Actionbutton 不起作用,Shiny DT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831190/