为什么我的 Shiny 应用程序没有发布

2024-02-28

我在尝试发布 Shiny 应用程序时遇到问题。

这是我发布的应用程序的代码:

UI:

library(shiny)
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  titlePanel("Visualizing Pitcher Statistics"),
  sidebarLayout(
    sidebarPanel(
      helpText("Data from Baseball Prospectus"),
      helpText("by Julien Assouline"),

      sliderInput("yearinput", "YEAR", 
                  min = 1970, max = 2016, value = c(2000, 2016),
                  animate = TRUE),

      selectInput("xcol", "X Axis", 
                  choices = c("YEAR","AGE","NAME","G","GS","PITCHES","IP","IP.Start","IP.Relief","W","L","SV","BS","PA","AB","R","ER","H","X1B","X2B","X3B","HR","TB","BB","UBB","IBB","SO","HBP","SF","SH","PPF","FIP","cFIP","ERA","DRA","PWARP","TEAMS","ROOKIE","League")),

      selectInput("ycol", "y Axis", 
                  choices = c("PWARP","YEAR","NAME","AGE","G","GS","PITCHES","IP","IP.Start","IP.Relief","W","L","SV","BS","PA","AB","R","ER","H","X1B","X2B","X3B","HR","TB","BB","UBB","IBB","SO","HBP","SF","SH","PPF","FIP","cFIP","ERA","DRA","TEAMS","ROOKIE","League")),

      checkboxInput(inputId = "smoother",
                    label = "show smoother",
                    value = FALSE),

      downloadButton("downloadPNG", "Download as a PNG file")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Scatterplot", plotOutput("plot1"),
                 verbatimTextOutput("descriptionTab1"), value = "Tab1"),
        tabPanel("Line Chart", plotOutput("plot2"),
                 verbatimTextOutput("descriptionTab2"), value = "Tab2"),
        id = "theTabs"
      ))
  )
)

server:

server <- function(input, output){ 
  ScatterPlot <- reactive({ 
    BP_Pitcher_1967_2016 <- read.csv("/Users/julienassouline/BP Pitcher 1967 2016.csv", header=TRUE, check.names = FALSE)
    Filtered1 <- BP_Pitcher_1967_2016 %>%
      filter(
        YEAR >= input$yearinput[1],
        YEAR <= input$yearinput[2]
      )
    p <- ggplot() +
      geom_point(data = Filtered1, aes_string(x = input$xcol, y = input$ycol)) + 
      Julien_theme() 

    if(input$smoother)
      p <- p + geom_smooth(data = Filtered1, aes_string(x = input$xcol, y = input$ycol), colour = "red")
    print(p)
  })

  output$plot1 <- renderPlot({
    print(ScatterPlot())

    output$downloadPNG <- downloadHandler(
      filename = "Graph.png",
      content = function(file){
        png(file)
        print(ScatterPlot())
        dev.off()
      })
  })

  linechart <- reactive({ 
    BP_Pitcher_1967_2016_trends <- read.csv("/Users/julienassouline/BP_Pitcher_1967_2016_trends.csv", header=TRUE, check.names = FALSE)

    Filtered2 <- BP_Pitcher_1967_2016_trends %>%
      filter(
        YEAR >= input$yearinput[1],
        YEAR <= input$yearinput[2]
      )
    d <- ggplot() +
      geom_line(data = Filtered2, aes_string(x = input$xcol, y = input$ycol), colour = "blue", size = 1) + 
      Julien_theme() 
    print(d)

  }
  )

  output$plot2 <- renderPlot({
    print(linechart())

    output$downloadPNG <- downloadHandler(
      filename = "Graph.png",
      content = function(file){
        png(file)
        print(linechart())
        dev.off()
      })
  })


} 


shinyApp(ui = ui, server = server) 

当我运行该应用程序时,它工作得很好。但是,当我尝试发布它时,它首先告诉我“第 42 行路径应该是项目目录中的文件” “第 70 行路径应该是项目目录中的文件”

如果我尝试以任何方式发布它,我会收到此错误:

“错误无法打开连接”https://julien1993.shinyapps.io/Shiny-App-3/ https://julien1993.shinyapps.io/Shiny-App-3/

我尝试使用传递到数据框的 csv 文件创建一个新的 R 文档。我还将 R 文档保存在名为 Shiny-App-3 的文件中,我还尝试在其中添加 csv 文件。那是行不通的。

我也知道,这个问题。部署闪亮应用程序时找不到数据对象 https://stackoverflow.com/questions/29965979/data-object-not-found-when-deploying-shiny-app。也就是说,如果我将 csv 文件代码放在反应函数之外,并且在我的文档请求时,它仍然不起作用。

如果我不包括BP_Pitcher_1967_2016 <- read.csv("/Users/julienassouline/BP Pitcher 1967 2016.csv"代码行,或者这个BP_Pitcher_1967_2016_trends <- read.csv("/Users/julienassouline/BP_Pitcher_1967_2016_trends.csv", header=TRUE, check.names = FALSE)然后我得到错误object 'BP_Pitcher_1967_2016' not found and object 'BP_Pitcher_1967_2016_trends' not found.

就其价值而言,此处描述的方法也不起作用:Shiny/R 错误:路径应该是项目目录中的文件 https://stackoverflow.com/questions/33052640/shiny-r-error-paths-should-be-to-files-within-the-project-directory

有人知道问题是什么吗? 感谢所有帮助。


该错误消息只是告诉您不能使用绝对文件路径。尝试将两个数据文件(BP Pitcher 1967 2016.csv and BP_Pitcher_1967_2016_trends.csv)到与闪亮的程序相同的目录/文件夹中,并从代码中的名称中删除路径。

所以第 24 行应该是这样的:

BP_Pitcher_1967_2016 <- read.csv("BP Pitcher 1967 2016.csv",header=TRUE, check.names=FALSE)

第 70 行应如下所示:

BP_Pitcher_1967_2016_trends <- read.csv("BP_Pitcher_1967_2016_trends.csv", header=TRUE,check.names=FALSE)

测试一下。如果你做对了,它应该会起作用。然后尝试发布。那么它也应该可以正常工作,除非有另一个我们还没有看到的错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 Shiny 应用程序没有发布 的相关文章

随机推荐