【问题标题】:Why am I unable to print the proper output produced using the kable function from the knitr package?为什么我无法打印使用 knitr 包中的 kable 函数生成的正确输出?
【发布时间】:2018-05-04 03:49:37
【问题描述】:

我一直在尝试使用 kable 函数打印数据字典。这是我的代码:

dict<-build_dict(my.data = tweets_train,linker=linker,option_description = NULL, prompt_varopts = T)
kable(dict,format="latex",caption="Data dictionary for the Training dataset") %>%
  kable_styling(position = "center")

这是输出:

> kable(dict,format="latex",caption="Data dictionary for the Training dataset") %>%
+   kable_styling(position = "center")

\begin{table}

\caption{\label{tab:}Data dictionary for the Training dataset}
\centering
\begin{tabular}[t]{l|l|l|l}
\hline
variable name & variable description & variable options & notes\\
\hline
sentiment & Sentiment Score 0 for Negative sentences and 4 for Positive sentences & 0 to 4 & The polarity of the tweet (0 = negative, 2 = neutral, 4 = positive)\\
\hline
text & The tweets collected & ''  oh u can take me to the game with u  mor  to zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz so tired   good night hokies & The dataset Sentiment 140 that originated from Stanford University for the purpose of Training\\
\hline
\end{tabular}
\end{table}

我该如何纠正这个问题。第一次使用 knitr,因为我必须创建一个数据字典。任何建议将不胜感激。提前致谢。

【问题讨论】:

  • 输出有什么问题?如果您将该代码包含在 RMarkdown 文件中(输出为 PDF)应该没问题,这是您在做什么吗?
  • 可能是的!第一次尝试使用knitr。抱歉,还有一个问题:我是否在降价文件中包含 kable(dict,format="latex",caption="Data dictionary for the Training dataset") %>% kable_styling(position = "center") 是吗?
  • 您很可能必须将 all 代码从当前的 R 脚本转移到 RMarkdown 文件(如果您正在使用该文件,则很容易在 RStudio 中创建)。另一种方法是将生成的乳胶代码(您在当前输出中看到的内容)复制到 LaTeX 文件中,但您似乎不熟悉 LaTeX,因此 RMarkdown 可能是更简单的选择。
  • 非常感谢。我会这样做的。

标签: r knitr kableextra


【解决方案1】:

这是@Marius 提到的如何改变你的代码

你需要把它放在一个markdown文件中,如下所示:

将其粘贴到 r 块中

dict<-build_dict(my.data = tweets_train,linker=linker,
                 option_description = NULL, prompt_varopts = T)
knitr::kable(dict,format="latex",caption="Data dictionary for the Training dataset") %>%
knitr::kable_styling(position = "center")  

像这样将 knitr 放在 kable 之前调用库本身(或在 library(knitr) 之前调用它):

然后保存并编织 :) 希望有帮助

【讨论】:

  • 谢谢!欣赏它。
猜你喜欢
  • 2021-08-06
  • 2016-01-28
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多