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!
studentsdefined? You have the linestudents.month<-studentsbut that variable is not defined anywhere else in the code. - MrFlickcacheDatafunction which is in another .R script. Additionally, when I callcacheDataindividually, both objects ofstudentsandgraduatescan show up. But I call it inside shineyServer, it does not work. Thank you for your comments. - Linna