我想创建一个闪亮的表格,以便表格的每个元素都是指向新页面的超链接,但新页面(由闪亮创建)知道单击了哪个单元格。因此,例如,我单击单元格 (i,j),这会将我带到一个新页面,其中的绘图基于我选择的 i 和 j 值。我可以使用 php 和/或 cookie 来做到这一点,但如果可能的话,我正在寻找一个闪亮的解决方案。
有任何想法吗?
注意:对我来说,另一种方法是使用 php 和 HTML UI,但是我需要 R 能够返回一个数组,并且我能够在 html 中引用该数组的元素。这更容易吗?
在回答您的问题之前,我要求您将您的闪亮更新到最新版本以避免不良错误。
通常,您需要两个 JavaScript 函数(已经在 shiny 中实现但没有很好的文档记录)与服务器通信:
javascript 中的Shiny.addCustomMessageHandler和Shiny.onInputChange
这是我的代码:
用户界面
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);
服务器.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]))}
})
})