r - 如何让我的 train() 函数在闪亮状态下运行以评估模型性能?
问题描述
我正在做一个项目来创建一个闪亮的应用程序,允许用户进行一些建模、选择一个模型并进行一些预测。
当我在我的数据上运行 r 控制台中的 train() 函数时,它工作正常。当我在闪亮的应用程序中运行它时,我一直收到一个错误:对比只能应用于具有 2 个或更多级别的因子。我的数据框没有任何因素。无法弄清楚为什么它在控制台中有效但不闪亮。任何帮助,将不胜感激!
这是我的代码:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Variables Impacting Hate Crime Rates"),
# define main panel layout
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Information",
textOutput("introduction")),
tabPanel("Data Exploration",
textOutput("data"),
selectInput("x","Explanatory variable:",
choices = colnames(crimes),
selected = "median_household_income"),
selectInput("y","Response variable:",
selected = "median_household_income",
choices = colnames(crimes)),
plotOutput("scatterplot"),
selectInput("y2", "Variable to compare:",
selected = "median_household_income",
choices = colnames(crimes)),
plotOutput("boxplot"),
selectInput("var","Summary variable:",
choices = colnames(crimes),
selected = "median_household_income"),
verbatimTextOutput("summary"))
,
tabPanel("Unsupervised Learning",
selectInput("k","Number of clusters:",
choices = c(1,2,3),
selected = 1),
plotOutput("cluster"),
selectInput("dmethod","Distance method:",
choices = c("euclidian","binary","minkowski",
"canberra","manhattan","maximum"),
selected = "euclidian"),
selectInput("cmethod", "Cluster method:",
choices = c("single","complete"),
selected = "single"),
plotOutput("tree")),
tabPanel("Modeling",
selectInput("xvar", "x variable:",
choices = colnames(crimes),
selected = "gini_index"),
selectInput("yvar", "y variable:",
choices = colnames(crimes),
selected = "median_household_income"),
verbatimTextOutput("model")),
tabPanel("Data",
DT::dataTableOutput("mytable1"))
)
)
))
source("C:/Users/W447075/Documents/ST558/Comora_final/helpers.R")
shinyServer(function(input, output,session){
output$introduction <- renderText({
"This is my introduction"
})
output$data <- renderText({
"This page allows you to create some basic graphical
and numeric summaries including a scatter plot, boxplot,
and statistical summary for any of the variables in the
'crimes' dataset."
})
selectedData <- reactive({
crimes %>% filter(region ==input$region)
})
selectData2 <- reactive({
crimes[ , c("region",input$y)]
})
selectData3 <- reactive({
crimes[ , c("region", input$y2)]
})
mydata <- reactive({
crimes[ ,input$var]
})
mycluster <- reactive({
kmeans(df_scale, centers = input$k, nstart = 25)
})
d <- reactive({
dist(df, method = input$dmethod)
})
clusterplot <- reactive({
plot(hclust(d(), method = input$cmethod))
})
model <- reactive({
train(input$yvar ~ input$xvar, crimes,
method = "lm",
trControl = trainControl(
method = "cv", number = 10,
verboseIter = TRUE))
})
#render a barplot
output$scatterplot <- renderPlot({
ggplot(crimes, aes_string(x = input$x,
y = input$y)) +
geom_point(size = 3) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank())
})
output$boxplot <- renderPlot({
ggplot(selectData3(), aes(x = region, y = selectData3()[ ,input$y2])) +
geom_boxplot(aes(fill = region)) + ylab(input$y2)+
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank())
})
output$summary <- renderPrint({
dataset <- na.omit(mydata())
summary(dataset)
})
output$cluster <- renderPlot({
fviz_cluster(mycluster(),data = df)
})
output$tree <- renderPlot({
clusterplot()
})
output$mytable1 <- DT::renderDataTable({
DT::datatable(df)
})
output$model <- renderText({
model()
})
})
解决方案
推荐阅读
- css - 使用 CSS 网格自动均匀分布列
- python - Pythonanywhere。ImportError:没有名为“crispy_forms”的模块
- java - 遵循 BDD 时单元测试与端到端(集成本身)之间的关系
- c# - System.ComponentModel.Win32Exception: '系统找不到指定的文件'
- uwp - UWP 控制台应用程序能否在 Windows 10 IoT 上运行
- com+ - Win 7/10 64 bit -- 注册 VBR/TLB 文件
- sql-server - SQL Server CDC:__$seqval 如何在幕后工作?
- java - 在 jpa Criteria 中热添加另一个对象的参数列表
- python - 列表子类的 Python 类型
- c - 我一直在这里遇到分段错误。在这里待了4个小时