3

If formattable and plotly are used simultaneously the error "Error in last_plot() : The last plot doesn't exist" is given if the following code is run which is a problem already mentioned by Nico Katze in the comment section of http://www.magesblog.com/2016/01/formatting-table-output-in-r.html.

library(formattable)
library(plotly)
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)))



formattable(DF, 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 problem can be solved by detaching the plotly package but the problem is that I want to use both packages.Trying to find a solution I ended up on this page http://www.ats.ucla.edu/stat/r/faq/referencing_objects.htm. A proposed solution is linking the function directly to the package using formattable::. This however gives the same error so it doesn't resolve the problem. Does anyone have a solution for this problem?

WhoKnows19
  • 125
  • 10

1 Answers1

6

Found the answer!It is actually using formattable:: but on the style function which seem to be the problem. Below the code that is working when both plotly and formattable are loaded.

library(formattable)
library(plotly) 
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)))
DF

formattable(DF, list(
  Name=formatter(
    "span",
    style = x ~ ifelse(x == "Technology", 
                       formattable::style(font.weight = "bold"), NA)),
  Value = color_tile("white", "orange"),
  Change = formatter(
    "span",
    style = x ~ formattable::style(color = ifelse(x < 0 , "red", "green")),
    x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), x)))
)
WhoKnows19
  • 125
  • 10