#' Select a dataset for the correlationdf <- reactive({ switch(input$datasetCor, ……… })
#' Generate a reactive element of the correlation data. This changes based on some input parameters: df() (a gene expression dataset) and input$geneCor corrData <- reactive({
……… })
#' Generate an HTML table view of the correlation data
output$corrData <- renderDataTable({
corr.table <- corrData()
}, options = list(orderClasses = TRUE), callback = "function(table) {
table.on('click.dt', 'tr', function() {
table.$('tr.selected').removeClass('selected');
$(this).toggleClass('selected');
Shiny.onInputChange('rows',
table.rows('.selected').data()[0][0]);
});
}")
# The call back function return the first value of the selected row as a new input called 'input$rows"
#' Generate the scatter plot
output$corrDataPlot <- renderPlot({
input$rows
validate( need(input$geneCor != "" & input$rows!= "","") ) scatter <- ggplot(df(),mapping = aes_string(x = input$gene, y = input$rows)) + geom_point(alpha=.5)
print(scatter)
})dataset1 <- data.frame(a=rnorm(50),b=rnorm(50),c=rnorm(50), d=rnorm(50))
dataset2 <- data.frame(a=rnorm(50),b=rnorm(50),c=rnorm(50), d=rnorm(50))
server <- function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"dataset1" = dataset1,
"dataset2" = dataset2)
})
#' Generate a reactive element of the the correlation data
corrData <- reactive({
data <- datasetInput()
values <- data[ ,input$gene, drop = F]
r <- apply(values, 2, function(x) { apply(data, 2, function(y) { cor(x,y, method = "pearson") })})
corr <- data.frame(Gene = row.names(r),r = round(r,3))
corr
})
#' Generate an HTML table view of the correlation table
output$corrData <- renderDataTable({
corrData()
}, options = list(orderClasses = TRUE), callback = "function(table) {
table.on('click.dt', 'tr', function() {
table.$('tr.selected').removeClass('selected');
$(this).toggleClass('selected');
Shiny.onInputChange('rows',
table.rows('.selected').data()[0][0]);
});
}"
)
#' Generate the correlation plot
output$corrDataPlot <- renderPlot({
input$rows
validate(
need(input$gene != "" & input$rows!= "","Click on a row to see the corresponding correlation plot."))
df <- datasetInput()
aes_scatter <- aes_string(x = input$gene, y = input$rows)
ggplot(df,mapping = aes_scatter) +geom_point(alpha=.5)
})
}
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "dataset", label = "Dataset", choices = c("dataset1","dataset2")),
selectInput(inputId = "gene", label = "Gene", choices = c("a","b","c"))
),
mainPanel(
dataTableOutput(outputId = "corrData"),
plotOutput("corrDataPlot"))
)
))
shinyApp(ui = ui, server = server)--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/e0503e6a-bc41-4ce0-a45a-bf8e84a5657c%40googlegroups.com.