How can I eliminate Rmarkdown error in parse when I knit? - r

I am getting an "error in parse`````````; attempt to use zero-length variable" when I try to knit the below.
It always occurs at line 38. Ideas?
setwd("~/Dropbox/varsel/")
library(olsrr)
simplemodel <- read.csv("~/Dropbox/varsel/Ranalyses var sel/simplemodel.csv")
model<-lm(out ~., data =simplemodel)
aicresult<- ols_step_backward_aic(model, details = TRUE)
aicresult
38 setwd("~/Dropbox/varsel/")
library(BMS)
library(dply)
swmodel<-read.csv ("~/Dropbox/causation project/sachs warner/mainvarsnomiss")
swmodelminuscol1<-select(swmodel,-1)
swbma<- bms(swmodelminuscol1, burn=100000, iter=200000, g="BRIC", mprior="uniform", nmodel=2000, mcmc="bd", user.int=FALSE)
coef(swbma, exact = TRUE)```

Related

Hmisc describe output when export as latex file giving API error 2

I am new to latex and I am trying to output the R results from describe( ) function through latex( ).
A .tex file is being created but it is not giving me a pdf output
df = data.frame(A = rnorm(n = 100, 0,1)) %>% dplyr::mutate(B = 2*A+ runif(n = 100,-0.05,0.05))
# call latex( )
f = 'sample.tex'
df_latext = Hmisc::latex(describe(df,descript = 'Sample Description'), file = f)
When I run df_latext in the R console it is giving me the following error:
MiKTeX Problem Report
Message: Windows API error 2: The system cannot find the file specified.
Data: path="C:\Users\domjo\AppData\Local\Temp\RtmpiYZcTM\file56f02ed47c3a.dvi"
Source: Libraries\MiKTeX\Core\File\win\winFile.cpp
Line: 301
MiKTeX: 22.8
OS: Windows 10.0.22000
Invokers: .../explorer/rstudio/rsession
SystemAdmin: no
Root0: C:\Users\domjo\AppData\Roaming\MiKTeX
Root1: C:\Users\domjo\AppData\Local\MiKTeX
Root2: C:\Users\domjo\AppData\Local\Programs\MiKTeX
UserInstall: C:\Users\domjo\AppData\Local\Programs\MiKTeX
UserConfig: C:\Users\domjo\AppData\Roaming\MiKTeX
UserData: C:\Users\domjo\AppData\Local\MiKTeX
Also, I tried running the .tex file created but its giving the following error saying Environment spacing undefined.:
! LaTeX Error: Environment spacing undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.1 \begin{spacing}{0.7}
?

tryCatch() does not suppress the error messages

I would like to create a function that does not print any error messages.
Let's say I have the following data:
library(fitdistrplus)
vec <- rnorm(100)
Then the following gives an error message:
fitdist(vec, "exp")
#> Error in computing default starting values.
#> Error in manageparam(start.arg = start, fix.arg = fix.arg, obs = data, : Error in start.arg.default(obs, distname) :
#> values must be positive to fit an exponential distribution
Now I would like to create a function that does return NULL. I tried this with tryCatch(). The problem is that fit_fn() still returns the error 'Error in computing default starting values':
fit_fn <- function(x){
tryCatch(fitdist(x, "exp"), error = function(e){ NULL })
}
fit_fn(vec)
#> Error in computing default starting values.
#> NULL
What is the way to do this? Only NULL should be printed here:
fit_fn(vec)
#> NULL
Created on 2021-11-02 by the reprex package (v2.0.1)
Desipte the fact that it says it's an error, the message that's being displayed is done not via the error mechanism, but the output is being printed directly to the console because it's already in it's own error handler. If you want to suppress that message, you'll need to capture the output of the function. Then you can ignore that output.
You can do that with
fit_fn <- function(x){
capture.output(result <- tryCatch(fitdist(x, "exp"),
error = function(e){ NULL }))
result
}
fit_fn(vec)
# NULL

Rpart Error with Anova: `Error in !isord : invalid argument type`

I'm running below code to call rpart function but it keeps giving me error Error in !isord : invalid argument type
# set arguments for rpart function
group.target.metric <- "loan_amount"
group.data.variables <- c(data.config$dict[is_group == TRUE, variable_name_modeling], group.target.metric)
print(group.data.variables)
group.training.data <- complete.data[, ..group.data.variables]
# run main code
group.tree <- rpart(formula = paste(group.target.metric, "~." ),
data = group.training.data,
method = "anova")
Can anyone please guide what this could be about?
Rpart version I'm using is 4.1-15
The issue was while creating data.config$dict it had missing definitions/datatypes for one variable I was using in the model. To check & update datatype in complete.data table use query:
complete.data <- UpdateDataTypes(complete.data, data.config$dict)

how to get document() to accept µ

I have been using this code
nutList <- gsub("_µg","",nutList, fixed = TRUE)
to remove the string "_µg" from the variable nutList for at least a year and have had no problems. Now I'm trying use document() to see where I have problems in getting ready for a package.
Error in parse(text = lines, n = -1, srcfile = srcfile) : [filename] : unexpected INCOMPLETE_STRING
nutList <- gsub("_
It appears that document() doesn't like the µ that is there. How can I modify the code so it is accepted?

Ignoring the symbol ° in R devtools function document()

I would like to create a package for internal usage (not to distribute somewhere). One of my functions contains the line
if (data$unit[i] != "°C") {
It works perfectly in the script, but if I want to create the documentation for my package using document() from devtools, i get the error
Error in parse(text = lines, keep.source = TRUE, srcfile = srcfilecopy(file, path_to_my_code: unexpected INCOMPLETE_STRING
279: if (! is.na(data$unit[i]){
280: if (data$unit[i] != "
addition: Warning message:
In readLines(con, warn = FALSE, n = n, ok = ok, skipNul = skipNul) :
invalid input found on input connection 'path_to_my_code'
If I delete the °-character, document() works. But I need this character there, so this is not an option.
When using double-\ in the if-clause, my function doesn't detect °C anymore as shown here:
test <- c("mg/l", "°C")
"\\°C" %in% test
[1] FALSE
If I use tryCatch, the documentation is also not created.
Replacing "°C" by gsub(pattern = '\\\\', replacement = "", x = '\\°C') causes the function to crash at the double-\ .
How can I tell document() that everything is fine and it should just create the files?

Resources