Data processing and plotting on shiny

60 views
Skip to first unread message

Rafael Batista

unread,
Aug 18, 2016, 10:02:31 AM8/18/16
to Shiny - Web Framework for R
Hi everyone. I am sorry if this is a stupid question but here it goes :)

I am using shiny to build an interface so people from work can load .csv files and print some reports. Everything is going fine except that I dont know how to plot data from a matrix that is processed inside shiny app.

On ui.R the user insert the csv file:

fluidRow(fileInput('file2', h5('Upload your long term (Merra) file'),accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')),
               actionButton("Load2", "Load the File"), width = 3),


On server.R I have the following:

#============================== Loading Long Term file:
data_LT <- reactive({
  if(input$Load2 == 0){return()}
  inFile2 <- input$file2
  if (is.null(inFile2)){return(NULL)}
  
  isolate({ 
    input$Load2
    my_data_LT <- read.table(inFile2$datapath, header = FALSE, sep = ";", dec = ".", skip = 6)
    my_data_LT <- my_data_LT[,c("V2","V3","V4")]
    my_data_LT$V2 <- as.POSIXct(strptime(my_data_LT$V2, format ="%Y-%m-%d %H:%M"), tz = "GMT")
    colnames(my_data_LT) <- c("date", "ws", "wd")
    my_data_LT$month <- NULL
    my_data_LT$month <- format(my_data_LT$date, '%m')
    my_data_LT$month <- as.factor(my_data_LT$month)
    print(my_data_LT$month)
    d_m_h <- NULL
    d_m_h <- aggregate(my_data_LT["ws"], format(my_data_LT["date"],"%m-%H"), mean, na.rm=TRUE)
    d_m_h$date <- paste(d_m_h$date, "-2000-25", sep="")
    d_m_h$date <- as.POSIXct(strptime(d_m_h$date, format ="%m-%H-%Y-%d"), tz = "GMT")
    d_m_h$month <- format.Date(d_m_h$date, "%m")
    d_m_h$hour <- format.Date(d_m_h$date, "%H")
    d_m_h$month <- as.numeric(d_m_h$month)
    d_m_h$hour <- as.numeric(d_m_h$hour)
    print(d_m_h)
    
    # generating matrix for heatmap plot:
    mat <- matrix(NA, ncol=12, nrow=24)
    for(imonth in 1:12){
      for(ihour in 1:24) {
        flag <- d_m_h$month == imonth & d_m_h$hour == (ihour-1)
        mat[ihour,imonth] <- d_m_h[flag,2]
        }
    }
    horas <- seq(from = 0, to = 23, by=1)
    meses <- seq(from = 1, to = 12, by=1)
    row.names(mat) <- horas
    colnames(mat) <- meses
    print(mat)
    
    means_lt <- NULL #media mensal
    means_lt <- tapply(my_data_LT$ws, my_data_LT$month, mean) # calculate means
    print(means_lt)
    })
  my_data_LT 
})

So as you can see I generate a new matrix called "mat" for the plotting of a heatmap.

Latter on ui.R I put, to plot 2 heatmaps on same page (code below) but only the first one appears. The sencond one, based on "mat" matrix doesnt appear and shiny does not give any error message.

          tabPanel("Hourly Wind Speed", plotOutput("plot_hourly_windpro"), plotOutput("output$plot_hourly_lt")),

The output part on server.R:

# Outputs:

    
output$plot_hourly_lt <- renderPlot({
  heatmap.2(mat, na.rm = TRUE, Rowv = FALSE, Colv = FALSE, key = FALSE, density.info = "none", trace = "none",
            cellnote = round(mat, 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 dont know why the heatmap is not plotted. Maybe because its inside a reactive object, or maybe I should not refer to object as mat in the renderplot.... or maybe its a ggplot bug.

Does anyone have an ideia to solve this?

Note: sorry not tu put all the code here. Its very big so I thought that no one would like to look at the thing entirely.

Best whishes!

Sigbert Klinke

unread,
Aug 23, 2016, 3:07:28 AM8/23/16
to Shiny - Web Framework for R
Hi,

the usual way to do it would be:

data_LT <- reactive({
 
...

  list
(mat, means_lt)
})


output$plot_hourly_lt
<- renderPlot({
  l
<- data_LT()
  heatmap
.2(l$mat, ...)
})

Instead of delivering a list with two matrices (for the plots I guess) you could make two reactive contexts, each for one plot. Please read the tutorial.

Best Sigbert

Rafael Batista

unread,
Aug 24, 2016, 6:50:55 AM8/24/16
to Shiny - Web Framework for R
Hello Sigbert.

I will try that later today. Thank you very much for your answer.

Best wishes,
Rafael
Reply all
Reply to author
Forward
0 new messages