简体   繁体   中英

How to transpose a formattable in R Shiny?

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)))
)

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM