0
votes

I am struggling with my first shiny app and having problems to make it work due to, I think, I am missing out something in the server function.

I have a R script that has two variables (the two reactive values in shiny) that creates a plot (a histogram) and a csv file with two columns (time and mm).

library(ggplot2)
**Pdmm** <- 125                 # numeric input in shiny
**IndiceTorrencial** <- 10      # slider between 8 and 12 in shiny 
DuracionAgua <- 24
IntervaloMin <- 60
IntervaloTiempo <- IntervaloMin/60
Intervalos <- DuracionAgua/IntervaloTiempo
t <- seq(1,Intervalos,IntervaloTiempo)
DF <- data.frame(t)
DF$I <- (**Pdmm**/24)*(**IndiceTorrencial**)^(((28^0.1)-(DF$t^0.1))/((28^0.1)-1)) # equation where the reactive values are running
DF$Pacu <- DF$t*DF$I
DF$Pmm <- c(DF$Pacu[1], diff(DF$Pacu, lag = 1))
DF$mm <- c(DF$Pmm[23],DF$Pmm[21],DF$Pmm[19],DF$Pmm[17], DF$Pmm[15],DF$Pmm[13],DF$Pmm[11],DF$Pmm[9],DF$Pmm[7],DF$Pmm[5],DF$Pmm[3],DF$Pmm[1],DF$Pmm[2],DF$Pmm[4],DF$Pmm[6],DF$Pmm[8],DF$Pmm[10],DF$Pmm[12],DF$Pmm[14],DF$Pmm[16],DF$Pmm[18],DF$Pmm[20],DF$Pmm[22],DF$Pmm[24])
ggplot(DF,aes(x=t,y=mm)) + geom_bar(stat = "identity",fill = "dodgerblue",color = "black") + scale_x_continuous(name = "t(h)", breaks = seq(1,24,1)) + scale_y_continuous(name = "mm")

My shiny app has, in its UI, a slider, a numeric input and a plot. It works, I will have time to make something better.

ui <- basicPage(
  sliderInput(inputId = "coefTo",
              label = h3("Torrentiality Coefficient"),
              value = 10, min = 8, max = 12),
  numericInput(inputId = "PmmS",
               label = h3("Areal Precipitation"),
               value = 100),
  imageOutput("plot")
)

The problem is in the server function. I think I am sure that I have to use a reactive function in order to execute the code and to yield the results (histogram and csv). Also, to plot the histogram, I have a renderPlot.

server <- function(input, output, session){

  data <- reactive({
    DuracionAgua <- 24
    IntervaloMin <- 60
    IntervaloTiempo <- IntervaloMin/60
    Intervalos <- DuracionAgua/IntervaloTiempo
    t <- seq(1,Intervalos,IntervaloTiempo)
    DF <- data.frame(t) 
    DF$I <- (input$PmmS/24)*(input$coefTo)^(((28^0.1)-(DF$t^0.1))/((28^0.1)-1))
    DF$Pacu <- DF$t*DF$I
    DF$Pmm <- c(DF$Pacu[1], diff(DF$Pacu, lag = 1))
    DF$mm <- c(DF$Pmm[23],DF$Pmm[21],DF$Pmm[19],DF$Pmm[17], DF$Pmm[15],DF$Pmm[13],DF$Pmm[11],DF$Pmm[9],DF$Pmm[7],DF$Pmm[5],DF$Pmm[3],DF$Pmm[1],DF$Pmm[2],DF$Pmm[4],DF$Pmm[6],DF$Pmm[8],DF$Pmm[10],DF$Pmm[12],DF$Pmm[14],DF$Pmm[16],DF$Pmm[18],DF$Pmm[20],DF$Pmm[22],DF$Pmm[24])
    DFm <- DF$mm

    return(DFm)

  })

  output$plot <- renderPlot({
    ggplot(data(DFm),aes(x=t,y=mm)) + geom_bar(stat = "identity",fill = "dodgerblue",color = "black") + scale_x_continuous(name = "t(h)", breaks = seq(1,24,1)) + scale_y_continuous(name = "mm")
  })

}

When I run this script, I get the shiny app I have the slider and the numeric input, but not the plot getting an error message saying Error: unused argument (DFm). DFm is not created, so I assume that I am placing bad the code from my original script inside the shiny app, but I cannot figure out how to do it.

I have tried several things to make the server works, but I think that the script never runs inside the shiny app. I tried to create two reactive functions, one per each reactive value. I tried to put all the code from my original script out of the shiny leaving just the reactive values inside the shiny code... I tried with observe function as well. Another problem I have is that I am not sure about the renderPlot. I am aware that I have to call the reactive function, data, but as I am not sure if it is well made. I think that my server function is a total disaster. I have looked for examples in the shiny gallery and in Google, but I do not see something similar to help me.

Could anyone give a tip in order to fix my server function?

Many thanks in advance.

1
Just use data() instead of data(DFm) in your ggplot - amrrs
I did it, in that case I get: Error: ggplot2 doesn't know how to deal with data of class numeric. But the most important is that DFm is not created, the script does not work inside the shiny app. - vacp73
Why are you returning only DFm instead of DF which is actually a dataframe? - amrrs
DONE!!!! as simple as change DFm for DF. I tried with DF, without return(), and it did not work. Later, I tried to create a new dataframe with the two columns I needed and then I used return(). Now, I cancel DFm and make return(DF) and it works.... many thanks.... several days with headache and it was so easy..... - vacp73
Would be good to answer your question and mark it answered. - amrrs

1 Answers

0
votes

Following the advice of our colleague, I could solve my problem by setting DF, the first data frame I created, in return() inside the reactive function. Another confusion was how to set the object from the reactive function inside the renderplot.