与许多其他人一样,我在html_nodes()
包中遇到rvest
函数的问题。我制作了一个Shiny应用程序,该应用程序进入公共数据网站并抓取它以查找最近发布的数据集的URL,该数据集始终是.csv文件。当我在本地使用它时,我的应用程序工作正常,但当我尝试将其部署到Shinyapps.io时,我收到以下错误消息:
Error : could not find function "xpath_element"
在做了一些挖掘之后,似乎有些人建议加载R的新版本/最新版本,我做了。其他人建议明确加载selectr
包,我也做了。还有一些人建议明确说明html_nodes()
中的css / xpath选择。我尝试过所有这些都无济于事。我是否错误地解释了rvest
包和语法错误,或者是否有其他力量在这里工作,我没有抓住?
这是我的代码和提前一百万的感谢:
library(shiny)
library(ggplot2)
library(knitr)
library(plyr)
library(dplyr)
library(reshape2)
library(tidyr)
library(lubridate)
library(rvest)
library(stringr)
library(selectr)
epa.data.page <- read_html("https://www.epa.gov/fuels-registration-reporting-and-compliance-help/spreadsheet-rin-generation-and-renewable-fuel")
epa.csv.page <- paste0("https://www.epa.gov",
epa.data.page %>%
html_nodes(css = "a") %>% # find all links
html_attr("href") %>% # get the url
str_subset("\\.csv") %>%
.[[1]]) # look at the first one
epa.data.date <-
epa.data.page %>%
html_nodes(css = ".fileinfo") %>%
html_text()
last.update <- word(epa.data.date[1], -2:-1)
last.update <- paste(last.update[1], last.update[2])
last.update <- substr(last.update, 1, (nchar(last.update)-1))
rindata <- read.csv(url(epa.csv.page))
rindata$rin.date <- as.Date(paste0(rindata$RIN.Year,"-",rindata$Production.Month,"-01"))
rindata$Fuel.Code <- as.factor(rindata$Fuel.Code)
rin.melt <- melt(rindata, .(Fuel.Code, RIN.Year, Production.Month, rin.date), .(RIN.Quantity, Batch.Volume), variable.name = "rf.metric")
tol12qualitative <- c("#332288", "#6699CC", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#661100", "#CC6677", "#AA4466", "#882255", "#AA4499")
col.usa <- c("#0071bc", "#205493", "#323a45", "#aeb0b5", "#02bfe7", "#046b99", "#9bdaf1", "#e31c3d", "#981b1e", "#e59393", "#fdb81e", "#2e8540")
ui <- shinyUI(fluidPage(
titlePanel("RIN Generation and Fuel Production by Month"),
sidebarLayout(
sidebarPanel(
dateRangeInput("rfs.period", "Date Range:",
start = "2010-07-01",
end = as.Date(paste0(max(rindata$RIN.Year), "-",
max(rindata$Production.Month[rindata$RIN.Year == max(rindata$RIN.Year)]), "-01")),
min = "2010-07-01",
max = paste0(max(rindata$RIN.Year), "-",
max(rindata$Production.Month[rindata$RIN.Year == max(rindata$RIN.Year)]), "-01"),
format = "MM d, yyyy",
startview = "decade"),
fluidRow(column(6, checkboxGroupInput("fuel.type", "D-Code:",
choices = c("D3" = "3", "D4" = "4", "D5" = "5", "D6" = "6", "D7" = "7"),
selected = c("D6" = "6"))),
column(6, radioButtons("metric.type", "Plot Type:",
choices = c("RIN Quantity" = "RIN.Quantity",
"Batch Volume" = "Batch.Volume",
"Both" = "dual.plot"),
selected = c("Both" = "dual.plot")),
checkboxInput("scale_y", "Scale Y-Axis", value = FALSE))),
uiOutput("ringen.message"),
br(),
a("Download this Data", href = epa.csv.page),
br(),
a("Contact Administrator", href="mailto:furlong.kyle@epa.gov"),
br(),
br(),
paste0("Date current as of ", last.update, ".")
),
mainPanel(
plotOutput("rin.plot")
),
position = "left"
)
)
)
server <- shinyServer(function(input, output) {
output$ringen.message <- renderUI({
if (input$metric.type != "Batch.Volume"){
tagList("Note: RINs are generated when a gallon of renewable fuel is produced. The quantity of RINs generated is determined by that fuel's Equivalence Value (EV). For more information,", a("read the regualtion defining this process", href = "https://www.ecfr.gov/cgi-bin/text-idx?rgn=div6&node=40:17.0.1.1.9.13#se40.19.80_11415"))
}
else
return()
})
title.text <- reactive({
if (input$metric.type == "dual.plot"){
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2])) ){
paste("Reported Generation and Production,", months(input$rfs.period[1]), year(input$rfs.period[1]))
}
else
paste("Reported Generation and Production,", months(input$rfs.period[1]), year(input$rfs.period[1]), "-", months(input$rfs.period[2]), year(input$rfs.period[2]))
}
else{
if (input$metric.type == "RIN.Quantity"){
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2])) ){
paste("Reported RIN Generation,", months(input$rfs.period[1]), year(input$rfs.period[1]))
}
else
paste("Reported RIN Generation,", months(input$rfs.period[1]), year(input$rfs.period[1]), "-", months(input$rfs.period[2]), year(input$rfs.period[2]))
}
else{
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2])) ){
paste("Reported Fuel Production,", months(input$rfs.period[1]), year(input$rfs.period[1]))
}
else
paste("Reported Fuel Production,", months(input$rfs.period[1]), year(input$rfs.period[1]), "-", months(input$rfs.period[2]), year(input$rfs.period[2]))
}
}
})
output$rin.plot <- renderPlot({
if (is.null(input$scale_y) || input$scale_y==FALSE){
if (input$metric.type != "dual.plot"){
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2]))){
rf.rin.plot <- ggplot(data = rindata[which(rindata$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type),]) +
geom_bar(stat = "identity", aes(x = Fuel.Code, y = rindata[rindata$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type,
input$metric.type]/1000000, fill = Fuel.Code)) +
labs(x = "", y = ifelse(input$metric.type == "RIN.Quantity", "RINs (Millions)", "Renewable Fuel (Million Gallons)"), title = title.text()) +
coord_flip() + theme(legend.position = "") +
scale_fill_manual(breaks = c("3", "4", "5", "6", "7"),
labels = c("D3", "D4", "D5", "D6", "D7"),
values = tol12qualitative[1:length(input$fuel.type)])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
else{
rf.rin.plot <- ggplot(data = rindata[which(rindata$rin.date %in% floor_date(input$rfs.period[1], unit = "month"):input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type),]) +
geom_line(aes(x = rin.date, y = rindata[which(rindata$rin.date %in% floor_date(input$rfs.period[1], unit = "month"):input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type),
input$metric.type]/1000000, color = Fuel.Code)) +
labs(x = "", y = ifelse(input$metric.type == "RIN.Quantity", "RINs (Millions)", "Renewable Fuel (Million Gallons)"), title = title.text()) +
scale_color_manual(name = "D-Code",
breaks = c("3", "4", "5", "6", "7"),
labels = c("D3", "D4", "D5", "D6", "D7"),
values = tol12qualitative[1:length(input$fuel.type)])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
print(rf.rin.plot)
}
else{
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2]))){
rf.rin.plot2 <- ggplot(data = rin.melt[which(rin.melt$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rin.melt$Fuel.Code %in% input$fuel.type),]) +
geom_bar(stat = "identity", aes(x = interaction(Fuel.Code, rf.metric), y = value/1000000, fill = Fuel.Code)) +
labs(x = "", y = "Quantity (Millions)", title = title.text()) +
coord_flip() + theme(legend.position = "") +
scale_x_discrete(breaks = c("3.RIN.Quantity", "4.RIN.Quantity", "5.RIN.Quantity", "6.RIN.Quantity", "7.RIN.Quantity", "3.Batch.Volume", "4.Batch.Volume", "5.Batch.Volume", "6.Batch.Volume", "7.Batch.Volume"),
labels = c("D3 RINs", "D4 RINs", "D5 RINs", "D6 RINs", "D7 RINs", "D3 Batch Volume", "D4 Batch Volume", "D5 Batch Volume", "D6 Batch Volume", "D7 Batch Volume")) +
scale_fill_manual(values = tol12qualitative[1:length(input$fuel.type)])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
else{
rf.rin.plot2 <- ggplot(data = rin.melt[rin.melt$rin.date %in% floor_date(input$rfs.period[1], unit = "month"):input$rfs.period[2] &
rin.melt$Fuel.Code %in% input$fuel.type,]) +
geom_line(aes(x = rin.date, y = value/1000000, color = interaction(Fuel.Code, rf.metric))) +
labs(x = "", y = "Quantity (Millions)", title = title.text()) +
scale_color_manual(name = "D-Code",
breaks = c("3.RIN.Quantity", "4.RIN.Quantity", "5.RIN.Quantity", "6.RIN.Quantity", "7.RIN.Quantity", "3.Batch.Volume", "4.Batch.Volume", "5.Batch.Volume", "6.Batch.Volume", "7.Batch.Volume"),
labels = c("D3 RINs", "D4 RINs", "D5 RINs", "D6 RINs", "D7 RINs", "D3 Batch Volume", "D4 Batch Volume", "D5 Batch Volume", "D6 Batch Volume", "D7 Batch Volume"),
values = tol12qualitative[1:(2*length(input$fuel.type))])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
print(rf.rin.plot2)
}
}
else{
if (input$metric.type != "dual.plot"){
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2]))){
rf.rin.plot <- ggplot(data = rindata[rindata$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type,]) +
geom_bar(stat = "identity", aes(x = Fuel.Code, y = rindata[rindata$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rindata$Fuel.Code %in% input$fuel.type,
input$metric.type]/1000000, fill = Fuel.Code)) +
labs(x = "", y = ifelse(input$metric.type == "RIN.Quantity", "RINs (Millions)", "Renewable Fuel (Million Gallons)"), title = title.text()) +
coord_flip() + theme(legend.position = "", strip.text.x = element_blank()) +
scale_fill_manual(breaks = c("3", "4", "5", "6", "7"),
labels = c("D3", "D4", "D5", "D6", "D7"),
values = tol12qualitative[1:length(input$fuel.type)])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
else{
rf.rin.plot <- ggplot(data = rin.melt[rin.melt$Fuel.Code %in% input$fuel.type &
rin.melt$rin.date %in% floor_date(input$rfs.period[1], unit = "month"):input$rfs.period[2] &
rin.melt$rf.metric == input$metric.type,]) +
geom_line(aes(x = rin.date, y = value/1000000, color = Fuel.Code)) +
facet_wrap(~Fuel.Code!=6, nrow = 2, ncol = 1, scales = "free_y") +
labs(x = "", y = ifelse(input$metric.type == "RIN.Quantity", "RINs (Millions)", "Renewable Fuel (Million Gallons)"), title = title.text()) +
scale_color_manual(name = "D-Code",
breaks = c("3", "4", "5", "6", "7"),
labels = c("D3", "D4", "D5", "D6", "D7"),
values = tol12qualitative[1:length(input$fuel.type)]) +
guides(colour = guide_legend(override.aes = list(size=3))) +
theme(strip.text.x = element_blank())
}
print(rf.rin.plot)
}
else{
if ( (months(input$rfs.period[1])==months(input$rfs.period[2])) &&
(year(input$rfs.period[1])==year(input$rfs.period[2]))){
rf.rin.plot2 <- ggplot(data = rin.melt[rin.melt$rin.date %in% input$rfs.period[1]:input$rfs.period[2] &
rin.melt$Fuel.Code %in% input$fuel.type,]) +
geom_bar(stat = "identity", aes(x = interaction(Fuel.Code, rf.metric), y = value/1000000, fill = Fuel.Code)) +
labs(x = "", y = "Quantity (Millions)", title = title.text()) +
coord_flip() + theme(legend.position = "", strip.text.x = element_blank()) +
scale_x_discrete(breaks = c("3.RIN.Quantity", "4.RIN.Quantity", "5.RIN.Quantity", "6.RIN.Quantity", "7.RIN.Quantity", "3.Batch.Volume", "4.Batch.Volume", "5.Batch.Volume", "6.Batch.Volume", "7.Batch.Volume"),
labels = c("D3 RINs", "D4 RINs", "D5 RINs", "D6 RINs", "D7 RINs", "D3 Batch Volume", "D4 Batch Volume", "D5 Batch Volume", "D6 Batch Volume", "D7 Batch Volume")) +
scale_fill_manual(values = tol12qualitative[1:length(input$fuel.type)])+
guides(colour = guide_legend(override.aes = list(size=3)))
}
else{
rf.rin.plot2 <- ggplot(data = rin.melt[rin.melt$rin.date %in% floor_date(input$rfs.period[1], unit = "month"):input$rfs.period[2] &
rin.melt$Fuel.Code %in% input$fuel.type,]) +
geom_line(aes(x = rin.date, y = value/1000000, color = interaction(Fuel.Code, rf.metric))) +
labs(x = "", y = "Quantity (Millions)", title = title.text()) +
scale_color_manual(name = "D-Code",
breaks = c("3.RIN.Quantity", "4.RIN.Quantity", "5.RIN.Quantity", "6.RIN.Quantity", "7.RIN.Quantity", "3.Batch.Volume", "4.Batch.Volume", "5.Batch.Volume", "6.Batch.Volume", "7.Batch.Volume"),
labels = c("D3 RINs", "D4 RINs", "D5 RINs", "D6 RINs", "D7 RINs", "D3 Batch Volume", "D4 Batch Volume", "D5 Batch Volume", "D6 Batch Volume", "D7 Batch Volume"),
values = tol12qualitative[1:(2*length(input$fuel.type))])+
guides(colour = guide_legend(override.aes = list(size=3))) +
facet_wrap(~Fuel.Code!=6, nrow = 2, ncol = 1, scales = "free_y") +
theme(strip.text.x = element_blank())
}
print(rf.rin.plot2)
}
}
})
})
shinyApp(ui = ui, server = server)