|
| 1 | +--- |
| 2 | +title: "Debugging Excercises" |
| 3 | +date: "2024-10-24" |
| 4 | +categories: Slides |
| 5 | +output: |
| 6 | + html_document: |
| 7 | + keep_md: yes |
| 8 | + number_sections: yes |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +In the following examples, evaluate each problem using the criteria of a minimal reproducible example from [this StackOverflow post](https://stackoverflow.com/help/minimal-reproducible-example). |
| 13 | + |
| 14 | +Specifically, address the following things: |
| 15 | + |
| 16 | +1. Are the question and problem description complete? |
| 17 | + |
| 18 | +2. Is the issue reproducible? Does it have a description of the problem, with code that reproduces the problem? What could improve it? |
| 19 | + |
| 20 | +3. Is the example minimal? Does it include extra code or information that is not necessary? |
| 21 | + |
| 22 | +4. Does the question describe any attempted solutions? |
| 23 | + |
| 24 | + |
| 25 | +Then, using what you know about R and Python, try to debug the problem using the strategies in [the debugging chapter](https://srvanderplas.github.io/stat-computing-r-python/part-gen-prog/06-debugging.html). Once you are thoroughly stumped, or you believe you have solved the problem, compare your solutions to those given on StackOverflow using the link. |
| 26 | + |
| 27 | +# Why Doesn't this R function work? |
| 28 | + |
| 29 | +> I created the following function to append new strings on a vector of strings called "meals". However, when I use this function to append an string input into my "meals" vector, it does not work. |
| 30 | +
|
| 31 | +``` |
| 32 | + add <- function(str) { |
| 33 | + meals <- append(meals, as.character(str)) |
| 34 | + } |
| 35 | +``` |
| 36 | + |
| 37 | +[Link](https://stackoverflow.com/questions/39795961/why-doesnt-this-r-function-work?noredirect=1&lq=1) to original SO post. |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +# Function not working R |
| 42 | + |
| 43 | +> I've never programmed before and am trying to learn. I'm following that "coursera" course that I've seen other people post about — a course offered by Johns Hopkins on R programming. |
| 44 | +
|
| 45 | +> Anyway, this was supposed to be my first function. Yet, it doesn't work! But when I type out all the steps individually, it runs just fine... Can anyone tell me why? |
| 46 | +
|
| 47 | +``` |
| 48 | +pollutantmean <- function(directory, pollutant, id = 1:332){ |
| 49 | + x<- list.files("/Users/mike******/Desktop/directory", full.names=TRUE) |
| 50 | + y<- lapply(x, read.csv) |
| 51 | + z<- do.call(rbind.data.frame, y[id]) |
| 52 | +
|
| 53 | + mean(z$pollutant, na.rm=TRUE) |
| 54 | +} |
| 55 | +pollutantmean(specdata,nitrate,1:10) |
| 56 | +[1] NA |
| 57 | +Warning message: |
| 58 | +In mean.default(z$pollutant, na.rm = TRUE) : |
| 59 | + argument is not numeric or logical: returning NA |
| 60 | +
|
| 61 | +#### |
| 62 | +
|
| 63 | +x<- list.files("/Users/mike******/Desktop/specdata",full.names=TRUE) |
| 64 | +y<- lapply(x,read.csv) |
| 65 | +z<- do.call(rbind.data.frame,y[1:10]) |
| 66 | +mean(z$nitrate,na.rm=TRUE) |
| 67 | +[1] 0.7976266 |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +[Link](https://stackoverflow.com/questions/24123224/function-not-working-r/24141502) |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +# Inexplicable error when trying to export my R notebook |
| 76 | + |
| 77 | +> Getting this error from R Markdown when trying to export my .RMD |
| 78 | +
|
| 79 | +``` |
| 80 | +"Error in filter(Gastropods, Species == "Cellana") : object 'Species' not found Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> filter" |
| 81 | +``` |
| 82 | + |
| 83 | +> However, all my plots are coming out successfully. I can clearly see in the data that the species column is there and that Cellana is a species. No spelling errors or anything. |
| 84 | +
|
| 85 | +> My first 20 or so lines of code are below (between the ### symbols) |
| 86 | +
|
| 87 | + |
| 88 | +````{r echo=FALSE} |
| 89 | +cat(paste(readLines("https://raw.githubusercontent.com/unl-statistics/stat850/refs/heads/main/homework/debugging_3.txt"), collapse = '\n')) |
| 90 | +```` |
| 91 | + |
| 92 | +You can find this sample file [here](https://raw.githubusercontent.com/unl-statistics/stat850/refs/heads/main/homework/debugging_3.txt) |
| 93 | + |
| 94 | + |
| 95 | +[Link](https://stackoverflow.com/questions/44193367/inexplicable-error-when-trying-to-export-my-r-notebook) |
| 96 | + |
| 97 | + |
| 98 | +# Python if elif else statement |
| 99 | + |
| 100 | +> I'm trying to create a program with python that calculate the cost for shipping. |
| 101 | +
|
| 102 | +> However, I can't run the program to where it works properly. |
| 103 | +
|
| 104 | +> What ever my total is the same amount comes out as $6 for US and $8 for Canada. I can't seem to get pass that. |
| 105 | +
|
| 106 | +> Example: |
| 107 | +
|
| 108 | +``` |
| 109 | +total = raw_input('What is the total amount for your online shopping?') |
| 110 | +country = raw_input('Shipping within the US or Canada?') |
| 111 | +
|
| 112 | +if country == "US": |
| 113 | + if total <= "50": |
| 114 | + print "Shipping Costs $6.00" |
| 115 | + elif total <= "100": |
| 116 | + print "Shipping Costs $9.00" |
| 117 | + elif total <= "150": |
| 118 | + print "Shipping Costs $12.00" |
| 119 | + else: |
| 120 | + print "FREE" |
| 121 | +
|
| 122 | +if country == "Canada": |
| 123 | + if total <= "50": |
| 124 | + print "Shipping Costs $8.00" |
| 125 | + elif total <= "100": |
| 126 | + print "Shipping Costs $12.00" |
| 127 | + elif total <= "150": |
| 128 | + print "Shipping Costs $15.00" |
| 129 | + else: |
| 130 | + print "FREE" |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +[Link](https://stackoverflow.com/questions/19371643/python-if-elif-else-statement) |
0 commit comments