shinyServer中的用户定义函数不显示对象

时间:2017-10-30 14:41:15

标签: r shiny

我正在为我公司的报告工作shinyApp。我在ShinyServer函数中编写了一个用户定义的函数来获取基于输入$ month的数据集。以下是我在ui.R和server.R文件中的代码:

ui.R:

shinyUI(fluidPage(
fluidRow(
  column(3, 
         selectInput('month', 'Choose month for the report:', choices=as.character(getMonths()))
  )
),
hr(),

tabsetPanel( 
   tabPanel('Overview', 
            selectInput('subgroup', 'Select a group', choices = c("All students" = "All", 
                                                            "Active Military" = "Military", 
                                                             "Veteran" = "Veteran", 
                                                           "Civilian" = "Civilian")),
            downloadButton('downloadData', 'Data'),



            fluidRow(
                    column(width=8, htmlOutput('overviewtitle')),
                    column(width=8, plotOutput('overviewplot')),

   ))
       ,
hr()
))
)

server.R:

shinyServer(function(input, output, session) {

  getData <- function(input) {
    cacheData(cache.date = format(as.Date(input$month), '%Y-%m-15'),
          cache.dir='cache',
          envir=parent.frame())
students.month<-students
graduates.month<-graduates
return(list(students=students.month,
            graduates=graduates.month))
  }
output$overviewtitle <- renderText({
paste("<b>Month to Month Student Status Change for", input$subgroup, ":")
}) 

output$overviewplot <- renderPlot({
thedata<-getData(input)
m2mresults <- monthToMonthStatus(thedata$students, thedata$graduates)
m2mresults$Military <- 'Civilian'
m2mresults[m2mresults$ACTIVE_MIL_STUDENT == 'Y',]$Military <- 'Military'
m2mresults[!is.na(m2mresults$MVET),]$Military <- 'Veteran'
 if(input$subgroup == 'All'){bar_plot <- print(plot(m2mresults))}
 else {bar_plot <- print(plot(m2mresults[m2mresults$Military == input$subgroup,]))}
 bar_plot
        }, width = 'auto', height = 'auto')

我在ShinyServer中编写了一个函数getData(),然后在以下渲染函数中调用它。但是当我运行App时,它会发出警告错误:找不到对象“学生”。

我是ShinyApp的新手并且在服务器日工作这个问题,但找不到解决方案。有人可以帮忙吗?任何建议都可能有所帮助,我对此表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

你的函数cacheData让我很困惑 据我了解,它在父环境中为学生和毕业生分配变量。

首先,我会修改这个功能,然后让它返回一个包含学生和毕业生。所以你的功能看起来像这样:

<强> helpers.R

  cacheData <- function(cache.date, cache.dir) {

    # get student & graduates

    toReturn <- list(students, graduates)
    names(toReturn) <- c("students", "graduates")
    return(toReturn)
  }

然后,我会用它如下:

<强> server.R

source("helpers.R")
shinyServer(function(input, output, session) {

  data <- reactive({
    req(input$month)
    return(cacheData(cache.date = format(as.Date(input$month), '%Y-%m-15'),
                          cache.dir='cache'))
  })

  output$overviewtitle <- renderText({
    paste("<b>Month to Month Student Status Change for", input$subgroup, ":")
  }) 

  output$overviewplot <- renderPlot({
    req(data())
    m2mresults <- monthToMonthStatus(data()$students, data()$graduates)
    m2mresults$Military <- 'Civilian'
    m2mresults[m2mresults$ACTIVE_MIL_STUDENT == 'Y',]$Military <- 'Military'
    m2mresults[!is.na(m2mresults$MVET),]$Military <- 'Veteran'
    if(input$subgroup == 'All'){bar_plot <- print(plot(m2mresults))}
    else {bar_plot <- print(plot(m2mresults[m2mresults$Military == input$subgroup,]))}
    bar_plot
  }, width = 'auto', height = 'auto')