Reputation: 2412
Very basic R shiny issue Im sure. I want to run a linear regression on a toy dataset, use that to predict corresponding values based on used input, and plot a line to the regression line from the given and predicted values.
When I run this, I get the error: Listening on http://XXX Warning: 'newdata' had 1 row but variables found have 6 rows Error in segments(newData, 0, newData, newData.pred, col = "red") : invalid first argument
SO I realise it's how I'm handling the user input. I jsut want to take the numeric value they give, feed it to predict.lm and draw the resultant line. Id like text too if possible to give the predict.lm output.
ui.R
library(shiny)
(pageWithSidebar(
headerPanel('Predicty'),
sidebarPanel
(
numericInput('clusters', 'Absorbance', 0.1, min = 0, max = 100, step = 0.00001)
),
mainPanel
(
plotOutput('plot1')
)
))
server.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)
})
})
Upvotes: 0
Views: 556
Reputation: 21425
You don't need the reactive
in your renderPlot
, the function is already reactive
.
predict.lm
did not find any column names abs
in your input which throws the first warning.
segments
takes coordinates as arguments, you inputed a data.frame
which explains the second error.
You could do:
output$plot1 <- renderPlot({
newData <- data.frame(abs=input$clusters)
newData.pred <- predict.lm(abss, newData)
abline(abss)
segments(newData$abs , 0, newData$abs, newData.pred, col = "red")
})
Also, you might want to change the xlim
of your plot or the start value for your input because 0.1
is not on the plot
Upvotes: 1