Gunn-Helen Moen
Gunn-Helen Moen

Reputation: 125

Calculate in Shinyapps

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?

Upvotes: 1

Views: 1634

Answers (1)

5th
5th

Reputation: 2395

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) 

Upvotes: 1

Related Questions