how to resolve write_csv() function error message

45 views Asked by At

I am working on Rstudio Cloud. write_csv will not work and keeps returning this message:

"Warning: cannot open file 'cloud/project/covid.csv': No such file or directoryError in file(file, ifelse(append, "a", "w")) : cannot open the connection

This is the entire code leading up to this error:

library(httr)
library(rvest)

get_wiki_covid19_page <- function(wiki_base_url, parameters) {
  wiki_base_url <<- "https://en.wikipedia.org/w/index.php"
  parameters <<- list(title="Template:COVID-19_testing_by_country")
  response <<- GET(wiki_base_url, query=parameters)
  return(response)
}

get_wiki_covid19_page(wiki_base_url="https://en.wikipedia.org/w/index.php", 
                      parameters=list(title="Template:COVID-19_testing_by_country"))

root_html_node <- read_html(response)
table_node <- html_nodes(root_html_node, "table")
covid_data_table <- html_table(table_node[2])
data_frame <- as.data.frame(covid_data_table)

preprocess_covid_data_frame <- function(data_frame) {
  shape <- dim(data_frame)
  data_frame <- data_frame[!(data_frame$`Country.or.region`=="World"), ]
  data_frame <- data_frame[1:172, ]
  data_frame["Ref."] <- NULL
  data_frame["Units.b."] <- NULL
  names(data_frame) <- c("country", "date", "tested", "confirmed",
                         "confirmed.tested.ratio", "tested.population.ratio", 
                         "confirmed.population.ratio")
  data_frame$country <- as.factor(data_frame$country)
  data_frame$date <- as.factor(data_frame$date)
  data_frame$tested <- as.numeric(gsub(", ", "", data_frame$tested))
  data_frame$confirmed <- as.numeric(gsub(", ", "", data_frame$confirmed))
  data_frame$'confirmed.tested.ratio' <- 
    as.numeric(gsub(", ", "", data_frame$`confirmed.tested.ratio`))
  data_frame$'tested.population.ratio' <-
    as.numeric(gsub(", ", "", data_frame$`tested.population.ratio`))
  data_frame$'confirmed.population.ratio' <- 
    as.numeric(gsub(", ", "", data_frame$`confirmed.population.ratio`))
  return(data_frame)
}

processed_data_frame <- preprocess_covid_data_frame(data_frame)

and finally this is the line that produces the error

write.csv(processed_data_frame, file="cloud/project/covid.csv")

the result of getwd() is "cloud/project"

this does not work on my local Rstudio app either

0

There are 0 answers