我想创建一个Shiny应用程序,以显示在插入符号中训练的模型的输出。然后,当运行第二个模型时,它将保留先前的结果并显示最新的结果。我尝试用帖子Previous input in Shiny中的答案对循环进行编码,并使用帖子R Shiny: keep old output中的答案对输出进行编码
但是,我无法找到一种方法来将插入符号输出显示为摘要或表,以使其更易于阅读。
这是我到目前为止的代码。问题出在 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)
当我运行此应用程序时,它将产生一些输出,而当运行两次时,将在下面添加输出。但是,此输出不是用户友好的样式。我想以摘要或表格格式显示它。