【问题标题】:Formula output in knitr inline chunkknitr inline 块中的公式输出
【发布时间】:2016-01-28 09:02:52
【问题描述】:

我想用 knitr 写一篇可重复的社会科学论文。因此,我想使用 knitr 内联块打印验证性因子分析的拟合指数。

我的最小例子:

\documentclass{article}

\begin{document}

Run a lavaan model

<<lavaanmodel>>=
library(lavaan)
## cfa model from help
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

cfaout <- cfa(HS.model, data=HolzingerSwineford1939)
@

Specifiy a fitprintfunction

<<fpf_def>>=
fpf_la <- function(x){  fm_tmp <- fitmeasures(x)

                        cat("\\chi^2 = ", fm_tmp[c("chisq")],
                            ", df = ",    fm_tmp[c("df")],
                            ", RMSEA = ", fm_tmp[c("rmsea")],
                            ", CFI = ",   fm_tmp[c("cli")],
                            ", TLI = ",   fm_tmp[c("tli")],
                            ", SRMR = ",  fm_tmp[c("srmr")],
                            sep = "")}
@

Print the fit inline:\\

The specified model resulted in good model fit $\Sexpr{fpf_la(cfaout)}$

\end{document}

问题:$\Sexpr{fpf_la(cfaout)}$ 没有任何结果。 我猜想在内联块中处理非数字输出会导致问题?

【问题讨论】:

    标签: r latex knitr


    【解决方案1】:

    \Sexpr 中使用的函数应该返回输出。你的函数打印它。

    小例子:

    \documentclass{article}
    \begin{document}
    <<>>=
    showSquare <- function(x) {
      return(sprintf("%s^2 = %s", x, x^2))
    }
    @
    
    Let's square 42: $\Sexpr{showSquare(42)}$.
    
    \end{document}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多