我希望能够改变的大小和 colored颜色 的标记在一个情节火山情节输入.用户写下基因的名称(Gene1等),对应于该基因的标记将更新,改变其大小(例如,更改为20)和 colored颜色 (更改为黄色).然后,当 Select 另一个基因时,先前的标记应返回到其先前的状态,新基因将突出显示.
这是我try 使用restyle
,但我没有让它工作:
# Simulate some data
set.seed(123) # For reproducibility
genes <- paste0("Gene", 1:100)
estimates <- rnorm(100, 0, 1)
metric <- runif(100, 0, 0.05)
contrasts <- sample(c("Contrast1", "Contrast2"), 100, replace = TRUE)
groups <- ifelse(estimates < -1 & metric <= 0.05, "Downregulated",
ifelse(estimates > 1 & metric <= 0.05, "Upregulated", "Not significant"))
# Create a dataframe
data <- data.frame(gene = genes, estimate = estimates, metric = metric, contrast = contrasts, group = groups)
library(shiny)
library(plotly)
ui <- fluidPage(
titlePanel("Interactive RNAseq Data Visualization"),
sidebarLayout(
sidebarPanel(
textInput("selectedGene", "Enter Gene Name:", value = ""),
actionButton("highlightBtn", "Highlight")
),
mainPanel(
plotlyOutput("volcano_plot")
)
)
)
server <- function(input, output, session) {
# Define your reactive data
reactive_data <- reactive({
# Your code to generate the data goes here.
# For the sake of example, let's assume you have a dataframe 'df' that includes a 'gene' column.
df <- data # Replace 'data' with the actual data frame variable
return(df)
})
# Output the plot
output$volcano_plot <- renderPlotly({
plot_ly(
data,
x = ~estimate,
y = ~metric,
text = ~gene,
type = "scatter",
mode = "markers",
color = ~group,
marker = list(opacity = 0.5, line = list(color = "black", width = 1)),
hoverinfo = "text",
source = "volcano_plot" # important for plotlyProxy to know which plot to target
) %>% layout(
title = "Volcano Plot",
xaxis = list(title = "Estimate"),
yaxis = list(title = "Metric")
)
})
observeEvent(input$highlightBtn, {
req(input$selectedGene) # Ensure a gene is selected
# Access the current reactive data
df <- reactive_data()
# Find the index of the gene to highlight
selected_gene <- input$selectedGene
gene_index <- which(df$gene == selected_gene)
# Make sure there is only one gene to highlight and it's in the plot
if(length(gene_index) == 1) {
# Calculate the correct index for JavaScript
gene_index_js <- gene_index - 1
# Restyle the plot to highlight the selected gene
plotlyProxy("volcano_plot", session) %>%
plotlyProxyInvoke("restyle",
list(
'marker.size' = list(20),
'marker.color' = list('yellow'),
'marker.opacity' = list(1)
),
gene_index_js
)
}
}, ignoreNULL = FALSE)
}
shinyApp(ui = ui, server = server)
我try 使用restyle
来改变用户输入的情节,但它没有工作.图表根本没有变化.我也试过update
,但它给了一个非常奇怪的行为,我无法控制.
提前谢谢!