need finite 'xlim' values using reactive function in Shiny

一世执手 提交于 2019-12-23 02:34:37

问题


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')

        )
    )
))

回答1:


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")
    }
})


来源:https://stackoverflow.com/questions/30263432/need-finite-xlim-values-using-reactive-function-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!