【问题标题】:Shiny App Not Running in Shinyapps.io (Error: Object UI Not Found)Shinyapps.io 中未运行 Shiny 应用程序(错误:未找到对象 UI)
【发布时间】:2020-07-01 07:32:31
【问题描述】:

我已经在本地成功运行了以下 Shiny App。不幸的是,当我在 shinyapps.io 中运行它时,我从日志中收到以下错误消息: Force(ui) 中的错误:找不到对象 'ui'。即使经过广泛的跟踪,我也找不到任何其他说明为什么找不到 ui 的内容,并且它再次可以在本地运行。

这是完整的日志:


tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() 

Warning: replacing previous import ‘mgcv::multinom’ by ‘nnet::multinom’ when loading ‘cfbscrapR’ 

The following objects are masked from ‘package:shiny’: dataTableOutput, renderDataTable 

The following object is masked from ‘package:shiny’: serverInfo Attaching package: ‘rsconnect’ 

64: shinyApp Warning: Error in force: object 'ui' not found –

这是我的 ui.R 代码:

library(tidyverse)
library(cfbscrapR)
library(gt)
library(dplyr)
library(ggplot2)
library(DT)
library(shiny)
library(shinythemes)
library(rsconnect)
library(logger)

###Now create the ui function

ui <- fluidPage(
  titlePanel(h1("College Football Analytics")),
  sidebarPanel(
    checkboxGroupInput("selections", label = h2(
      "Choose Weeks to Analyze"),
      choices = list("Week 1" = 1, "Week 2" = 2,
                     "Week 3" = 3, "Week 4" = 4,
                     "Week 5" = 5, "Week 6" = 6,
                     "Week 7" = 7, "Week 8" = 8,
                     "Week 9" = 9, "Week 10" = 10,
                     "Week 11" = 11, "Week 12" = 12,
                     "Week 13" = 13, "Week 14" = 14,
                     "Week 15" = 15),
      selected = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
                   "12", "13", "14", "15")),
    actionButton("submit", "Update")
  ),
  mainPanel(
    h2("2019 Season"),
    DT::dataTableOutput("table"),
    theme = shinytheme("cerulean")
  )
)

至于 server.R:

library(tidyverse)
library(cfbscrapR)
library(gt)
library(dplyr)
library(ggplot2)
library(DT)
library(shiny)
library(shinythemes)
library(rsconnect)
library(logger)

pbp_2019 <- read.csv("pbp_2019.csv")
drives_2019 <- read.csv("drives_2019.csv")


#Define server logic
server <- function(input, output, session) {
  cfb.table2 <- reactive({
    input$submit
    isolate({
      req(input$selections)
      new.pbp_2019 <- subset(pbp_2019, week %in% input$selections)})
  })
  plays <- reactive({cfb.table2() %>% filter(rush == 1 | pass == 1)})
  offense <- reactive({plays() %>% group_by(offense_play) %>% summarise(ypa = mean(yards_gained[pass==1]), ypr = mean(yards_gained[rush==1]), num.plays = n()) %>% filter(num.plays > 300)})
  offense <- reactive({plays() %>% group_by(offense_play) %>% summarise(epa.pass.off = mean(EPA[pass==1]), success.rate = mean(success), epa.rush.off = mean(EPA[rush==1]), num.plays = n()) %>% filter(num.plays > 300)})
  defense <- reactive({plays() %>% group_by(defense_play) %>% summarise(epa.pass.def = mean(EPA[pass==1]), epa.rush.def = mean(EPA[rush==1]), num.plays = n()) %>% filter(num.plays > 300)})
  update.epa <- reactive({left_join(offense(), defense(), by = c("offense_play" = "defense_play"))})
  drives.table2 <- reactive({
    input$submit
    isolate({
      req(input$selections)
      new.drives_2019 <- subset(drives_2019, week %in% input$selections)})
  })
  games <- cfb_game_info(2019) %>% rename("game_id" = id)
  drives.off <- reactive({drives.table2() %>% left_join(games, by = c("game_id")) %>%
      mutate(
        adj_start_yardline = ifelse(offense == away_team, 100-start_yardline, start_yardline), 
        success = ifelse(drive_result %in% c("TD", "FG"), 1, 0),
        drive.pts = ifelse(drive_result == "TD", 6, ifelse(drive_result == "FG", 3, 0))) %>%
      group_by(offense) %>% 
      summarise(
        fp = mean(adj_start_yardline[adj_start_yardline > 10 & adj_start_yardline <40]), 
        srate = mean(success),
        drives = n(),
        drives.pts = sum(drive.pts))
  })
  drive.update.epa <- reactive({left_join(update.epa(), drives.off(), by=c("offense_play"="offense")) %>%
      mutate(pts.per.drive = drives.pts / drives)})
  cfb.table3 <- reactive({data.frame(drive.update.epa() %>% 
                                       select(offense_play, success.rate, epa.pass.off, epa.rush.off, epa.pass.def, epa.rush.def, fp, drives, pts.per.drive) %>% gt() %>%
                                       tab_header(title = "2019 Season"))})
  output$table = DT::renderDataTable({
    datatable(cfb.table3(),
              rownames = FALSE, 
              class = 'cell-border stripe',
              colnames = c('Team', 'Success Rate',
                           'Pass EPA', 'Run EPA',
                           'Pass EPA Def.',
                           'Run EPA Def.',
                           'Avg. Field Position',
                           'Drives',
                           'Points Per Drive'),
              list(pageLength = 25)) %>%
      formatPercentage(c('success.rate'),1) %>%
      formatRound(c('epa.pass.off'),3) %>%
      formatRound(c('epa.rush.off'),3) %>%
      formatRound(c('epa.pass.def'),3) %>%
      formatRound(c('epa.rush.def'),3) %>%
      formatRound(c('fp'),1) %>%
      formatRound(c('pts.per.drive'),3)
  })
}

#Run the application
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 这很可能是因为在某处之前存在一个相关错误,该错误导致ui 无法正确定义。之前是否有任何可能支持该理论的警告或错误? (也许是there is no package called ...?)
  • tidyverse_conflicts() ── ✖ dplyr::filter() 屏蔽 stats::filter() ✖ dplyr::lag() 屏蔽 stats::lag() 警告:替换之前的 import 'mgcv:: multinom' by 'nnet::multinom' when loading 'cfbscrapR' 以下对象被 'package:shiny' 屏蔽:dataTableOutput、renderDataTable 以下对象被 'package:shiny' 屏蔽:serverInfo 附加包:'rsconnect' 64: shinyApp 警告:有效错误:找不到对象“ui”
  • 请编辑您的问题并将其放在那里。评论对于那种东西(代码、数据、换行嵌入的东西等)来说是可怕的。

标签: r user-interface deployment shiny


【解决方案1】:

https://shiny.rstudio.com/articles/two-file.html 引用了一个包含两个文件的闪亮应用程序。在 0.10.2 版本之前,这是一项要求。从该页面(以及https://shiny.rstudio.com/articles/app-formats.html):

对于这样定义的应用程序,server.R文件必须返回server函数,ui.R文件必须返回UI对象(本例中UI对象由fluidPage()创建)。换句话说,如果文件包含其他代码(如实用程序函数),您必须确保文件中的最后一个表达式是服务器函数或 UI 对象。

具体来说,“包含其他代码” 包括以shinyApp(ui = ui, server = server) 结尾的server.R,这不会“返回”服务器对象。

所以这里有三个问题:

  • server.R 文件应该以 server 对象结尾,或者它的定义是该文件中的最后一件事,或者您必须添加 server 作为该文件中的最后一行代码。

  • 由于您使用的是双文件系统,因此您不需要使用shinyApp(ui, server)

  • 但是,既然您这样做了,ui 对象就没有在server.R 中定义或可用。 (事实上​​,我不知道你是否可以安全地假设文件的顺序是sourced,所以一个黑客可能是source("ui.R") in server.R,但我认为这不是你需要的。

可能的解决方案:

  1. 将这两个组合到一个文件中,也许是上面第二个链接中引用的app.R 设置。这样,保持shinyApp(ui, server) 通话。

  2. 保持文件原样,并删除对shinyApp的调用。

【讨论】:

  • 我保留了文件原样,删除了“shinyApp”调用,并将我之前抓取的两个文件包含在两个文件中。有效。非常感谢!!!!!!!
  • 如果解决了,请accept it。谢谢!
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多