如何在Shiny中对radioButtons的数据进行不同的处理?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Shiny中对radioButtons的数据进行不同的处理?相关的知识,希望对你有一定的参考价值。
我想用Shiny创建一个应用程序,我想对每个单选按钮的数据进行不同的处理,为每个单选按钮选项创建不同的图。
我的代码是这样的。
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)
然而,对于output$plot,我想为每个单选按钮的可能性做不同的图。我试过这样做,但没有成功。
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")
}
})
答案
你需要使用 "原始 "的名称,是在 radioButtons
在 if
条件,而不是你给选择的新名称。
output$plot <- renderPlot({
if(input$algorithm == "rf"){
model<-train(X~., datos_train, method="rf", trControl= trainControl(method='cv', number=5))
plot(model$finalModel, main="Random Forest")
}
if(input$algorithm == "mlp"){
model<-train(X~., datos_train, method="mlp", trControl= trainControl(method='cv', number=5))
plotnet(model, main="Artificial Neural Network")
}
if(input$algorithm == "svmRadial"){
model<-train(X~., datos_train, method="svmRadial", trControl= trainControl(method='cv', number=5))
plot(model, main="Support Vector Machine")
}
})
以上是关于如何在Shiny中对radioButtons的数据进行不同的处理?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 R Shiny 中对使用反应数据框呈现的数据表执行计算?
从 Shiny 中的 sideBarMenu 和 radioButton 选择时不显示图表