0
votes

I am working on shinyApp for my company's report. I write a user-defined function inside ShinyServer function to get dataset that is based on input$month. The following is my code in ui.R and server.R files:

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

I write a function getData() inside ShinyServer and then call it in following render functions. But when I run App, it keeps warning errors: object 'students' not found.

I am new to ShinyApp and working on this issue for server days but could not find a solution. Could anybody help on this? Any suggestions could be helpful and I am appreciating on that. Thanks!

1
Well, where is students defined? You have the line students.month<-students but that variable is not defined anywhere else in the code. - MrFlick
@MrFlick It is defined in cacheData function which is in another .R script. Additionally, when I callcacheData individually, both objects of students and graduates can show up. But I call it inside shineyServer, it does not work. Thank you for your comments. - Linna

1 Answers

0
votes

Your function cacheData made me confused.
As I understand it, it assign variables students and graduates in the parent environment.

First of all, I would modify this function and make it return a named list containing students & graduates. So your function would look like something like that :

helpers.R

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

    # get student & graduates

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

And then, I would use it as below :

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