Problem in feeding an R script by reactive inputs in R shiny

I am creating a Shiny app, taking 3 inputs (numbers) through sliders and creating some plots as its output. 3 pre-defined R files should do the process on these inputs. the process is as follows:
inputs ---> file-1 ---> file-2 ---> file-3 ---> output
Neglecting the Shiny app, these 3 files work well (they are modeling weather forecasts). They should be run consecutively and plots would be the final output of the 3rd file. File-1 reads some data from other csv and xlsx files as well. The primary values for our 3 variables are also set in this file so that the rest of the process and the output is dependent on these values. In order to be fed by the users in the app, I removed these primary values from the file, to be valued by the user through sliders. In the server part of the app, I made reactivity to read these 3 inputs. Then I tried to run my files through "source()" consecutively. But it is not working, whether I put the 3 "source()" functions within the same reactivity or out of it. The problem is that: "reactivity creates objects of the kind "closure", like R functions, but my file-1 needs simple numbers to start."
Here, I have made a sample code of what I am looking for. Removing the lines of my_a, my_b, and my_c, everything goes well and the app runs. But it is vital for me to use a, b, and c to start my main calculations. If I put my_a, my_b, and my_c in the reactive expression, they work, but again they are "closure" objects that are not fit for the next steps of my calculations. As I understand, I have to either cut the chain of creating closure objects somewhere or include all my model‘s codes and files within reactivity (that I couldn‘t do them).

library(shiny)

 x <- 10

ui <- fluidPage(
  sliderInput("aaa", label= "Select aaa", min = -10, max = 10, value = 0.5),
  sliderInput("bbb", label= "Select bbb", min = -10, max = 10, value = 0),
  sliderInput("ccc", label= "Select ccc", min = -10, max = 10, value = 0.5),
  textOutput("txt")
)

server <- function(input, output, session) {
        a <- reactive({input$aaa})
        b <- reactive({input$bbb})
        c <- reactive({input$ccc})
           
# Here I need to fetch some external data frames and do some simple pre-defined mathematical calculations:
#     my_a <- a + x
#     my_b <- b - x
#     my_c <- c * x
    
 output$txt <- renderText({
   paste0("The final results are: ", a(), "/", b(), "/", c())
#  paste0("The final results are: ", my_a, "/", my_b, "/", my_c)
   })
}

shinyApp(ui, server)

I made some edits to your example and got it to work. I changed my_a/b/c to reactives, and within these objects replaced a/b/c with direct calls to their associated input$aaa/bbb/ccc. Finally, in the text output, my_a was changed to my_a() since it's now a reactive (same for b and c). I hope this helps.

library(shiny)

x <- 10

ui <- fluidPage(
  sliderInput("aaa", label= "Select aaa", min = -10, max = 10, value = 0.5),
  sliderInput("bbb", label= "Select bbb", min = -10, max = 10, value = 0),
  sliderInput("ccc", label= "Select ccc", min = -10, max = 10, value = 0.5),
  textOutput("txt")
)

server <- function(input, output, session) {
  # a <- reactive({input$aaa})
  # b <- reactive({input$bbb})
  # c <- reactive({input$ccc})
  
  # Here I need to fetch some external data frames and do some simple pre-defined mathematical calculations:
      my_a <- reactive({ input$aaa + x })
      my_b <- reactive({ input$bbb - x })
      my_c <- reactive({ input$ccc * x })
  
  output$txt <- renderText({
    # paste0("The final results are: ", a(), "/", b(), "/", c())
     paste0("The final results are: ", my_a(), "/", my_b(), "/", my_c())
  })
}

shinyApp(ui, server)

image

This topic was automatically closed 54 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.