在闪亮的表格中嵌入链接

use*_*811 11 r shiny

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

有任何想法吗?

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

Mah*_*iha 3

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

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

javascript 中的Shiny.addCustomMessageHandler和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)
})"))
  )  
)
Run Code Online (Sandbox Code Playgroud)

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

var myWindow = window.open("", "MsgWindow", "width=200, height=100");
myWindow.document.write(x);
Run Code Online (Sandbox Code Playgroud)

服务器.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]))}
    })
})
Run Code Online (Sandbox Code Playgroud)