首页 > 解决方案 > 与运行块相比,Knit 无法正常工作

问题描述

在编织 markdown 文档时,以下块仅在s数据帧中生成 NA,但独立运行该块按预期工作。lmer() 函数中调用的所有必要的 data.frames 都是在 Markdown 文档中较早创建的。我认为它可能与 get() 函数有关,但不明白为什么它可以用于运行块而不是在编织时。

s=data.frame(row.names = c("ss_on","ss_off","nonss_on","nonss_off","log_draws","log_length","ss_on_co2"),
             var_names=c("ss_on_","ss_off_","nonss_on_","nonss_off_","log_draws_","log_length_","ss_on_co2_"),
             log_twh=c(T,T,F,T,F,T,F),
             log_swh=c(T,T,T,T,T,F,F),
             twh = "",twh_uci = "",twh_lci = "",swh="",swh_uci="",swh_lci="")

for(i in which(s$log_twh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=t$coefficients[1]
  s$twh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$twh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
for(i in which(s$log_swh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=t$coefficients[1]
  s$swh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$swh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}

for(i in which(s$log_twh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=exp(t$coefficients[1])
  s$twh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$twh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}

for(i in which(s$log_swh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=exp(t$coefficients[1])
  s$swh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$swh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
print(s)

编织时插入数据框中的所有值s都是 NA,但在运行块时可以正常工作。

标签: rmarkdownr-markdown

解决方案


你应该检查你的环境是否干净。运行您提供的代码会引发几个错误(见下文)。

这似乎是代码在控制台中按预期运行或逐块运行但在编织时失败的最常见原因。

library(lme4)
#> Loading required package: Matrix
s=data.frame(row.names = c("ss_on","ss_off","nonss_on","nonss_off","log_draws","log_length","ss_on_co2"),
             var_names=c("ss_on_","ss_off_","nonss_on_","nonss_off_","log_draws_","log_length_","ss_on_co2_"),
             log_twh=c(T,T,F,T,F,T,F),
             log_swh=c(T,T,T,T,T,F,F),
             twh = "",twh_uci = "",twh_lci = "",swh="",swh_uci="",swh_lci="")

for(i in which(s$log_twh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=t$coefficients[1]
  s$twh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$twh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'nonss_on_twh' not found
for(i in which(s$log_swh==F)){
  lmer.fit <- lmer(y ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=t$coefficients[1]
  s$swh_uci[i]=t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1]
  s$swh_lci[i]=t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1]
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'log_length_swh' not found

for(i in which(s$log_twh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"twh")))
  t=summary(lmer.fit)
  s$twh[i]=exp(t$coefficients[1])
  s$twh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$twh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'ss_on_twh' not found

for(i in which(s$log_swh==T)){
  lmer.fit <- lmer(log(y) ~  (1| Location) , data=get(paste0(s$var_names[i],"swh")))
  t=summary(lmer.fit)
  s$swh[i]=exp(t$coefficients[1])
  s$swh_uci[i]=exp(t$coefficients[1]+as.data.frame(t$varcor)$sdcor[1])
  s$swh_lci[i]=exp(t$coefficients[1]-as.data.frame(t$varcor)$sdcor[1])
}
#> Error in checkFormulaData(formula, data, checkLHS = control$check.formula.LHS == : bad 'data': object 'ss_on_swh' not found
print(s)
#>              var_names log_twh log_swh twh twh_uci twh_lci swh swh_uci
#> ss_on           ss_on_    TRUE    TRUE                                
#> ss_off         ss_off_    TRUE    TRUE                                
#> nonss_on     nonss_on_   FALSE    TRUE                                
#> nonss_off   nonss_off_    TRUE    TRUE                                
#> log_draws   log_draws_   FALSE    TRUE                                
#> log_length log_length_    TRUE   FALSE                                
#> ss_on_co2   ss_on_co2_   FALSE   FALSE                                
#>            swh_lci
#> ss_on             
#> ss_off            
#> nonss_on          
#> nonss_off         
#> log_draws         
#> log_length        
#> ss_on_co2

reprex 包(v0.2.1)于 2019 年 5 月 18 日创建


推荐阅读