r-使用dplyr对热图进行排序



我正在构建一个交互式热图Shiny应用程序,并希望从下拉菜单中选择要排序的列(例如:在mtcars中从低到高排序"mpg"或"wt")。我正在使用d3heatmap包实现热图,并希望使用dplyr缩短数据帧的反应性。我在运行代码时得到以下错误:

eval中出错(替换(expr)、envir、enclos):无效的下标类型"关闭"

我还尝试使用reactiveValues而不是reactive,并将代码作为注释包含在内。使用reactiveValues方法,我得到以下错误:

警告:观察者中出现未处理的错误:无效的下标类型"闭包"

如果能为热图排序工作提供任何帮助,我们将不胜感激!

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是作为字符串传递的,因此需要使用arrange_(请参阅NSE上的渐晕)。

以下应该可以做到:

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") ##
  )
)

相关内容

  • 没有找到相关文章