R Shiny:serverFuncSource()中的错误:server.R返回了一个意外类型的对象:list

时间:2016-12-15 03:00:21

标签: r shiny

我正在尝试构建我的第一个闪亮的应用程序,并从可用的模板中大量借用。不幸的是,当我运行应用程序时,它在崩溃之前会短暂闪烁。相关的错误消息是:

  

serverFuncSource()中的错误:server.R返回了一个对象   意外类型:列表

导致此错误的原因是什么?如何解决此问题(在线搜索没有运气)。错误消息令人沮丧地模糊不清。

require(shiny)

ui = pageWithSidebar(
  headerPanel("NFL"),
    sidebarPanel(
      sliderInput("Margin", "Current margin", min=-50, max=50, value=0, step=1),
      textInput("Spread", "Spread", value=0, width="30%"),
      radioButtons("Quarter", "Current period", choices=c("1st", "2nd", "3rd", "4th", "OT"), 
                         selected = "1st", inline = TRUE,width = NULL),
      textInput("TimeRemaining", "Time remaining (mm:ss)", value="15:00",  width="30%"),
      radioButtons("Down", "Down", choices=c("1st", "2nd", "3rd", "4th", "N/A"), inline = TRUE),
      textInput("YTG", "Yards to go", value=10,  width="30%"),
      textInput("YFOG", "Yards from own goal", value=50,  width="30%"),
      checkboxGroupInput("Timeouts_Off", "Timeouts: Offense", choices=c("1", "2", "3")),
      checkboxGroupInput("Timeouts_Def", "Timeouts: Defence", choices=c("1", "2", "3"))
      ),
    mainPanel(
      tableOutput('table')
    )
  )

server=function(input, output){
  x=matrix(0, nrow=1, ncol=11)
  colnames(x)=c("mar", "timeRemaining", "dwn.1", "dwn.2", "dwn.3", "dwn.4", "ytg","yfog", "closingLine", "timo", "timd")

  qtr=switch(input$Quarter, "1st"=1, "2nd"=2,"3rd"=3,"4th"=4, "OT"=5)
  mins=substr(input$TimeRemaining,1,2)
  secs=substr(input$TimeRemaining,4,5)
  timeLeft=100-10/6*((4-qtr)*15+(mins+secs/60))

  x[1,1]=input$Margin
  x[1,2]=timeLeft
  x[1,3]=switch(input$Down, "1st"=1, "2nd"=0,"3rd"=0,"4th"=0)
  x[1,4]=switch(input$Down, "1st"=0, "2nd"=1,"3rd"=0,"4th"=0)
  x[1,5]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=1,"4th"=0)
  x[1,6]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=0,"4th"=1)
  x[1,7]=input$YTG
  x[1,8]=input$YFOG
  x[1,9]=input$spread
  x[1,10]=input$Timeouts_Off
  x[1,11]=input$Timeouts_Def

  #x=data.frame(x)
  xsq=select(x,mar:timd, -dwn.1, -dwn.2, -dwn.3, -dwn.4)^2
  colnames(xsq)=paste(colnames(xsq), "sq",  sep = "_")
  xln=log(select(x,timeRemaining:timd, -closingLine, -dwn.1, -dwn.2, -dwn.3, -dwn.4))
  colnames(xln)=paste(colnames(xln), "ln",  sep = "_")
  x=cbind(x, xsq, xln)
  rm(xln, xsq)
  x
  output$table <- renderTable(x)
}

shinyApp(ui=ui, server=server)

2 个答案:

答案 0 :(得分:0)

我注意到的一些事情:

1)您在代码x = data.matrix(0, nrow=1, ncol=11)中使用的行是错误来源。创建矩阵使用x = matrix(0, nrow=1, ncol=11)

2)不允许在没有包装reactive()函数的情况下使用闪亮的代码,这是其他错误的原因。要获得更多?reactive?eventReactive

修复这两个问题后,代码不会出现任何错误。但是,您需要检查代码的正常运行。找到下面的工作代码。

require(shiny)

ui = pageWithSidebar(
  headerPanel("NFL"),
  sidebarPanel(
    sliderInput("Margin", "Current margin", min=-50, max=50, value=0, step=1),
    textInput("Spread", "Spread", value=0, width="30%"),
    radioButtons("Quarter", "Current period", choices=c("1st", "2nd", "3rd", "4th", "OT"), 
                 selected = "1st", inline = TRUE,width = NULL),
    textInput("TimeRemaining", "Time remaining (mm:ss)", value="15:00",  width="30%"),
    radioButtons("Down", "Down", choices=c("1st", "2nd", "3rd", "4th", "N/A"), inline = TRUE),
    textInput("YTG", "Yards to go", value=10,  width="30%"),
    textInput("YFOG", "Yards from own goal", value=50,  width="30%"),
    checkboxGroupInput("Timeouts_Off", "Timeouts: Offense", choices=c("1", "2", "3")),
    checkboxGroupInput("Timeouts_Def", "Timeouts: Defence", choices=c("1", "2", "3"))
  ),
  mainPanel(
    dataTableOutput('table')
  )
)

server=function(input, output){
  out <- reactive({
  x=matrix(0, nrow=1, ncol=11)
  colnames(x)=c("mar", "timeRemaining", "dwn.1", "dwn.2", "dwn.3", "dwn.4", "ytg","yfog", "closingLine", "timo", "timd")

  qtr=switch(input$Quarter, "1st"=1, "2nd"=2,"3rd"=3,"4th"=4, "OT"=5)
  mins=substr(input$TimeRemaining,1,2)
  secs=substr(input$TimeRemaining,4,5)
  timeLeft=100-10/6*((4-qtr)*15+(mins+secs/60))

  x[1,1]=input$Margin
  x[1,2]=timeLeft
  x[1,3]=switch(input$Down, "1st"=1, "2nd"=0,"3rd"=0,"4th"=0)
  x[1,4]=switch(input$Down, "1st"=0, "2nd"=1,"3rd"=0,"4th"=0)
  x[1,5]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=1,"4th"=0)
  x[1,6]=switch(input$Down, "1st"=0, "2nd"=0,"3rd"=0,"4th"=1)
  x[1,7]=input$YTG
  x[1,8]=input$YFOG
  x[1,9]=input$spread
  x[1,10]=input$Timeouts_Off
  x[1,11]=input$Timeouts_Def

  #x=data.frame(x)
  xsq=select(x,mar:timd, -dwn.1, -dwn.2, -dwn.3, -dwn.4)^2
  colnames(xsq)=paste(colnames(xsq), "sq",  sep = "_")
  xln=log(select(x,timeRemaining:timd, -closingLine, -dwn.1, -dwn.2, -dwn.3, -dwn.4))
  colnames(xln)=paste(colnames(xln), "ln",  sep = "_")
  x=cbind(x, xsq, xln)
  rm(xln, xsq)
  x
  })
  output$table <- renderDataTable(out())
}

shinyApp(ui=ui, server=server)

答案 1 :(得分:0)

通过将一个ui.R代码意外复制到server.R中,使用一个闪亮的应用程序(包含3个文件,而不是一个文件,不是一个文件)来解决此错误。