2

If I run the following crosstab code, using the ctable function from the summarytools package:

library(summarytools)
data(mtcars)
varlist <- names(mtcars[,3:5])
crosstables <- list(NULL)
for (i in varlist){
  crosstables[[i]] <- ctable(mtcars[[i]], mtcars$cyl, prop = 'r', style="simple", method = "render", header=TRUE)
  view(crosstables[[i]])
  }

instead of seeing three crosstab tables in the RStudio viewer only the last one is displayed. If I attempt to display all three tables:

view(crosstables)

I get the following error message:

x must either be a summarytools object created with freq(), descr(), or a list of freq() / descr() objects created using by(), or a list of freq() objects created using lapply(). Support for by() used with ctable() may be available in future realeases.

Is there a way to stack all three tables in the same viewer window? Maybe a way to combine the html output files for the crosstabs?

hem
  • 1,012
  • 6
  • 11
RobertF
  • 824
  • 2
  • 14
  • 40
  • This hack is less than ideal, but I've used Window's snipping tool to have two plots side by side. Same could be used for tables. – Paul Jul 03 '19 at 03:06

2 Answers2

0

you can change view to print and knit to html

It's the same code:

library(summarytools)
data(mtcars)
varlist <- names(mtcars[,3:5])
crosstables <- list(NULL)
for (i in varlist){
  crosstables[[i]] <- ctable(mtcars[[i]], mtcars$cyl, prop = 'r', style="simple", method = "render", header=TRUE)
  print(crosstables[[i]])
  }

Only the last line is different. And then use RStudio's knitting feature:

enter image description here

wisamb
  • 470
  • 3
  • 11
  • @wisamb Could you include some code in your answer demonstrating how this is done? – RobertF Jul 03 '19 at 12:34
  • Thank you for posting the code - however I don't see the "Knit HTML" button on the toolbar of my RStudio session. Is there a separate package I need to install in addition to `knitr`? – RobertF Jul 15 '19 at 18:09
  • The "Knit" button has a drop down arrow which shows knit to HTML, PDF or Word. When you use the option, it will default as "Knit HTML". So check the drop down arrow. No need to install anything else. – wisamb Jul 16 '19 at 04:40
0

As an alternative to the markdown solution, there is an append parameter to the package's print method / view functions. So when you use the file parameter and direct outputs from your first cross-table to an html file, you can use the same file path for the 2 other ones using append=TRUE.

Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61