【问题标题】:user-defined functions in shinyServer does not show objectsshinyServer 中的用户定义函数不显示对象
【发布时间】:2018-04-11 14:17:54
【问题描述】:

我正在为我公司的报告编写 shinyApp。我在 ShinyServer 函数中编写了一个用户定义的函数来获取基于 input$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()
))
)

服务器.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() 函数,然后在下面的渲染函数中调用它。但是当我运行应用程序时,它会一直显示警告错误:找不到对象'students'。

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

【问题讨论】:

  • 那么,students 是在哪里定义的?您有 students.month&lt;-students 行,但该变量未在代码中的其他任何地方定义。
  • @MrFlick 它在另一个 .R 脚本中的 cacheData 函数中定义。此外,当我单独调用cacheData 时,studentsgraduates 的两个对象都可以出现。但是我在shineyServer内部调用它,它不起作用。谢谢你们的cmets。

标签: r shiny


【解决方案1】:

你的函数 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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多