WhoKnows19 WhoKnows19 - 1 year ago 122
R Question

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.

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(
style = x ~ ifelse(x == "Technology",
style(font.weight = "bold"), NA)),
Value = color_tile("white", "orange"),
Change = formatter(
style = x ~ style(color = ifelse(x < 0 , "red", "green")),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)))

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download