1

I would like to create a Shiny app that shows the output from a model trained in caret. Then, when a second model is run it would keep the previous result in display and it would show the most recent result. I tried to code the loop with the answers in the post Previous input in Shiny and to code the output with the answers in the post R Shiny: keep old output

However, I cannot figure out a way to display caret output as a summary or maybe a table that would make it easier to read.

This is the code I have so far. The problem is in the part output$model_record

library(shiny)
library(dplyr)
library(caret)

X <- r_sample_factor(c("low", "high"), n=232)
MAMAMA<-r_sample_factor(c("C/C", "C/G", "G/G"), n=232)
MEMEME<-r_sample_factor(c("C/C", "C/T", "T/T"), n=232)
MIMIMI<-r_sample_factor(c("A/A", "A/T", "T/T"), n=232)

datos<-data.frame(X,MAMAMA,MEMEME,MIMIMI)

set.seed(12345)
#Define the size of training dataset
inTrain <- createDataPartition(y=datos$X, p=0.66666666666666667, list=FALSE)
#Create training and test data
datos_train<-datos[inTrain,]
datos_test<-datos[-inTrain,]
#Label
class_train<-datos[inTrain,1]
class_test<-datos[-inTrain,1]


ui<-    fluidPage(

                sidebarLayout(

                    sidebarPanel(

                        h3("Design your algorithm"),
                        radioButtons("algorithm",
                                     "Select one algorithm to visualize its output",
                                     choices= c("Random Forest" = "rf",
                                                "Artificial Neural Network" = "mlp",
                                                "Support Vector Machine" = "svmRadial") ),

                        radioButtons("checkGroup", 
                                     "Select a resampling method", 
                                     choices = c(#"Nothing" = "none",
                                                 "Cross validation" = "cv", 
                                                 "Bootstrap" = "boot"
                                     )
                        ),

                        numericInput("num", 
                                     "Select a number of resampling events", 
                                     value = 5),

                        checkboxGroupInput("preproc", 
                                           "Select one or multiple preprocessing methods", 
                                           choices = c("Center" = "center", 
                                                       "Scale" = "scale", 
                                                       "Box Cox" = "BoxCox")
                        ),


                        actionButton(inputId = "fit_model",
                                     label = "Fit Model"),
                        numericInput(inputId = "model_to_show",
                                     label = "Show N most recent models",
                                     value = 2)
                    ),
                    mainPanel(
                        htmlOutput("model_record")
                    )
                )
            )


    server<-function(input, output, session){

            Model <- reactiveValues(
                Record = NULL
            )

            observeEvent(
                input[["fit_model"]],
                {
                    fit <- 
                        train(X~., 
                              data= datos_train, 
                              method=input$algorithm,
                              trControl= trainControl(method=input$checkGroup, number=input$num, classProbs=TRUE, savePredictions = TRUE),
                              preProc = c(input$preproc)
                              )
                    Model$Record <- c(Model$Record, summary(fit))
                }
            )

            output$model_record <- 
                renderPrint({
                    paste(tail(Model$Record, input[["model_to_show"]]), collapse = "<br/><br/>")

                })

        }


# Run the application 
shinyApp(ui, server)

When I run this application it produces some output, and when run twice the output is added below. However, this output is not in a user-friendly style. I wanted to display it in a summary or maybe a table format.

Application output

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
GFA
  • 25
  • 4

0 Answers0