xpath - xpath_element in deployed Shiny App error -
like many other people, i'm having issues html_nodes()
function in rvest
package. i've made shiny app goes onto publicly available data website , scrapes find url of posted dataset .csv file. app works when use locally, when try deploy shinyapps.io, receive following error message:
error : not find function "xpath_element"
after doing digging, seems people have suggested loading newer/newest versions of r, did. , others have suggested explicitly loading selectr
package, have done. still others suggested explicitly stating css/xpath choice in html_nodes()
. i've tried of these no avail. misinterpreting rvest
package , syntax incorrectly or there other forces @ work here i'm not grasping?
here's code , million in advance:
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 links html_attr("href") %>% # url str_subset("\\.csv") %>% .[[1]]) # @ first 1 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 , fuel production 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 data", href = epa.csv.page), br(), a("contact administrator", href="mailto:furlong.kyle@epa.gov"), br(), br(), paste0("date current 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 generated when gallon of renewable fuel produced. quantity of rins generated determined fuel's equivalence value (ev). more information,", a("read regualtion defining 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 , production,", months(input$rfs.period[1]), year(input$rfs.period[1])) } else paste("reported generation , 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)
Comments
Post a Comment