I would like to have a dynamic regression qqplot in Shiny that has a drop-down menu for x value(predictor variable). Youn can find my code below where I have defined an LM function that takes the response variable form first place and predictor variable from the second place when I run the model afterward. The problem is that I have to write exactly the variable names when I run the model (see below in the code), but I would like to have selectInput function which will deliver the variable name to the model. Link to my ShinyApp in order to have a more clear picture (last dashboard): https://schnappi.shinyapps.io/coronavirus/
The code is below:
Regression model function:
ggplot(fit$model, aes_string(
x = names(fit$model)[2],
y = names(fit$model)[1],
label = names(fit$model)[3]
)) +
geom_point() + labs(x = "Population density (km2)", y = "Cases per 1M") +
stat_smooth(method = "lm") +
labs(title = paste(
"R2 =",
signif(summary(fit)$r.squared, 2),
" Slope =",
signif(fit$coef[[2]], 2),
" P =",
signif(summary(fit)$coef[2, 4], 2)
))
}```
UI code:
tabItem(
tabName = "Dens",
titlePanel(h1(
"Population density and Covid-19 cases", align = "center"
)),
absolutePanel(
top = 130,
left = 250,
draggable = FALSE,
numericInput(
"num",
"x-axis cut off:",
max(join$Pop_density),
min = 1,
max = max(join$Pop_density)
)
),
absolutePanel(
top = 130,
left = 550,
draggable = FALSE,
numericInput(
"num2",
"y-axis cut off:",
max(join$TotCases_1M),
min = 1,
max = max(join$TotCases_1M)
)
),
absolutePanel(
top = 130,
left = 850,
draggable = FALSE,
selectInput("con", "Select continent:", choices =
join$Continent)
),
absolutePanel(
top = 130,
left = 1050,
draggable = FALSE,
selectInput("paraM", "Select parameter:", choices = colnames(join)) ## This is the part of the code that defines the predictor variable
),
absolutePanel(
top = 190,
left = 220,
width = 1700,
height = 500,
draggable = FALSE,
mainPanel(plotlyOutput("plotDens"))
)
)
This is part which RUN the regression model:
output$plotDens <- renderPlotly({
ggplotRegression(lm(
TotCases_1M ~ input$paraM, ## Here the LM model should recognize "input$paraM" as the column name but this does not work.
data = subset(join, Continent == input$con | input$con == "") %>%
filter(input$paraM > input$num &
TotCases_1M < input$num2)
))
})
THANK YOU.