Я пытаюсь сделать свое первое приложение shinyapp. Но я получаю эту ошибку: операция не разрешена без активного реактивного контекста. (Вы пытались сделать что-то, что может быть сделано только внутри реактивного выражения или наблюдателя.) Я подозреваю, что с этим есть проблема, если. Это мой код:
#install.packages("shinythemes")
library(shiny)
library(shinythemes)
library(caret)
#library(tree)
library(rattle)
heart=read.csv("Heart3.csv",header = TRUE)
heart=heart[complete.cases(heart),]
#checking missing values
#sum(is.na(heart))
# removing missing values
heart=heart[complete.cases(heart),]
#create training/test split
set.seed(345)
train.index=createDataPartition(heart[,ncol(heart)],p=0.7,list=FALSE)
train=heart[train.index,]
test=heart[-train.index,]
# Define UI for random distribution app ----
ui <- fluidPage(
# theme of the app
theme = shinytheme("cerulean"), #slate
# App title ----
titlePanel("Classification Alghoritms GC3"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
helpText("Classify heart deaseses based on
decision tree or random forest"),
radioButtons(inputId = "algorithm",
label= "Choose an algorithm",
choices = c("Decision Tree",
"Random Forest"),
selected = "Decision Tree"),
# Input: Select whether you want a decision tree or a random forest ----
#radioButtons("option", "Type:",
#c("Decision Tree" = "dt",
#"Random Forest" = "rf")),
# br() element to introduce extra vertical spacing ----
#br(),
# Input: Slider for the values to generate ----
sliderInput("ff", "Fold:",
value = 10,
min = 1,
max = 50,
step = 1),
sliderInput("rr", "Repeats:",
value = 3,
min = 1,
max = 50,
step=1),
sliderInput("cpr", "cp:",
min = 0.01,
max = 0.1,
value=c(0.01,0.03),
step=0.01)
),
# Main panel for displaying outputs ----
mainPanel(
#plotOutput(outputId = "plot"),
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)# tabsetPanel
)# main panel
)# sidebar panel
)# sidebar layout
# Define server logic for random distribution app ----
server <- function(input, output) {
if(input$algorithm == "Random Forest"){
outuput$plot<- function () renderPlot({
fitcontrol=trainControl(
method ="repeatedcv",
number = input$ff,
repeats = input$rr)
set.seed(324)
cpGrid=expand.grid(cp=input$cpr)
rfFit = train(AHD~. , data=train, method="rf", metric="Accuracy",
trControl=fitControl, tuneGrid=cpGrid)
plot(rFit)
})
}
if(input$algorithm == "Decision Tree"){
output$plot <- function() renderPlot({
# decision tree
fitcontrol=trainControl(method="repeatedcv",
number=input$ff,
repeats = input$rr)
set.seed(1)
cpGrid=expand.grid(cp=input$cpr)
heart.rparts=train(train[,-ncol(heart)],
train[,ncol(heart)],
method = "rpart",
tuneGrid = cpGrid,
trControl = fitcontrol)
#heart.rpart
#plot(heart.rpart$finalModel)
#text(heart.rpart$finalModel, cex=0.8)
fancyRpartPlot(heart.rparts$finalModel) })
}
}
shinyApp(ui = ui, server = server)
Большое спасибо за ваш ответ.