ui <- fluidPage( sidebarPanel( checkboxGroupInput("predictors_1", "Select Predictor variables for Model 1", names(df[,3:8])), # some data frame
actionButton("generate_1", "Generate", class = "btn-success"),
actionButton("reset_1", "Reset", class = "btn-danger"),
width = 3,
position="left"
)
mainPanel(
fluidRow(
column(width = 6,
verbatimTextOutput("model_1_summary"),
),
column(width = 6,
verbatimTextOutput("model_2_summary")
)
)
)
server <- function(input, output, session) {
rv1 <- reactiveValues()
# Functionality for Generate button
rv1$model_1_formula <- eventReactive(input$generate_1, {
reformulate( termlabels = input$predictors_1,
response = "Precipitation")})
# Printing Model 1 Summary
output$model_1_summary <- renderPrint(tryCatch(summary(lm(formula = rv1$model_1_formula(),
data = training_set)),
error = function(e) print("Select at least one Predictor and click Generate!")))
# Functionality for Reset Button
observeEvent(input$reset_1, {
updateTextInput(session, "predictors_1", value = "")
rv1$model_1_formula <- eventReactive(input$generate_1, {
reformulate( termlabels = input$predictors_1,
response = "Precipitation")})
})
}
My code is working in the following manner:
So the code is working but its not complete yet. Any help will be immensely appreciated.
This code should have the functionality you want.
I fixed a few things, and removed the tryCatch()
statement, since it's better to explicitly prevent errors than catch them. I also created some very basic example data to test the program.
library(shiny)
df <- as.data.frame(as.list(1:8))
names(df) <- letters[1:8]
df$Precipitation <- 9
training_set <- df
ui <- fluidPage(
sidebarPanel(
checkboxGroupInput("predictors_1", "Select Predictor variables for Model 1", names(df[,3:8])), # some data frame
actionButton("generate_1", "Generate", class = "btn-success"),
actionButton("reset_1", "Reset", class = "btn-danger"),
width = 3,
position="left"
),
mainPanel(
fluidRow(
column(
width = 6,
verbatimTextOutput("model_1_summary"),
),
column(
width = 6,
verbatimTextOutput("model_2_summary")
)
)
)
)
server <- function(input, output, session) {
rv1 <- reactiveValues()
# Functionality for Generate button
observeEvent(input$generate_1, {
if (length(input$predictors_1) > 0) {
rv1$model_1_formula <- reformulate(
termlabels = input$predictors_1,
response = "Precipitation"
)
} else {
# Signal to output$model_1_summary that no predictors were selected.
rv1$model_1_formula <- NULL
}
})
# Printing Model 1 Summary
output$model_1_summary <- renderPrint({
if (is.null(rv1$model_1_formula)) {
cat("Select at least one Predictor and click Generate!")
} else {
summary(lm(formula = rv1$model_1_formula,
data = training_set))
}
})
# Functionality for Reset Button
observeEvent(input$reset_1, {
updateTextInput(session, "predictors_1", value = "")
# Revert the model_1_summary back to the original message
rv1$model_1_formula <- NULL
})
}
shinyApp(ui, server)