0

is there anyone who could help me with transposing a formattable? Below is some code that illustrates the problem. The script works fine if DF is used in formattable() but not if the transpose (tDF) is used. Any suggestions are more than welcome.

library(formattable)
DF <- data.frame(Ticker=c("", "", "", "IBM", "AAPL", "MSFT"),
                 Name=c("Dow Jones", "S&P 500", "Technology", 
                        "IBM", "Apple", "Microsoft"),
                 Value=accounting(c(15988.08, 1880.33, NA, 
                                    130.00, 97.05, 50.99)),
                 Change=percent(c(-0.0239, -0.0216, 0.021, 
                                  -0.0219, -0.0248, -0.0399)))


tDF <- t(DF)


formattable(tDF, list(
  Name=formatter(
    "span",
    style = x ~ ifelse(x == "Technology", 
                       style(font.weight = "bold"), NA)),
  Value = color_tile("white", "orange"),
  Change = formatter(
    "span",
    style = x ~ style(color = ifelse(x < 0 , "red", "green")),
    x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)))
)
WhoKnows19
  • 125
  • 10
  • When I run the `formattable(...)` part, I get the following error message: `Error in create_obj(x, "formattable", list(formatter = formatter, format = list(...), : argument "formatter" is missing, with no default` – nilsole Sep 04 '16 at 11:20
  • Answer is in the comment section. Thanks Nilsole! – WhoKnows19 Sep 04 '16 at 16:53

1 Answers1

2

The transpose turned the DF data frame into a matrix: documentation. Does this help you?

tDF <- as.data.frame(t(DF))

I am not an expert on this package, still the output of your original code is obvious:

> class(DF)
[1] "data.frame"
> class(tDF)
[1] "matrix"
nilsole
  • 1,663
  • 2
  • 12
  • 28
  • Nilsole, thanks for the response. That is indeed the problem. I'm not an expert on the package either but it seems the function formattable() needs a dataframe with named variables. Therefore my first solution was to try to transpose the original dataframe as proposed here http://stackoverflow.com/questions/7970179/transposing-a-dataframe-maintaining-the-first-column-as-heading?noredirect=1&lq=1. However it is not possible in the proposed solution to use the names assigned to the rows and therefore the problem remained. – WhoKnows19 Sep 04 '16 at 12:58
  • I found this: "In v0.2, area formatting is supported so user can format by column, row, or an area." https://github.com/renkun-ken/formattable/issues/36 Find this on pages 4/5 in this document: https://cran.r-project.org/web/packages/formattable/formattable.pdf – nilsole Sep 04 '16 at 13:10
  • That's it. Thank you! – WhoKnows19 Sep 04 '16 at 16:52