0

I want to format the output to proper looking tables in rmarkdown with HTML as output.

I've having trouble formatting the output objects that result from ordinate function

nmds.bray <- ordinate(entero, method="NMDS", distance="bray")

and betadisperser:

betadisp_entero<-betadisper(entero_BC, sampledf$Nationality)

I tried with each to use pander, xtable, flextable, kable but they all failed.

The error was "Cannot coerce class ....to a data.frame error"

Geosphere
  • 315
  • 4
  • 15

1 Answers1

0

Kable etc. require an object that is coercible into a data frame as input. However, the output objects of those functions are not data frames or matrices, but list-like structures, which cannot be coerced into data frames (hence your error). It is not clear from your question which information you want to display from each object, but you can access each attribute using the $ operator, e.g. nmds.bray$points or betadisp_entero$vectors.

Edit:

The author has specified that they want to capture specific attributes. To capture any attributes in a tidy table, you can use the following code chunk, provided that you know which attributes you are interested in. You can inspect all attributes of an object using attributes(object)$names.

I'm assuming that you are using the phyloseq::ordinate function above, which internally uses vegan::metaMDS. To see which attributes are printed by print(nmds.bray), have a look at the source code on vegan's GitHub page. Likewise, to see which attributes are printed by vegan::betadisper, check out the source code for the print statement.

# Returns attribute: value mappings with readable names in a data.frame
tidy_attributes <- function(object, attributes){
  
  # Get the attribute names
  if (!is.null(names(attributes))){
    atts <- names(attributes)
  } else {
    atts <- attributes
  }
  
  # Store the attribute values in a data frame
  df <- data.frame(attribute = atts,
                   value = unlist(object[atts]))
  
  # Set readable names if names are given
  if (!is.null(names(attributes))){
    df$attribute <- attributes[df$attribute]
  }
  return(df)
}
tidy_attributes(object = nmds.bray, 
                attributes = c("stress" = "Stress", 
                               "nobj" = "Number of objects"))
gmt
  • 325
  • 1
  • 7
  • 1
    I understand better now, thanks. I want to display pretty much the same information that you can see in the output in the console from those two objects. For Bray, I'd like stress level, stress type. For betadisperser, top eigenvalues, Average distance to median. – Geosphere Apr 03 '23 at 16:05
  • 1
    `vegan::scores` function for `vegan::metaMDS` object has option `tidy = TRUE` that returns a data frame. However, I have no idea about function `ordinate` and whether it has anything to do with `vegan::metaMDS`. Certainly there is no such function in **vegan**. You better consult the package you are using (and that ain't **vegan**). As to `vegan::betadisper`: if you want a specifically formatted output, just format it like you please. – Jari Oksanen Apr 03 '23 at 19:34