Ответ 1
Прежде чем отвечать на ваши вопросы, я прошу вас обновить свой блеск до последней версии, чтобы избежать нежелательных ошибок.
Как правило, для взаимодействия с сервером вам нужны две функции JavaScript (которые уже реализованы в блестящей, но плохо документированной):
Shiny.addCustomMessageHandler и Shiny.onInputИзменить в 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]))}
})
})