端口heatmap.2绘图闪亮(找不到对象的问题)

时间:2016-06-04 19:15:10

标签: r shiny heatmap

我在尝试将手动R脚本移植到闪亮时遇到了一些问题。原始脚本读取数据表并使用gplots包绘制热图(heatmap.2)。它完美地运作。这是输入文件: enter image description here

绘制热图的原始代码是:

library(RColorBrewer)
library(gplots)

#setwd("C:\\Users\\rafael.batista\\Documents\\shiny_locally\\dist\\shiny\\workplace")
my_data_24.12 <- read.table("24-12_windPRO_chicoloma.txt", header = FALSE, sep = "\t", dec = ".", stringsAsFactors=FALSE) # reading 24-12 table copied from windPRO
my_data_24.12 <- my_data_24.12[-c(1,26),]
my_data_24.12 <- my_data_24.12[,-c(1, 14)]
my_data_24.12 <- data.matrix(my_data_24.12)

min.v <- min(my_data_24.12)
max.v <- max(my_data_24.12)

# Agora o heatmap:
rnames <- c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00")
rownames(my_data_24.12) <- rnames
cnames <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
colnames(my_data_24.12) <- cnames

my_palette <- colorRampPalette(c("chartreuse4", "yellow", "red", "darkmagenta"))(n = 199)

col_breaks = c(seq(min.v,9.999,length=100),  # for 1st interval
               seq(10,11.999,length=50),              # for 2nd interval
               seq(12,max.v,length=50))              # for 3rd interval


heatmap.2(my_data_24.12, na.rm = TRUE, Rowv = FALSE, Colv = FALSE, key = FALSE, density.info = "none", trace = "none",
          cellnote = round(my_data_24.12, digits = 1), notecol = "black", xlab = "Months", ylab = "Hours",
          colsep=c(1,2,3,4,5,6,7,8,9,10,11,12), srtCol=0,
          rowsep = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23),
          col = my_palette,
          margins =c(3.5,5),
          lhei = c(0.05,1),
          lwid = c(0.02, 0.7),
          adjCol = c(0.5,1),
          breaks=col_breaks,
          sepcolor = "white")

现在问题。我试图通过以下代码将其移植到闪亮的地方:(这是server.r)

data_24.12 <- reactive({      #(file will be loaded by fileInput in ui.r)
  if(input$Load3 == 0){return()}
  inFile3 <- input$file3
  if (is.null(inFile3)){return(NULL)}


  isolate({ 
    input$Load3
    my_data_24.12 <- read.table(inFile3$datapath, header = FALSE, sep = "\t", dec = ".", stringsAsFactors=FALSE) # reading table
    my_data_24.12[-c(1,26),] #doing some processing...
    my_data_24.12 <- my_data_24.12[,-c(1, 14)]
    my_data_24.12 <- data.matrix(my_data_24.12)

    rownames(my_data_24.12) <- c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00")
    colnames(my_data_24.12) <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

    my_palette <- colorRampPalette(c("chartreuse4", "yellow", "red", "darkmagenta"))(n = 199)
    min.v <- min(my_data_24.12)
    max.v <- max(my_data_24.12)

    col_breaks = c(seq(min.v,9.999,length=100),   # for 1st interval
                   seq(10,11.999,length=50),      # for 2nd interval
                   seq(12,max.v,length=50))       # for 3rd interval

    })


  my_data_24.12 
})
#==============================


# Outputs:

output$plot_hourly_windpro <- renderPlot({
   heatmap.2(data_24.12(), na.rm = TRUE, Rowv = FALSE, Colv = FALSE, key = FALSE, density.info = "none", trace = "none",
            cellnote = round(my_data_24.12, digits = 1), notecol = "black", xlab = "Months", ylab = "Hours",
            colsep=c(1,2,3,4,5,6,7,8,9,10,11,12), srtCol=0,
            rowsep = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23),
            col = my_palette,
            margins =c(3.5,5),
            lhei = c(0.02,0.95),
            lwid = c(0.02,0.95),
            adjCol = c(0.5,0.2),
            breaks=col_breaks,
            sepcolor = "white")})

我收到以下错误:

  

聆听http://127.0.0.1:6897聆听   警告:重复错误:找不到对象'col_breaks'   堆栈跟踪(最里面的第一个):
          78:重复的           77:热图.2           76:renderPlot [/home/rafael/Documentos/wobben/shiny_version/shiny/server.R#81]           68:输出$ plot_hourly_windpro            1:runApp

因为看起来有光泽并没有找到一些物体。我现在一定是数据处理的东西......但我不知道出了什么问题。

有人可以帮忙吗? (我没有在这里粘贴ui.r,因为我很确定问题出在server.r中)

非常感谢!

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题,但有一个plolty元素,你应该尝试避免绘图代码中的反应数据集,也许试试这个:

... output$plot_hourly_windpro <- renderPlot({

data.plot<-data_24.12()
   heatmap.2(data.plot, na.rm = TRUE, ...

希望它有所帮助;)