Bypass "Reading objects from shinyoutput object not allowed"

Hello,

My shiny app produces two integers output$number1 and output$number2.
I want to display output$number1/output$number2 but I keep getting the error "Reading objects from shinyoutput object not allowed".
Googling the error tells me that I should use a reactive() function but I have no idea how.

We don't really have enough info to help you out, could you make a minimal REPRoducible EXample (reprex) about your issue?
Here is a very useful guide about how to make a reprex for a shiny app

Here is what I want to do:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

ui <- fluidPage(numericInput("x", label = "number", value = 150), textOutput("n1"), textOutput("n2"), textOutput("n3"))


f = function(x){
  
  return(x^2)
}

g = function(x){
  
  return(x^3)
}

server <- function(input, output) {
  output$n1 =  renderText({
    
    print(system.time(f(input$x)))
    
    
  })
  
  output$n2 =  renderText({
    
    print(system.time(g(input$x)))
    
    
  })
  
  output$n2 =  renderText({
    
    print(strtoi(output$n1) - strtoi(output$n2))
    
    
  })
  
}

shinyApp(ui, server)

In a typical Shiny app, the outputs are merely artifacts to display some result in the UI. I would recommend utilizing reactive elements that you can place in one or more outputs. You will find it much easier to add new features and iterate on the UI of your app. Here is a modified version that accomplishes what you're looking for:

library(shiny)

ui <- fluidPage(
  numericInput("x", label = "number", value = 150), 
  textOutput("n1out"), 
  textOutput("n2out"), 
  textOutput("n3out")
)


f = function(x){
  return(x^2)
}

g = function(x){
  return(x^3)
}

server <- function(input, output) {
  
  n1 <- reactive({
    f(input$x)
  })
  
  n2 <- reactive({
    g(input$x)
  })
  
  n3 <- reactive({
    n1() - n2()
  })
  
  output$n1out =  renderText({
    print(n1())
  })
  
  output$n2out =  renderText({
    print(n2())
  })
  
  output$n3out =  renderText({
    print(n3())
  })
}

shinyApp(ui, server)

I am not sure what you were trying to do with those system.time calls Once you make the paradigm shift to do your app's processing in reactives, it will set you up for success in making your app's outputs much more streamlined.

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.