3

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

eval 中的错误(substitute(expr)、envir、enclos):无效的下标类型“闭包”

我也尝试过使用reactiveValues代替reactive并将代码作为注释包含在内。使用该reactiveValues方法我得到以下错误:

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

任何帮助热图排序工作将不胜感激!

应用程序.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)
4

1 回答 1

4

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") ##
  )
)
于 2016-01-08T05:00:22.013 回答