Please I am trying to convert the graphs in my shiny app into ggplot2 graphs. I use a reactive function to generate the values of financial ratios, which are plotted in bar graphs and pie charts, e.g. Profit/Turnover. When you load the app, by default, each input field (profit input fields and turnover input fields) contains values, whose ratios are plotted in the graphs. The ggplot2 graph displays when the app is loaded. But, when I clear an input field, say in profit, I get the following error message from my app:
Warning in profit/turnover :
longer object length is not a multiple of shorter object length
Warning: Error in data.frame: arguments imply differing number of rows: 5, 4
131: stop
130: data.frame
129: <reactive:profitability.df> [C:\Users\Idiaye\Documents\R data\mavisanalytic/app.R#446]
113: profitability.df
99: renderPlotly [C:\Users\Idiaye\Documents\R data\mavisanalytic/app.R#451]
98: func
95: func
82: origRenderFunc
81: output$bar
1: shiny::runApp
The app is designed to compute and plot financial ratios over just five years; hence five data input fields each for profit and turnover. Nevertheless, I want a user to be able to analyse financial performance over any number of years they choose. But, when an input field is cleared, I get the above error message. I didn't experience this problem prior to converting my plots to ggplot2. Below is my code:
Years<-reactive({c(if(input$num>0){"Year 1"},
if(input$numb>0){"Year 2"},
if(input$number>0){"Year 3"},
if(input$numbers>0){"Year 4"},
if(input$numbered>0){"Year 5"})})
profitability.df<-reactive({data.frame(profitability.ratio(),Years())})
output$bar<-renderPlotly({
input$go
isolate(ggplot(profitability.df())+aes(x=Years(),y=profitability.ratio(),fill=Years())+
geom_bar(stat="identity")+scale_fill_manual(values=c("#5dade2","#ec7063","#48c9b0","#a569bd","#f4d03f"))+labs(x="Years",y="Margins (%)",fill="Years"))
profit<-reactive({
as.numeric(c(if(input$num>0){input$num},
if(input$numb>0){input$numb},
if(input$number>0){input$number},
if(input$numbers>0){input$numbers},
if(input$numbered>0){input$numbered}))
})
turnover<-reactive({
as.numeric(c(if(input$fig>0){input$fig},
if(input$figs>0){input$figs},
if(input$figu>0){input$figu},
if(input$figur>0){input$figur},
if(input$figure>0){input$figure}))
})
profitability.ratio<-reactive({profitability(profit(),turnover())})
})
Thanks in anticipation of your suggestions.