【问题标题】:Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frameas.data.frame.default 中的错误:无法将类“c(“reactiveExpr”,“reactive”)”强制转换为 data.frame
【发布时间】:2017-08-07 15:09:08
【问题描述】:

我有一个简单的 Shiny 应用程序,它可以抛出 Error:cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame。我正在尝试根据用户选择的line 将名为positions 的data.frame 子集化为positions$LineCode。下面是我的代码。

library(shiny)
library(shinydashboard)

lines <- c('All','RD','YL','GR','BL','OR','SV')

ui <- dashboardPage(
  dashboardHeader(title="Metro Positions"),
  dashboardSidebar(
    sidebarMenu(
      selectInput('line', 'Select Line:', lines)
    )
  ),
  dashboardBody(
    tableOutput('positionstable')
  )
)

positions <- read.csv('positions.csv')

server <- function(input, output, session) {
  pos <- reactive({
    if(input$line!='All'){
      pos <- positions[positions$LineCode==input$line,]
    }
    return(pos)
  })

  output$positionstable <- renderTable(pos())
}

shinyApp(ui, server)

【问题讨论】:

  • 应该是input$lines
  • 不,selectInput 的名字是'line'。

标签: r shiny


【解决方案1】:

我认为问题在于您的反应式不知道pos 变量是什么,以防if 语句未执行,即input$line == 'All'。然后你要求它在上下文中返回未知变量。

【讨论】:

  • 谢谢,将if 语句更改为下面的代码修复它:if(input$line!='All'){ pos &lt;- positions[positions$LineCode==input$line,] } else { pos &lt;- positions }
猜你喜欢
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
相关资源
最近更新 更多