0

Hi can anyone see what is the problem with the code below?

 output$zscore_chart <- renderPlot({

   xvals <- 2:186 
   req(input$countrySelectZScore)
   idx_country = which(input$countrySelectZScore == esiCountries)



   max_comp_z <- max(esiData_DF[idx_country, 2:186], na.rm = TRUE)

   overall_max_z <- max(max_comp_z, na.rm = TRUE)

   foo = ts(esiData_DF[idx_country, 2:186], frequency = 12, start = 2001)
   dates = seq(as.Date("2001-01-01"), by = "month", along = foo)

   plot(x = 2:186, y = esiData_DF[idx_country, 2:186], type = "l", 
        xlab = "", ylab = "", col = "grey20", ylim = c(-2, overall_max_z),
        lwd=3,las=2)
    mtext("Quarterly percentage changes", side = 3, adj = 0, line = 0.1, 
      cex = 1, font = 0.5)
    axis(1, at = xvals, label = dates, cex.axis = 1, las = 2)
    mtext("Economic Sentiment Indicators", side = 3, adj = 0, 
          line = 1.2, cex = 2, font = 2)

    legend(
      "bottom", 
      lty = c(1,1), 
      lwd = c(3,3), 
      col = c("grey20", "green2"), 
      legend = c("Economic Sentiment Indicator", "GDP growth"),
      bty = "n",
      xjust = 0.5,
      yjust = 0.5,
      horiz = TRUE
    )
  }, height = 525)

esiData_DF is the DF used to index and plot the correct data. The dataframe has the country names down the left hand side with the dates, monthly across the top. I need the plot to render but it wont when I run the app. Any ideas?

enter image description here

The data continues to the right, up to May 2017 monthly.

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
OwlieW
  • 217
  • 6
  • 13
  • 1
    Can you include a small example of the data or a reproducible example so that people can help can help you? – RobertMyles Jun 26 '17 at 12:37
  • have added it, let me know if you need any more information – OwlieW Jun 26 '17 at 12:39
  • Try to create the plot without shiny first. Or have you managed to do that already? – Gregor de Cillia Jun 26 '17 at 12:39
  • I think you should put your entire code `server`+ `ui`. Do you build `esiData_DF` before or after the beggining of `server` code ? – Smich7 Jun 26 '17 at 12:42
  • I build it before, it comes from excel in the format in the picture above, but with more columns to the right – OwlieW Jun 26 '17 at 12:43
  • @OwlieW I apologise, I should have been clearer. When I said "an example of the data", I meant in this fashion: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – RobertMyles Jun 26 '17 at 12:44
  • It is just a 6 by 186 dataframe, with the countries organised on the left and the data to the right – OwlieW Jun 26 '17 at 12:48
  • And `esiCountries` ? Is it build in `server` ? It could be great if you put the entire code ! You'll get a faster answer. – Smich7 Jun 26 '17 at 12:59
  • Thank you, I think I have it working now, but I have another issue with the date axis, now I have the plot showing but the chart is trying to show each month of the dates, how do I get it to only show years, or just one month out of each year to stop them from bunching up? – OwlieW Jun 26 '17 at 13:00
  • 1
    put `xaxt='n'` in your plot parameters. And after write `axis(1,at=seq(1,round(length(xvals)/12))*12,label=paste('Jan',2000+seq(1,round(length(xvals)/12),sep='')))` – Smich7 Jun 26 '17 at 13:11
  • 1
    add `las=2` in `axis` if you want xlabels vertically – Smich7 Jun 26 '17 at 13:13
  • Excellent, that worked perfectly, thank you very much – OwlieW Jun 26 '17 at 13:16
  • 1
    No problem ! Have a nice time with shiny ! – Smich7 Jun 26 '17 at 13:23

0 Answers0