0
votes

I am having some issues trying to port a manual R script to shiny. The original script read a data table and uses gplots package to plot a heatmap (heatmap.2). It works perfectly. This is the input file: enter image description here

The original code to plot the heatmap is:

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

Now the problem. I am trying to port this to shiny with the following code: (this is the 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")})

I am getting the following error:

Listening on http://127.0.0.1:6897
Warning: Error in duplicated: object 'col_breaks' not found
Stack trace (innermost first):
78: duplicated
77: heatmap.2
76: renderPlot [/home/rafael/Documentos/wobben/shiny_version/shiny/server.R#81] 68: output$plot_hourly_windpro 1: runApp

As it seems shiny doesnt find some objects. I now that it must be something with the data processing... but I dont know what is wrong.

Can anyone help? (I didnt paste ui.r here because I am pretty sure that the problem is in server.r)

Many thanks!

1
Just a small sidenote... this is not the ENTIRE server.r code... just part of it. The initial part is missing (library loading... shinySever()...etc). - RafaelRS

1 Answers

0
votes

I had a similar issue but with a plolty element, you should try to avoid the reactive dataset in the plot code, maybe try this:

... output$plot_hourly_windpro <- renderPlot({

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

Hope it helps ;)