在闪亮的表格中嵌入链接

时间:2013-09-09 14:51:24

标签: r shiny

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

有什么想法吗?

注意:另一种方法是让我使用php和HTML UI,但是我需要能够让R返回一个数组,并且让我能够在html中引用该数组的元素。那更容易吗?

1 个答案:

答案 0 :(得分:3)

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

通常,您需要两个JavaScript函数(已经实现了闪亮但没有详细记录)与服务器进行通信:

Shiny.addCustomMessageHandler jiny中的Shiny.onInputChange

这是我的代码:

<强> 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)
})"))
  )  
)

我刚用过&#34; alert(x)&#34;显示来自服务器的返回值。您可以使用一个好的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]))}
    })
})
相关问题