0
wmedian <- function(directory, day) {
    files_list <- list.files(directory, full.names = TRUE)
    dat <- data.frame()
    for (i in 1:5) {
            dat <- rbind(dat, read.csv(files_list[i]))
    }
    dat_subset <- dat[which(dat[, "Day"] == day), ]
    median(dat_subset[, "Weight"], na.rm = TRUE)
}

The above code is returning the following error in R when I try to define it ""rror: unexpected input in "wmedian <- function(directory, day) {". I'm actually learning R right now and this code was straight from a tutorial. I tried re-writing the code, and also copy/pasting the code. Both are returning errors and I can't figure out why. What are your thoughts?enter image description here

The goal of this function is to bind a set of csv files together, that have columns "Weight" and "Day". The function binds those files together and return the median of Weight for a given Day.

  • 1
    Just to be clear, are you getting the error when you try to define the function, or when you are trying to run the function? It sounds like the former but I am unable to replicate. What version of R are you using (`sessionInfo()`)? Are you running other code before this that may be incomplete? – MrFlick Dec 11 '14 at 17:58
  • I'm getting the error when I try to define the function. –  Dec 11 '14 at 18:01
  • To confirm: I can define the function using R 3.1.1 and RStudio 0.98.1079. That said, it is difficult to debug the function without the CSV files. Could you link to the tutorial? – Berk U. Dec 11 '14 at 18:02
  • The contents of the CSV are irrelevant. The error means R is encountering unexpected input in the code itself. Sometimes this can happy with funny line endings between different OSes but since the OP claims to have tried re-typeing everything and still gets the same error, it's very odd. – MrFlick Dec 11 '14 at 18:03
  • @Daniel Well, you answered one of my questions. Can you post a screen shot of your R console where you input the code and get the error? Are you sure you get the exact same error when you re-type it? – MrFlick Dec 11 '14 at 18:05
  • here is where I got the data: https://github.com/derekfranks/practice_assignment –  Dec 11 '14 at 18:05
  • @MrFlick Yes you're right. My guess is that he has a open bracket / parentheses lying around elsewhere in the code. – Berk U. Dec 11 '14 at 18:05
  • I uploaded a screenshot if that helps! Thanks everyone for helping me out. –  Dec 11 '14 at 18:07
  • Where you got the data is irrelevant. And the error is NOT coming from that function. You have a function call to `list.files` that you improperly terminated with a right-curley-brace, `}`. The error message is telling where in that line the syntax error occurred. – IRTFM Dec 11 '14 at 18:14
  • The other puzzle is why there is no capital-E in the error message. I'm wondering if either the RStudio or R interpreter got corrupted somehow? Those right-curley-braces seem to be getting inserted too frequently. – IRTFM Dec 11 '14 at 18:20
  • @BondedDust thanks for your help. I'm not exactly sure what you mean. The line of code (outside the function) that calls list.files seems to terminate appropriately (I was able to store the variables just fine and call those variables). When I call list.files in the function, I do not see an incorrectly inserted curly brace. Would you mind pointing it out for me? –  Dec 11 '14 at 18:21
  • In your screenshot you have three instances of right-curley-braces that follow right-angle-brackets. The error messages each follow those instances. Did you type those instances? – IRTFM Dec 11 '14 at 18:23
  • Interestingly enough, when I run the code in regular R (and not R-studio), it works just fine. Should I re-install R studio? –  Dec 11 '14 at 18:24
  • At the very least you should close this session out without saving it and see if the problem recurs. Removing .Rdata files may be needed as well. – IRTFM Dec 11 '14 at 18:25
  • 1
    Closing/re opening worked. Thanks @BondedDust! Does this kind of thing happen frequently in R studio? I'm not sure it's good for my sanity to pour over code for a few hours to find its a software problem. –  Dec 11 '14 at 18:40
  • 1
    I've never had a similar problem with RStudio on a PC, although I more frequently use R with the MAC-GUI. I have not seen any other reports that are similar and I am fairly wider read in the R mailing list and SO. It provides many facilities for time-saving so I'd probably stay with it if I were you. – IRTFM Dec 11 '14 at 18:45

3 Answers3

4

Try reopen with encoding on the File button, the problem is about your encoding

DQ_happy
  • 505
  • 2
  • 6
  • 20
2

I got the same result (with the missing 'E').

I managed to solve it by copying piece by piece over to a new editor window and rewriting parts by hand until it all finally ran.

Seems like there was some special character (no idea which one though) that corrupted some very basic lines of code, e.g. library(ggplot2) failed.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
OLAO
  • 36
  • 2
0

I also have the same problem and it was due to the file only having CR at the end of each line instead of CR/LF at the end of each line. The reason the "E" is missing is because for some reason R doesn't know how to process CR on its own, so in the process of trying to print the line, it prints the CR which brings the cursor to the beginning of the line, and then it prints the end quote right on top of the E. So, change the CRs in your file to CR/LF and you should be good.

brfox
  • 323
  • 3
  • 14