我在尝试发布我的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 <- 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/
我尝试使用传递给数据框的csv文件创建一个新的R文档。我还将我的R文档保存在名为Shiny-App-3的文件中,我还尝试添加csv文件。这不起作用。
我也知道这个问题。 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
和object 'BP_Pitcher_1967_2016_trends' not found
。
此处描述的方法也不起作用,因为它的价值: Shiny/R error: Paths should be to files within the project directory
有人知道问题是什么吗? 所有帮助表示赞赏。
答案 0 :(得分:5)
错误消息只是告诉您不能使用绝对文件路径。尝试将两个数据文件(BP Pitcher 1967 2016.csv
和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)
测试一下。如果你做得对,它应该有效。然后尝试发布。它应该也可以正常工作,除非我们还没有看到另一个错误。