Search code examples
rknitrkableextra

Why am I unable to print the proper output produced using the kable function from the knitr package?


I have been trying to print a data dictionary using the kable function. Here is my code:

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")

And this is the output:

> 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}

How do I rectify this. Using knitr for the first time as I had to create a data dictionary. Any suggestions would be highly appreciated. Thanks in advance.


Solution

  • This is how to shift your code as mentionned by @Marius

    You need to put it in a markdown file as below:

    Open markdown file

    paste this in the r chunk

    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")  
    

    like that and put knitr before kable to call the library itself (or call it before library(knitr)) :

    enter image description here

    Then save and knit :) Hope that helps