2 plots in the same page but only 1 appears

23 views
Skip to first unread message

Rafael Batista

unread,
Sep 29, 2016, 1:12:50 PM9/29/16
to Shiny - Web Framework for R

Hello. I am doing a shiny app that process some data (input by the user) and plot some graphs. I am want to display 2 plots in the same window.. however only the first one appears. Code bellow:

#============================== Loading Short Term file: user input one file  
data_ST
<- reactive({
   
if(input$Load1 == 0){return()}
    inFile1
<- input$file1
   
if (is.null(inFile1)){return(NULL)}
   
    isolate
({
      input$Load1
      my_data_ST
<- read.table(inFile1$datapath, header = FALSE, sep = ";", dec = ".", skip = 6)
      my_data_ST
<- my_data_ST[,c("V2","V3","V4")]
      my_data_ST$V2
<- as.POSIXct(strptime(my_data_ST$V2, format ="%Y-%m-%d %H:%M"), tz = "GMT")
      colnames
(my_data_ST) <- c("date", "ws", "wd")
      my_data_ST$month
<- NULL
      my_data_ST$month
<- format(my_data_ST$date, '%m')
      my_data_ST$month
<- as.factor(my_data_ST$month)
      means_st
<- NULL
      means_st
<- tapply(my_data_ST$ws, my_data_ST$month, mean) # calculate means
     
print(means_st) #first verification - print to see if its ok...
     
})
    my_data_ST
})
#============================== Loading Long Term file: user insert another 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)
   
# generating matrix for heatmap plot:
    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) # second verification - print to see if its ok...

   
# generating a matrix to plot later
    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) # third verification - print to see if its ok...
   
})
   
list(my_data_LT, d_m_h, mat)
})

According to my "verification points" processing of data is correct. Later for the plotting I have the following:

# First plot (this one appears normally)
output$plot_box_st
<- renderPlot({
 
if(is.null(data_ST())){return ()}
  boxplot
(ws~month, data = data_ST(), main = "Short term monthly wind speed", xlab = "Months", ylab = "Wind Speed (m/s)", col = "MediumAquamarine", outline = FALSE, ylim=c(0,25))
})

# Second plot (this one doesnt appear)
output$plot_box_lt
<- renderPlot({
 
if(is.null(data_LT()$my_data_LT)){return ()}
  boxplot
(ws~month, data = data_LT()$my_data_LT, main = "Long term monthly wind speed", xlab = "Months", ylab = "Wind Speed (m/s)", col = "MediumAquamarine", outline = FALSE, ylim=c(0,25))
})


Its weird that shiny doesnt give me any kind of error message/warning. Everything is crystal clear in the rstudio console. Did someone know why the second plot isnt apppearing? My only clue about this is related to the bold part of the code. I have 3 reactive objects (my_data_LT, d_m_h and mat) in a list and maybe this is not the correct way to do it.

jeremiah rounds

unread,
Sep 29, 2016, 1:46:08 PM9/29/16
to Rafael Batista, Shiny - Web Framework for R
I am guessing this return:

   list(my_data_LT, d_m_h, mat)

perhaps this instead:
list(my_data_LT = my_data_LT, ...)





--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/0bbf992c-501d-4c1d-ac56-07b192114640%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rafael Batista

unread,
Sep 30, 2016, 1:56:15 PM9/30/16
to Shiny - Web Framework for R
Yeah it works. Thanks a lot!
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages