【问题标题】:Embedding links in shiny tables在闪亮的表格中嵌入链接
【发布时间】:2013-09-09 14:51:24
【问题描述】:

我想创建一个闪亮的表格,以便表格的每个元素都是指向新页面的超链接,但新页面(由闪亮创建)知道单击了哪个单元格。因此,例如,我单击单元格 (i,j),这会将我带到一个新页面,其中包含基于我选择的 i 和 j 值的图。我可以使用 php 和/或 cookies 来做到这一点,但如果可能的话,我正在寻找一个闪亮的解决方案。

有什么想法吗?

注意:对我来说,另一种选择是使用 php 和 HTML UI,但是我需要 R 能够返回一个数组,并且我能够在 html 中引用该数组的元素。这样容易吗?

【问题讨论】:

  • 我在 Shiny google 论坛上问过这个问题。但没有答案。
  • 我遇到了同样的问题,但是 Shiny 不支持这样的东西,所以我用 HTML 代码中的 java-script 创建了我的表
  • @Paul 我正在寻找类似的东西。是否可以在闪亮的 dataTableOutput 中创建包含链接的列?
  • @Thomas 我认为是的,但我停止在我的项目中使用闪亮的 dataTableOutput 并开始使用 JavaScript 创建表格,因为它提供了更多可能性。我建议你也这样做

标签: r shiny


【解决方案1】:

在回答您的问题之前,我要求您将您的 Shiny 更新到最新版本以避免出现不良错误。

通常你需要两个 JavaScript 函数(已经在 shiny 中实现但没有很好的文档记录)来与服务器通信:

Shiny.addCustomMessageHandlerShiny.onInputChange in javascript

这是我的代码:

ui.R

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

# Define the overall UI
shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),

    # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4, 
             selectInput("man", 
                         "Manufacturer:", 
                         c("All", 
                           unique(as.character(mpg$manufacturer))))
      ),
      column(4, 
             selectInput("trans", 
                         "Transmission:", 
                         c("All", 
                           unique(as.character(mpg$trans))))
      ),
      column(4, 
             selectInput("cyl", 
                         "Cylinders:", 
                         c("All", 
                           unique(as.character(mpg$cyl))))
      )        
    ),
    # Create a new row for the table.
    fluidRow(
      dataTableOutput(outputId="table")
    ),
    tags$head(tags$script("var f_fnRowCallback = function( nRow, aData, iDisplayIndex,     iDisplayIndexFull ){
      $('td', nRow).click( function(){Shiny.onInputChange('request_ij',     [$(this).parent().index(),$(this).index()])} );
}                                        

Shiny.addCustomMessageHandler('showRequested_ij', function(x) { 

    alert(x)
})"))
  )  
)

我只是使用“alert(x)”来显示来自服务器的返回值。您可以使用一个好的 JavaScript 函数来更好地表示您的数据。如果您想打开新窗口,您可以使用:

var myWindow = window.open("", "MsgWindow", "width=200, height=100");
myWindow.document.write(x);

Server.r

library(shiny)
library(ggplot2)
shinyServer(function(input, output, session) {

  # Filter data based on selections
  output$table <- renderDataTable({
    data <- mpg
    if (input$man != "All"){
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All"){
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All"){
      data <- data[data$trans == input$trans,]
    }
    data
  },options =  list(
  fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull )     {f_fnRowCallback( nRow, aData, iDisplayIndex, iDisplayIndexFull ) }")))
  observe({   
    if(!is.null(input$request_ij)){
    session$sendCustomMessage(type = "showRequested_ij", paste( "row:     ",input$request_ij[1],"    col: ",input$request_ij[2]))}
    })
})

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2020-08-04
    • 2021-09-30
    相关资源
    最近更新 更多