I tried to run my script in R Markdown. It does not run and gives the error below:
Quitting from lines 15-139 (m.Rmd)
Erro em $<-.data.frame(*tmp*, "efavirens", value = integer(0)) :
replacement has 0 rows, data has 5794
Calls: ... withVisible -> eval -> eval -> $<- -> $<-.data.frame
Execu��o interrompida
The following is a part of the script:
```{r, echo=FALSE}
suicidio <- read.csv2("E:/Spss/suicidio4.csv",header=T,sep=";")
str(suicidio)
suicidio<-subset(suicidio,desfecho != 9)
suicidio2<-subset(suicidio,Desfecho2 != 9)
suicidio$tempo<-as.numeric(suicidio$tempo)
suicidio$dias_inicio[suicidio$dias_inicio_cont == "#NULO!"]<-NA
suicidio$dias_fim[suicidio$dias_fim_cont == "#NULO!"]<-NA
suicidio$cor[suicidio$A19_COR == "#NULO!"]<-NA
suicidio$sexo<-factor(suicidio$sexo)
suicidio$cor<-factor(suicidio$cor)
suicidio$obito<-factor(suicidio$obito)
suicidio$escol2<-factor(suicidio$escol3)
suicidio$idade_hiv_cat<-factor(suicidio$idade_hiv_cat2)
suicidio$idade_hiv_cat<-factor(suicidio$idade_dtfim_cat)
suicidio$acomp_regular<-factor(suicidio$acomp_regular)
suicidio$drogas<-factor(suicidio$drogas)
suicidio$alcool<-factor(suicidio$alcool)
suicidio$efavirens<-factor(suicidio$efavirens)
suicidio$epilepsia<-factor(suicidio$epilepsia)
suicidio$tentativa<-factor(suicidio$tentativa)
suicidio$causa_externa<-factor(suicidio$causa_externa)
suicidio$causa_externa<-factor(suicidio$transgen)
suicidio$causa_externa<-factor(suicidio$cate_expo2)
suicidio$causa_externa<-factor(suicidio$etnia
)
library(survival)
y<-Surv(suicidio$tempo,suicidio$desfecho)
Can someone help me?
This is not a R Markdown error, this is an R error, telling you that you have a bug in your R code. Run the script line by line (CTRL+Enter is a good shortcut) and see which one gives the error. It is basically saying that you are trying to overwrite a column with 5794 cells/rows with a vector that has no data in it (something where length(x) would yield 0; printing the object would report factor(0) or character(0), or whatever the data type).
Related
I was trying to knit my document, but came across an error:
Error in eval (expr, envir, enclos) : object 'AVG' not found
Calls: ...withVisible -> eval_with_user_handlers -> eval -> eval
Execution halted
This was my code. I'm confused because I thought I defined AVG in the second line. Any ideas?
for (i in 1:30)
{AVG[i] <- mean(rnorm(100))
}
"SD" <- sd(AVG)
print(SD)
I have been trying to knit my rmd file, but keep getting an error message
line 56 Error in eval(expr, envir, enclos) : object 'gol.fac' not found Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval Execution halted
```{r}
data(golub, package = "multtest")
grep("Zyxin",golub.gnames[,2])
all <- golub[2124,gol.fac=="ALL"]
aml <- golub[2124,gol.fac=="AML"]
summary(all)
summary(aml)
```
Line 56 is the first line after the ```{r}.
I am not sure why it is giving me errors, when I am able to run the code before knitting
Using pvclust::pvclust , I got an error
Error in solve.default(crossprod(X, X/vv)) : Lapack routine dgesv:
system is exactly singular: U[2,2] = 0 Calls: ...
pvclust.merge -> lapply -> FUN -> msfit -> solve -> solve.default
Execution halted
I don't want stop analysis even if crossprod(X, X/vv) be singular matrix, so I tried to insert an if {...} block in pvclust::msfit to check whether crossprod(X, X/vv) is singular or not by matrixcalc::is.singular.matrix, and if it is so, return NA and continue.
After saved my.msfit.R which contain msfit which incerted if(!is.singular.matrix(...)) {...}else{...} in original pvclust::msfit,
methods::insertSource('/myFuncDir/my.msfit.R',package="pvclust",functions='msfit')
But I got error below
Error in assign(this, thisObj, envir = envwhere) :
cannot change value of locked binding for 'msfit'
In addition: Warning message:
In methods::insertSource(filename, package = "pvclust", functions = "msfit", :
cannot insert these (not found in source): "msfit"
Is there any solution? Should I request of the author of the pvclust package?
== Below added after posting ==
An accurate advice was given in comments to use try/catch syntax, but I don't think it will give me solution.
Concerning my poor English skill, I present a toy sample which tells the situation.
fun.a <- function(a1,a2,a3,a4){
sum1 <- a1 + a2
sum2 <- a2 + a3
sum3 <- a3 + a4
return(list(sum1,sum2,sum3))
}
fun.a(1,2,3,'Char')
Because that the sum3 will be an error, so fun.a(1,2,3,'Char') returns error.
But, I want to return
List [sum1, sum2, NaN]
If I use tryCatch(...,error=expr), sum1 to sum3(actually, solve(...)in pvclust::msfit) should be wrapped.
But, fun.a(msfit) is inner function of locked package(pvclust).
I am able to run this code normally in R.
setInternet2(use = TRUE)
download.file("http://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip", "Coursera-SwiftKey.zip", method = "curl", mode = 'wb')
unzip("Coursera-SwiftKey.zip", files=c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"), exdir=".")
But when i try to use knitR it gives me this error
Quitting from lines 15-21 (milestone.Rmd)
Error in parse(text = x, srcfile = src) : <text>:5:106: unexpected ')'
4: unzip("Coursera-SwiftKey.zip", files=c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"), exdir=".")
5: file.copy(c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"?)
^
Calls: <Anonymous> ... <Anonymous> -> parse_all -> parse_all.character -> parse
Execution halted
I am using R Studio and MAC OS X.
I tried removing the s from https but it still di not work.
I intend to record the errors in my R code while calling functions in a dataframe (ERR_LOG, say). I want to use 'try' to identify errors while calling a function,if any.The dataframe(ERR_LOG) will have the following columns :
Time : The time at which the function was called (Sys.time)
Loc : For which function call was this error recorded (name of the
function)
Desc : Description of the error which R throws at us (Error message
in R)
Example :
First I would like to initialize a blank dataframe 'ERR_LOG' with these columns
Then write the function
f <- function(a){
x <- a*100
return(x)
}
Now I put the output of the call to 'f' in 'chk'
chk <- try(f())
The above call gives the error 'Error in a * 100 : 'a' is missing' (description of the error)
Check
if(inherits(chk,'try-error'))
{then I want to populate ERR_LOG and stop the code execution}
How can this be done in R?
use tryCatch instead of try
Then inside tryCatch(), use the argument error=function(e){}
e will have an element named message, which is what you would like
Use the following call with browser to explore e$message:
x <- tryCatch(stop("This is your error message"), error=function(e) {browser()})
Note that your function need not be anonymous.
MyErrorParser <- function(e) {
m <- e$message
if (grepl("something", m))
do something
return (something_else)
}
## THEN
tryCatch(stop("This is a test"), error=MyErrorParser)