2

I'm new to making tables in R and using html. I have R on two different laptops, and when working on one of the computers, when I use htmlTable, xtable, etc, a table appears in the viewer on the ride side of RStudio. However, on the other computer, no table displays in the viewer but html code appears in the console. Is there another package I need to render the html? I'm not using Markdown or anything, I just want to see simple tables using htmlTable in the viewer instead of the code.

I'm not sure how to make this reproducible, so I'm hoping someone knows the answer. On the computer that shows the actual table in the viewer, all I do is load the htmlTable package, and it works.

What am I missing? Is it related to my computer's settings, version of RStudio, etc?

3
  • 1
    It depends on the package. xtable just returns HTML code, which you can then insert into your document. Among others, knitr::kable and most LaTeX formatters do the same. Some packages generate a complete HTML widget that will display in the viewer, e.g. DT.
    – alistaire
    May 8, 2017 at 4:28
  • 2
    In this case, some package on one of the OP's dystopian systems has defined a print.html S3 method and it is taking the lead for printing since the class for htmlTable objects is c("html", "htmlTable", "character"). OP shld debug that OR do class(object_returned_from_call_to_htmlTable) <- c("htmlTable", "html", "character") before just calling print() OR explicitly call htmltools::html_print(object_returned_from_call_to_htmlTable) to achieve consistency in RStudio Viewer pane usage across their systems.
    – hrbrmstr
    May 8, 2017 at 4:44
  • Using htmltools::html_print worked! Thanks. If you make it an official answer I can give credit.
    – Mike
    May 9, 2017 at 0:58

1 Answer 1

2

It was a bug. While I used

if(!code) htmltools::html_print(out_html) else out_html

as @hrbrmstr suggested in the comments, it seems the fix was among Changes for 1.10 so it is now (v. 1.11.1) possible to just

output <- matrix(1:4,ncol=2,
    dimnames = list(list("Row 1", "Row 2"),
                    list("Column 1", "Column 2")))
print(htmlTable::htmlTable(output,useViewer=TRUE))

and I can simply use useViewer=!code in my wrapper functions. Note you can also do

print(htmlTable::htmlTable(output,useViewer=utils::browseURL))

as the documentation suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.