I'm trying to build a Linear regression Shiny app with a custom file input. I have a problem with the reactive function in Server.R. The reactive function data returns a data frame called qvdata. When data() is called in renderPlot and I plot from the qvdata I get the following error: "Error in plot.window(...):need finite 'xlim' values". When I do not use the reactive function and do the calculations in renderPlot the error does not occur. Why is this? I want later to be able to call this data reactive function in other render functions to prevent repetition in the code.
Server.R
library(shiny)
shinyServer(function(input, output) {
data<-reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
#takes in data (In my case .dat file)
qvdata=read.table(inFile$datapath)
names(qvdata)=c("H","Q")
qvdata$Hlog=log(qvdata[,1])
qvdata$Qlog=log(qvdata[,2])
xbar=mean(qvdata$Hlog)
ybar=mean(qvdata$Qlog)
xdif=qvdata$Hlog-xbar
ydif=qvdata$Qlog-ybar
Sxy=sum(xdif*ydif)
Sxx=sum(xdif*xdif)
#point estimates of alfa and beta
qvdata$b=Sxy/Sxx
qvdata$a=ybar-qvdata$b*xbar
qvdata
})
output$contents <- renderPlot({
qvdata=data()
t=seq(0,2,0.01)
abline=qvdata$a+qvdata$b*t
plot(qvdata$Hlog,qvdata$Qlog,type="p",pch=20,col="red")
lines(t,abline,col="black")
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel('Linear regression'),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose file')
),
mainPanel(
plotOutput('contents')
)
)
))
Your data
function runs the first time when your shiny app starts. Because the input file is missing it will return NULL
. But Your plotting function doesn't test its input and is equivalent to plot(NULL)
in this case. If you test for an input !is.null()
it should work:
output$contents <- renderPlot({
qvdata=data()
if (!is.null(qvdata)) {
t=seq(0,2,0.01)
abline=qvdata$a+qvdata$b*t
plot(qvdata$Hlog,qvdata$Qlog,type="p",pch=20,col="red")
lines(t,abline,col="black")
}
})