Multiple linear regressions in a reactive environment

I'm trying to do multiple linear regressions that are dependent on multiple UI inputs in shiny and struggling to get them to work in a reactive environment.

If I remove the UI input or output as a renderText() then they work but not as is in the example below.

Any help greatly appreciated!!!


library(shiny)


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            
            selectizeInput("Event", "", choices = df1$event, width = "85%"),
            selectizeInput("Name", "", choices = df1$athlete, width = "85%"),
            
            numericInput("Prediction", "Target time (s):", 56, min = 0, max = 150, step = 0.1, width = "85%")

        ),

        mainPanel(
           DTOutput("table", width= "50%")
        )
    )
)


server <- function(input, output) {
    
TTime = c(61,62,63,64,65,66,67,68,69,70,69,68,67,66)
L1_15m = c(6.8,6.76,7.64,7.44,7.4,6.96,6.88,6.42,6.22,6.36,6.1,6.2,6.52,7.54)
L1_5in = c(3.06,2.89,2.86,2.78,3.01,3.04,2.9,3,2.76,3.08,2.74,2.82,2.72,3.12)
L2_15m = c(9.34,9.11,9.78,9.56,9.45,9.26,9.08,9.02,8.98,9.02,8.88,9.02,9.08,9.68)
athlete = c('Athlete_A','Athlete_B','Athlete_C','Athlete_C','Athlete_C','Athlete_D','Athlete_D','Athlete_B','Athlete_E',
            'Athlete_A','Athlete_E','Athlete_B','Athlete_E','Athlete_C')
event = c('event1', 'event1', 'event2', 'event1', 'event2', 'event2', 'event1', 'event2', 
          'event1', 'event1', 'event2', 'event1','event2', 'event2')

df1 <- data.frame(event, athlete, TTime, L1_15m,  L1_5in,  L2_15m)


df1b <- reactive({
    df1 %>% filter(event == input$Event)
})

# target <- reactive({
#     input$Prediction
# })

#target = 52.8

lm_df <- reactive({
    
    target = input$Prediction

start <- lm(L1_15m ~ TTime, data = df1b)
startp <- round((start$coef[1]) + (start$coef[2] * target), 2)

wall5m <- lm(L1_5in ~ TTime, data = df1b)
wall5mp <- round((wall5m$coef[1]) + (wall5m$coef[2] * target), 2)

wall15m <- lm(L2_15m ~ TTime, data = df1b)
wall15mp <- round((wall15m$coef[1]) + (wall15m$coef[2] * target), 2)

totalp = input$Prediction


preds = c(totalp, startp, wall5mp, wall15mp)
time = c('TTime', 'L1_15m', 'L1_5in', 'L2_15m')

preds_time <- data.frame(time, preds)

})


df2 <- reactive({
    df1 %>% filter(swimmer == input$Name) %>%
        arrange(TTime) %>% slice(1:1) %>% 
        gather(TTime, L1_15m, L1_5in, L2_15m, key='time', value='pb') %>%
        select(time, pb) %>% 
    left_join(preds_time, by='time') %>%
        mutate(diff = round(pb - preds, 2))
})



output$table <- renderDT({
    
    datatable(
    
    df2(),
    
    options = list(
        dom = ''),
    rownames = FALSE)
})


}

# Run the application 
shinyApp(ui = ui, server = server)

Hi there,

I see this post hasn't received any responses yet. I am pretty sure you can fix most of your problems here with quasiquoation. Have a look here: 19 Quasiquotation | Advanced R and here: Tidy evaluation is one of the major feature of the latest versions of dplyr and tidyr. - RStudio

See this reprex below of it in action. You should be able to do something similar for your problem.

## Only run examples in interactive R sessions
if (interactive()) {

library(ggplot2)

# single selection
shinyApp(
  ui = fluidPage(
    varSelectInput("variable", "Variable:", mtcars),
    plotOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderPlot({
      ggplot(mtcars, aes(!!input$variable)) + geom_histogram()
    })
  }
)


# multiple selections
## Not run: 
shinyApp(
 ui = fluidPage(
   varSelectInput("variables", "Variable:", mtcars, multiple = TRUE),
   tableOutput("data")
 ),
 server = function(input, output) {
   output$data <- renderTable({
      if (length(input$variables) == 0) return(mtcars)
      mtcars %>% dplyr::select(!!!input$variables)
   }, rownames = TRUE)
 }
)
## End(Not run)

}

I'd be willing to help you if you create a cleaner/simpler reprex (FAQ: How to do a minimal reproducible example ( reprex ) for beginners)

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.