问题
What I'm trying is import (copy&paste) some data from Excel with comma as decimal points by using scan(dec=",") command, but I have a problem with knitr when compiling this chunk:
<<>>=
conc<-scan(dec=",")
9,562445749
37,66119516
72,9103195
@
I get this error: Line 22: Unexpected ','
Thanks for your help
回答1:
In this case scan()
requires your to run it in an interactive fashion (e.g. it waits for a blank line to terminate input), whereas knitr
/Sweave
runs R code in a non-interactive fashion. In general you should avoid code that requires interaction; in other words, you should guarantee the code can be executed without human interaction. For example, this runs on itself regardless of the environment:
conc <- scan(dec=",", text='9,562445749
37,66119516
72,9103195
')
It is especially important to avoid code which involves with human intervention for the sake of reproducible research, because intervention means the results can not be determined by code itself (depending on human input for each run).
回答2:
Try removing the space between your last line and the @
. This is a problem with Sweave, so perhaps it's a problem with knitr too. Since scan normally stops when you enter a blank line (and you don't want a blank line), you'll need to enter the number of items you want to scan.
<<>>=
conc<-scan(dec=",", n=3)
9,562445749
37,66119516
72,9103195
@
来源:https://stackoverflow.com/questions/11248705/knitr-fail-compiling-chunk-with-scandec