benmarwick / wordcountaddin

Word counts and readability statistics in R markdown documents
Other
337 stars 33 forks source link

HTML output in console #44

Closed earcanal closed 2 years ago

earcanal commented 3 years ago

Describe the bug

The plugin sometimes writes raw HTML rather than the results in the console, e.g.

> wordcountaddin:::text_stats()
<table>
 <thead>
  <tr>
   <th style="text-align:left;"> Method </th>
   <th style="text-align:left;"> koRpus </th>
   <th style="text-align:left;"> stringi </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Word count </td>
   <td style="text-align:left;"> 229 </td>
   <td style="text-align:left;"> 211 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Character count </td>
   <td style="text-align:left;"> 1346 </td>
   <td style="text-align:left;"> 1345 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Sentence count </td>
   <td style="text-align:left;"> 19 </td>
   <td style="text-align:left;"> Not available </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Reading time </td>
   <td style="text-align:left;"> 1.1 minutes </td>
   <td style="text-align:left;"> 1.1 minutes </td>
  </tr>
</tbody>
</table>

To Reproduce

I haven't established a way to reproduce this, but it happens frequently. Restarting the session is a temporary fix.

Expected behavior

> wordcountaddin:::text_stats()
|Method          |koRpus      |stringi       |
|:---------------|:-----------|:-------------|
|Word count      |234         |216           |
|Character count |1396        |1395          |
|Sentence count  |20          |Not available |
|Reading time    |1.2 minutes |1.1 minutes   |

Session Info

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] koRpus.lang.en_0.1-3 koRpus_0.11-5        sylly_0.1-5         

loaded via a namespace (and not attached):
 [1] bookdown_0.20             digest_0.6.27             magrittr_2.0.1            evaluate_0.14             highr_0.8                 rlang_0.4.10             
 [7] stringi_1.5.3             fs_1.5.0                  rstudioapi_0.13           data.table_1.13.0         rmarkdown_2.5             tools_3.6.3              
[13] sylly.en_0.1-3            purrr_0.3.4               xfun_0.21                 yaml_2.2.1                compiler_3.6.3            wordcountaddin_0.3.0.9000
[19] htmltools_0.5.1.1 
tarensanders commented 3 years ago

I'm having the same issue (with both text_stats and readability), and it seems to occur if I switch between open documents. I'm not sure how to write a reprex for that scenario. But I can replicate it by:

  1. Call readability on the active file (works as expected)
  2. Switch to another file in rstudio
  3. Return to the original file and call readability again

I've had a look through the code an I can't understand why this happens.

benmarwick commented 3 years ago

This is a tricky one. We use knitr::kable to format the table for display in the console. Looking at discussions elsewhere of unexpected HTML output from that function, I see that updating to the most recent version of RStudio is typically the solution. Please try the preview version and let me know if that helps: https://www.rstudio.com/products/rstudio/download/preview/

Another possibility is that you have options set somewhere that change the way kable() behaves. Because making a table is a side-effect of the function it's tricky to pin down the things that control it's behavior

tarensanders commented 3 years ago

You're right Ben. Calling knitr::kable(head(mtcars[, 1:4]), "html") directly causes the same bug. Updating to the latest preview doesn't fix this. I'll submit a bug to knitr. Thanks for looking at this, and for the great package.

benmarwick commented 2 years ago

Thanks for following up here and with knitr, I'll close this for now.