【发布时间】:2015-04-28 19:44:10
【问题描述】:
我确定是非常基本的 R 闪亮问题。我想在玩具数据集上运行线性回归,使用它来根据使用的输入预测相应的值,并从给定值和预测值绘制一条线到回归线。
当我运行它时,我得到了错误: 收听http://XXX 警告:“newdata”有 1 行,但找到的变量有 6 行 段错误(newData, 0, newData, newData.pred, col = "red") : 第一个参数无效
所以我意识到这是我处理用户输入的方式。我只是想获取他们给出的数值,将其提供给 predict.lm 并绘制结果线。如果可能的话,我也喜欢文本以提供 predict.lm 输出。
ui.R
library(shiny)
(pageWithSidebar(
headerPanel('Predicty'),
sidebarPanel
(
numericInput('clusters', 'Absorbance', 0.1, min = 0, max = 100, step = 0.00001)
),
mainPanel
(
plotOutput('plot1')
)
))
服务器.R
library(shiny)
## Initial data and linear regression.
conc <- c(.25, .5, 1, 1.5, 2, 2.5)
abs <- c(.332, .631, .972, 1.201, 1.584, 1.912)
results <- data.frame(abs,conc)
abss <- lm(conc~abs, data = results)
shinyServer(function(input, output) {
output$plot1 <- renderPlot({
newData.in <- reactive({
inFile <- input$clusters
inFile
})
newData <- data.frame(newData.in())
newData.pred <- predict.lm(abss, newData)
plot(abs,conc)
abline(abss)
segments(newData , 0, newData, newData.pred, col = "red")
# segments(0, newData.pred[i], clusters()$centers, newData.pred[i], col = i)
})
})
【问题讨论】: