标签之间的R闪亮构建链接 [英] R shiny build links between tabs

查看:182
本文介绍了标签之间的R闪亮构建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[代码]

library(shiny)

server <- function(input, output) {
  output$iris_type <- renderDataTable({
    data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>"))
  })
  output$filtered_data <- renderDataTable({iris})
}

ui <- shinyUI(fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Iris Type", dataTableOutput("iris_type")),
      tabPanel("Filtered Data", dataTableOutput("filtered_data"))
    )
  )
))

shinyApp(ui = ui, server = server)

[问题]

我想链接第一个选项卡上的 DataTable 输出到第二个选项卡。例如,当我点击 setosa 时,接下来显示的是第二个选项卡,其中 iris 数据集仅包含 setosa 。它应该从R执行这段代码: iris [iris $ Species ==setosa,] 。它应该适用于 iris 中的其他物种

I am trying to link the DataTable output on first tab to the second tab. For example, when I click on setosa, the next thing showing up is second tab with iris dataset containing only setosa. It should execute this piece of code from R: iris[iris$Species=="setosa",]. It should work for other Species in iris too.

如何通过点击建立链接并运行该R命令?

How can I build up the link and run that R command by clicking?

[更新回答]

如果你有不同的布局并且需要具体,你可以做以下的事情。

In case you have a different layout and needs to be specific, here is what you can do.


  1. 您的 DataTable 回调函数:

callback =
"function(table) {
   table.on('click.dt', 'tr', function() {
     Shiny.onInputChange('rows', table.row(this).data()[0] );
     $(\".tabbable .nav.nav-tabs li a:contains('Filtered Data')\").click();
   });
 }"


  • 你的 R 代码:

    output$filtered_data <- renderDataTable({
      tagString <- input$rows
      rawTags <- gsub("</a>", "", gsub("<a href='#filtered_data'>", "", tagString))
    
      if (identical(tagString, character(0))) {
        iris
      } else {
        ...
      }
    })
    



  • 推荐答案

    更容易拥有单击第一个表的行上的函数。您可以添加一个回调,
    查找表中行的单击。当观察到点击时,行索引被发送到闪亮的被动输入:

    It is easier to have the click function on the row of the first table. You can add a callback that looks for a click on the rows of the table. When a click is observed the row index is sent to a shiny reactive input:

    library(shiny)
    
    server <- function(input, output) {
      output$iris_type <- renderDataTable({
        data.frame(Species=paste0("<a href='#filtered_data'>", unique(iris$Species), "</a>"))
      },
      callback = "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 <- 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", dataTableOutput("iris_type")),
          tabPanel("Filtered Data", dataTableOutput("filtered_data"))
        )
      )
    ))
    
    shinyApp(ui = ui, server = server)
    

    这篇关于标签之间的R闪亮构建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆