1

I want to calculate some values and return the values to my shiny app:

ui <- fluidPage(
sidebarLayout(
sidebarPanel(numericInput(inputId = "ME",
               label = "Maternal effect:",
               min = -1,
               max = 1,
               value = 0.5),
  numericInput(inputId = "CE",
               label = "Child effect:",
               min = -1,
               max = 1,
               value = 0.5)
),
mainPanel(h3(textOutput("Power"))
)
)
)


server <- function(input, output) {
bzc <- sqrt(abs(input$CE)) * sign(input$CE)     
bzm <- sqrt(abs(input$ME)) * sign(input$ME) 
results <- bzc * bzm
  output$Power <- renderPrint({results  
})
}

shinyApp(ui = ui, server = server) 

This doesnt apprear to work. Any tips on how to calculate in the shiny app?

4
  • What is not working? What is the expected outcome? Any error messages? Commented Jun 6, 2018 at 11:48
  • The input sidebar appreares in the web browser, but no output. Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) Commented Jun 6, 2018 at 11:53
  • 57: stop 56: .getReactiveEnvironment()$currentContext 55: .subset2(x, "impl")$get 54: $.reactivevalues 52: server [C:/Users/ghmoen/Documents/SEM/trial_shiny.R#47] Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) Commented Jun 6, 2018 at 11:53
  • 1
    Please add this information to the question by editing it. Commented Jun 6, 2018 at 11:56

1 Answer 1

1

The error-messages arise, because you have input-objects outside of the render-functions. If you want to calculate something, which you want to reuse in multiple plots, then use a reactive or observe-function.

For all other cases it is enough add the code for bzc, bzm and result inside the render-functions:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput(inputId = "ME",
                              label = "Maternal effect:",
                              min = -1,
                              max = 1,
                              value = 0.5),
                 numericInput(inputId = "CE",
                              label = "Child effect:",
                              min = -1,
                              max = 1,
                              value = 0.5)
    ),
    mainPanel(h3(textOutput("Power"))
    )
  )
)


server <- function(input, output) {

  output$Power <- renderPrint({
    bzc <- sqrt(abs(input$CE)) * sign(input$CE)     
    bzm <- sqrt(abs(input$ME)) * sign(input$ME) 
    results <- bzc * bzm

    results  
  })
}

shinyApp(ui = ui, server = server) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! How would I go about if I'm doing a lot of calculations and want to get three different answers out in for instance a table?
depends whether you want to store your calculations or whether you want to want to just have three calculations. In the latter case you would just copy paste the inputs and add them to your calculations. If you want to store all your calculations, I suggest opening a new question. Because that is more complicated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.