citing a t statistic inline with Rmarkdown

Hi. When rendering the below code as a pdf using RMarkdown, I get the following, unexpected formatting when citing the t statistic and p value inline: (t = c(t = -4.66705273025142), p = 0.00011).

The pdf document appears to automatically place the t statistic in parentheses and adds a "c(t=" before the value. The html document does not. Is it possible with pdf output to display both the t statistic and p value within the same parentheses and without the preceding "c(t=", as is the case with the html output?


title: troubleshooting inline code
    toc: no
# bookdown::html_document2

```{r libraries, include=FALSE}

```{r, echo=FALSE}
tstat_tbl <- mtcars %>%

There is a difference in mpg by engine type (t = `r tstat_tbl %>% select(statistic)`, p = `r tstat_tbl %>% select(p)`).

I don't have bookdown installed, and hence can't try myself, but I'll suggest you to use pull once (in place of select in inline codes) and see what happens.

And, I edited your thread. I added four backticks (````) before and after the code to display the file content as it is. I hope you won't mind.


While I suggested, I thought pull may work as it'd return a vector instead of a tibble. But unfortunately, now that doesn't seem to be enough explanation.

Here's what I tried:

title: "test"
  html_document: default
  pdf_document: default

blah blah blah


p <- t.test(formula = extra ~ group, data = sleep)
q <- tidy(x = p)

blah `r q %>% select(statistic)` blah `r q %>% select(p.value)` blah

blah `r q %>% pull(statistic)` blah `r q %>% pull(p.value)` blah

str(object = q)

blah blah blah

As you have noticed, HTML output is fine.

But PDF is not. While using select, it is showing c for the statistic value, but not for p value.

I checked the types of statistic and p.value, and both are of same type. So, there is some other reason that I do not know. May be someone more familiar with these may help.

System Information
> xfun::session_info()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Catalina 10.15.7
## Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
## Package version:
##   assertthat_0.2.1 backports_1.2.1  base64enc_0.1.3  broom_0.7.3     
##   cli_2.2.0        compiler_4.0.3   cpp11_0.2.4      crayon_1.3.4    
##   digest_0.6.27    dplyr_1.0.2      ellipsis_0.3.1   evaluate_0.14   
##   fansi_0.4.1      generics_0.1.0   glue_1.4.2       graphics_4.0.3  
##   grDevices_4.0.3  highr_0.8        htmltools_0.5.0  jsonlite_1.7.2  
##   knitr_1.30       lifecycle_0.2.0  magrittr_2.0.1   markdown_1.1    
##   methods_4.0.3    mime_0.9         pillar_1.4.7     pkgconfig_2.0.3 
##   purrr_0.3.4      R6_2.5.0         rlang_0.4.9      rmarkdown_2.6   
##   stats_4.0.3      stringi_1.5.3    stringr_1.4.0    tibble_3.0.4    
##   tidyr_1.1.2      tidyselect_1.1.0 tinytex_0.28     tools_4.0.3     
##   utf8_1.1.4       utils_4.0.3      vctrs_0.3.6      xfun_0.19       
##   yaml_2.2.1


I wanted an explanation why select behaves differently for statistic and p.value.

See the pdf output for this line:

blah `r q %>% select(statistic)` blah `r q %>% select(p.value)` blah

For statistic, it's showing c(t = -1.860813), while for p.value it is showing 0.07939414.

Is this difference expected? Because as far as I can see, both have same data type.

currently getting

blah c(t = -1.86081346748685) blah 0.0793941401873582 blah

expected option 1 (both shown as vector)

blah c(t = -1.86081346748685) blah c(p = 0.0793941401873582) blah

expected option 2 (both shown as scalar)

blah -1.86081346748685 blah 0.0793941401873582 blah

Or, anything that'll be same for both.

Does this expectation make sense?

Great, thank you. That worked. And also thank you for changing the backticks--I had struggled to figure out how to show the code as is, so I appreciate the hint.

Thanks for the solution. No additional explanation is required, but I am curious to know why "pull" works here and "select" does not. Any thoughts?

Thanks for following up! The reason for the difference is a bit of a mystery, maybe someone can chime in?

You get a difference here because html output and pdf output have different inline hook to process inline code chunk.

It seems the hook when html is used does an implicit conversion from data.frame to unnamed character vector when the hook for pdf output does not.

I am not sure if this is a bug or something we can improve - in any case it is better to use objects that could be converted and pasted to one character string.

Using select won't do that. You should stick with pull or do any formating to character string yourself.

As I said, this is because the inline code are not processed exactly the same when using HTML output and PDF output. It is not exactly the same function (inline hook in knitr) that is used. There is a small difference in the formatting that lead to this behavior. IMO which is not supposed to work in the first place

But Inline code chunk are supposed to be for output that are character strings - if a vector of string it will be pasted.

In the case above, the inline chunk output data.frame - this does not surprise me that there could be something odd. However I agree we could make rmarkdown more clever and convert a one row data.frame to its content cell as character, or at least make pdf and html output work the same.

However, my advice still remain: Inline code chunk should be R code that result to a character string (or could be coerce to it easily - like a numeric which will be formatted correctly by knitr)

Does it answer your question ?

Then it is me who have surely missed something.

What is your expected output ? Is it the one from HTML ? Or it is a different one ?
Can you share what you are expecting to be printed ?

Yes this make sense.

Option 2 should be what you get when using pull() and not select()and that is expected as pull() will result in a vector, not a data.frame.

For option 1, you'll need to build it yourself - that is the safest to get this behavior in time. You don't get that now in PDF because p.value is not a named vector in your tibble. look at the result of your str(q) - statistic has a name attribute, p.value have not.

For example using

blah `r glue::glue("c(t = {q$statistic})")` blah `r glue::glue("c(p = {q$p.value})")` blah
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.