Watts-College / paf-514-template

https://watts-college.github.io/paf-514-template/
0 stars 0 forks source link

Final Project-Sourcing utils.R while knitting function-demo.Rmd #62

Open jacobmorgan05 opened 5 months ago

jacobmorgan05 commented 5 months ago

I was trying to knit the function-demo, and I ran into an error in the code chunk where I load all the library packages and source the utils.R file:

# LOAD PACKAGES
library( dplyr )
library( pander )
library( knitr )
library( gender )

# SOURCE CUSTOM FUNCTIONS
source( "utils.R" )

and get the following error: image

I load the data in the next chunk, which looks like this:

# LOAD DATA
#URL <- params$url
#d <- read.csv( URL )

# FOR MANUAL TESTING ONLY
URL <- "https://raw.githubusercontent.com/Watts-College/paf-514-template/main/labs/batch-demo/asu-salaries-2020.csv"
d <- read.csv( URL )

I can't figure out why this is an issue. Do i need to load data before packages? Or do you think there is another issue?

Thanks!

lecy commented 5 months ago

I think you are encountering the same issue as this student:

https://github.com/Watts-College/paf-514-template/issues/58#issuecomment-1954662446

Mainly, the utils.R file should contain functions, but nothing else. I typically include code to test the functions, but I comment it out so it is not run when sourcing the file.

Note that for Step 7 the instructions state:

Create a function called get_study_sample() that takes the full salary dataset and returns a subset of employees that belong to the academic units listed above and belong to one of the five title categories we created.

If you copy the provided code directly into utils.R without adding a function wrapper then you will get that error when sourcing the file.

academic.units <- 
c("CISA-Intrdisp Hum & Comm", "CISA-Science & Mathmatics", 
"College of Health Solutions MS", "College of Health Solutions NT", 
"College of Health Solutions SH", "College Of Law", "English", "Hugh Downs School Of Comm", 
"Humanities Arts & Cultural", "Journalism & Mass Comm", "Ldrshp and Integrative Studies", 
"Math & Natural Sciences Div", "MDT Music", "Physics Department", 
"Psychology", "Sch Biological & Hlth Sys Engr", "Sch Compt Infor & Dec Sys Engr", 
"Sch Elect Comptr & Energy Engr", "Sch Engr Matter Trnsprt Energy", 
"Sch Future of Innov in Society", "Sch Sustain Engr & Built Envrn", 
"School Of Art", "School of Criminology & Crim J", "School Of Earth & Space Explor", 
"School of Geog Sci & Urban Pln", "School of Math & Stat Sciences", 
"School of Molecular Sciences", "School of Politics & Global St", 
"School Of Public Affairs", "School of Social Transform", "School Of Social Work", 
"SHPRS History Faculty", "Social & Behavioral Sciences", "Sols Administration & Faculty", 
"SOS Faculty & Researchers", "The Design School", "The Sanford School", 
"WPC Accountancy", "WPC Economics", "WPC Information Systems", 
"WPC Management", "WPC Supply Chain Management")

d <- 
  d %>% 
  filter( title != "" & ! is.na(title) ) %>% 
  filter( Department.Description %in% academic.units ) %>% 
  arrange( Department.Description, title )

You want to be able to execute these steps easily by calling a function in your salary-report.rmd document.

#wrapper function example:
# df input >> fx() >> df.new output

fx <- function( df ) {

  # your data recipe
  df.new <- 
    df %>% 
    data_steps()

  return( df.new )
}
jacobmorgan05 commented 5 months ago

Thanks, that was exactly the problem!