Reuse values generated in serverpart shiny app (error variable not found)

,

Hello,
I want to reuse predicted values in calculation
eg my code

type predictionRadio <-reactive({
    y_predTV<- req(y_predTV)
    x <- input$user_input
    x<-as.data.frame(x)
    names(x)<-"Total_invest"
    # Example: Simple linear model y = 2x + 3
    y_predRadio <- round((x-y_predTV)/0.16*100,digits = 2)

    return(y_predRadio)
  })
  #TV
  predictionTV <- reactive({
    x <- input$user_input
    x<-as.data.frame(x)
    names(x)<-"Total_invest"
    # Example: Simple linear model y = 2x + 3
    y_predTV <- round(predict(model2,x),digits = 2)
    if (x$Total_invest==0) y_pred<- 0
    return(y_predTV)
  })

so in first part y_predTV is not found to calculate that formula.
How to solve such problems?
Kind regards
Nobel

1 Like

The variable y_predTV has local scope inside the reactive expression for predictionTV. It is not visible outside that expression. Does changing the second line to y_predTV <- req(y_predTV()) work for you?

Also, the next to last line of the second reactive expression sets a value for local variable y_pred, which is never used. Should that be y_predTV rather than y_pred?

1 Like

Thx prubin,
but changing the second line. does not help.

1 Like

Sorry, my bad. I should have said to replace req(y_predTV) with req(predictionTV()).

1 Like

The solution I found myself and is working, is placing all in one reactive part and output results as a list.
So thank you very much for trying helping me guys.
Kind regards

Nobel

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.