简体   繁体   English

使用dplyr对Heatmap进行排序

[英]Sort Heatmap with dplyr

I'm working on building an interactive heatmap Shiny app and would like select columns to sort from a drop down menu (example: sort "mpg" or "wt" in mtcars from low to high). 我正在构建一个交互式热图Shiny应用程序,并希望从下拉菜单中选择列进行排序(例如:在mtcars中从低到高排序“mpg”或“wt”)。 I'm implementing the heatmap with the d3heatmap package and want to reactivity short the dataframe using dplyr . 我正在使用d3heatmap包实现热图,并希望使用dplyr使数据帧的反应性变短。 I get the following error when running the code: 运行代码时出现以下错误:

Error in eval(substitute(expr), envir, enclos) : invalid subscript type 'closure' eval中的错误(替换(expr),envir,enclos):无效的下标类型'closure'

I have also tried using reactiveValues instead of reactive and included the code as a comment. 我也尝试使用reactiveValues而不是reactive ,并将代码作为注释包含在内。 Using the reactiveValues approach I get the following error: 使用reactiveValues方法我收到以下错误:

Warning: Unhandled error in observer: invalid subscript type 'closure' 警告:观察者中未处理的错误:无效的下标类型'closure'

Any help getting the heatmap sorting to work would be greatly appreciated! 任何有助于热图分类工作的帮助将不胜感激!

app.R app.R

library(ggplot2)
library(d3heatmap)
library(dplyr)
library(shiny)

## ui.R
ui <- fluidPage(
  sidebarPanel(
    h5(strong("Dendrogram:")),
    checkboxInput("cluster_row", "Cluster Rows", value=FALSE),
    checkboxInput("cluster_col", "Cluster Columns", value=FALSE),
    selectInput("sort", "Sort By:", c(names(mtcars),"none"), selected="mpg")
  ),
  mainPanel(
    h4("Heatmap"),
    d3heatmapOutput("heatmap", width = "100%", height="600px") ##
  )
)

## server.R
server <- function(input, output) {

#   values <- reactiveValues(df=mtcars)
#   
#   observe({
#     if(input$sort != "none"){
#       values$df <- arrange(mtcars, input$sort)
#     }else{
#       values$df <- mtcars
#     }
#   })

  df_sort <- reactive({
    df <- mtcars
    if(input$sort != "none"){
      df <- arrange(mtcars, input$sort)
    }else{
      df <- mtcars
    }
  })

  output$heatmap <- renderD3heatmap({
    d3heatmap(df_sort(),
      colors = "Blues",
      if (input$cluster_row) RowV = TRUE else FALSE,
      if (input$cluster_col) ColV = TRUE else FALSE,
      yaxis_font_size = "7px"
    ) 
  })
}

shinyApp(ui = ui, server = server)

input$sort is being passed as a character string, so you need to use arrange_ (see the vignette on NSE ). input$sort作为字符串传递,因此您需要使用arrange_ (请参阅NSE上插图 )。

The following should do the trick: 以下应该做的伎俩:

server <- function(input, output) {
  df_sort <- reactive({
    df <- if(input$sort=='none') mtcars else arrange_(mtcars, input$sort)
  })
  output$heatmap <- renderD3heatmap({
    d3heatmap(
      df_sort(),
      colors = "Blues",
      RowV <- if(input$cluster_row) TRUE else FALSE,
      ColV <- if(input$cluster_col) TRUE else FALSE,
      yaxis_font_size = "7px"
    )
  })
}

ui <- fluidPage(
  sidebarPanel(
    h5(strong("Dendrogram:")),
    checkboxInput("cluster_row", "Cluster Rows", value=FALSE),
    checkboxInput("cluster_col", "Cluster Columns", value=FALSE),
    selectInput("sort", "Sort By:", c(names(mtcars),"none"), selected="mpg")
  ),
  mainPanel(
    h4("Heatmap"),
    d3heatmapOutput("heatmap", width = "100%", height="600px") ##
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM