I am trying to create an app with Shiny and I would like to treat differently the data from each radio button to create different plots for each of the radioButton options.
The code that I have and works is this one:
library(shiny)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(caret)
library(wakefield)
#Create dummy dataset named datos
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)
#Data partition
set.seed(12345)
inTrain <- createDataPartition(y=datos$X, p=0.66666666666666667, list=FALSE)
datos_train<-datos[inTrain,]
datos_test<-datos[-inTrain,]
class_train<-datos[inTrain,1]
class_test<-datos[-inTrain,1]
#Define ui
ui <- fluidPage(
titlePanel("This is a title"),
sidebarLayout(
sidebarPanel(
radioButtons("algorithm",
"Select one algorithm to visualize its output",
choices= c("Random Forest" = "rf",
"Artificial Neural Network" = "mlp",
"Support Vector Machine" = "svmRadial") )
),
mainPanel(
tabsetPanel(type="tabs",
tabPanel("Prediction", verbatimTextOutput("confucio")),
tabPanel("Plot", plotOutput("plot")),
tabPanel("Confussion Matrix", verbatimTextOutput("matrix"))
)
#From select variable model
#tableOutput("table"),
#plotOutput("myPlot")
)
)
)
server <- function(input, output, session) {
output$confucio<-renderPrint(train(X~., datos_train,
method=input$algorithm,
trControl= trainControl(method='cv', number=5)))
output$plot <- renderPlot({
plot(train(X~., datos_train, method=input$algorithm, trControl= trainControl(method='cv', number=5)))
})
output$matrix<-renderPrint({ confusionMatrix(
predict(
train(X~., datos_train, method=input$algorithm, trControl= trainControl(method='cv', number=5)), datos_test)
, class_test)
})
}
# Run the application
shinyApp(ui, server)
However, for output$plot I would like to do different plots for each of the radiobutton possibilities. I tried this but it does not work.
output$plot <- renderPlot({
if(input$algorithm == "Random Forest"){
model<-train(X~., datos_train, method="rf", trControl= trainControl(method='cv', number=5))
plot(model$finalModel, main="Random Forest")
}
if(input$algorithm == "Artificial Neural Network"){
model<-train(X~., datos_train, method="mlp", trControl= trainControl(method='cv', number=5))
plotnet(model, main="Artificial Neural Network")
}
if(input$algorithm == "Support Vector Machine"){
model<-train(X~., datos_train, method="svmRadial", trControl= trainControl(method='cv', number=5))
plot(model, main="Support Vector Machine")
}
})
Aucun commentaire:
Enregistrer un commentaire