我希望能够改变的大小和 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,但它给了一个非常奇怪的行为,我无法控制.

提前谢谢!

推荐答案

另一个选项是使用addTracesdeleteTraces添加另一个轨迹以突出显示所选基因,并在 Select 另一个基因时删除此轨迹:

observeEvent(input$highlightBtn,
  {
    req(input$selectedGene) # Ensure a gene is selected

    # Access the current reactive data
    df <- reactive_data()
    df <- df[df$gene %in% input$selectedGene, ]

    plotlyProxy("volcano_plot", session) |>
      plotlyProxyInvoke(
        "deleteTraces",
        list(3) # Delete fourth trace
      ) |>
      plotlyProxyInvoke(
        "addTraces",
        list(
          x = list(df$estimate),
          y = list(df$metric),
          text = list(df$gene),
          type = "scatter",
          mode = "markers",
          marker = list(
            size = 20,
            color = "yellow",
            opacity = 1,
            line = list(color = "black", width = 1)
          ),
          showlegend = FALSE,
          name = "highlight"
        )
      )
  },
  ignoreNULL = FALSE
)

enter image description here

R相关问答推荐

按块将载体转换为矩阵-reshape

跨列应用多个摘要函数:summerise_all:列表对象无法强制为double类型'

变量计算按R中的行更改

检测(并替换)字符串中的数学符号

R创建一个数据透视表,计算多个组的百分比

行式dppr中的变量列名

名称输出pmap on tible

用derrr在R中查找组间的重复项

如何使下一个按钮只出现在Rshiny 的一段时间后?""

如何调整曲线图中的y轴标签?

在使用bslb和bootstrap5时,有没有办法更改特定dt行的 colored颜色 ?

线性模型斜率在减少原始数据时提供NA

如何指定我的函数应该查找哪个引用表?

按多列统计频次

Data.table';S GForce-将多个函数应用于多列(带可选参数)

根据纬度和距离连接两个数据集

Geom_arcbar()中出错:找不到函数";geom_arcbar";

分隔日期格式为2020年7月1日

conditionPanel不考虑以下条件

修复标签重叠和ggploy内的空间