同时使用 formattable 和 plotly

Using formattable and plotly simultaneously

如果同时使用 formattable 和 plotly 则报错 "Error in last_plot() : The last plot doesn't exist" 如果下面的代码是 运行 这是 Nico Katze 在 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)))
)`

这个问题可以通过分离 plotly 包来解决,但问题是我想同时使用这两个 packages.Trying 来找到我最终在这个页面 http://www.ats.ucla.edu/stat/r/faq/referencing_objects.htm 上找到的解决方案。一个建议的解决方案是使用 formattable:: 将函数直接链接到包。但是,这会产生相同的错误,因此无法解决问题。有人能解决这个问题吗?

找到答案了!其实是在用formattable::,但是好像是style函数上的问题。在加载 plotly 和 formattable 时工作的代码下方。

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