同时使用formattable和plotly

时间:2016-09-04 17:05:47

标签: r datatable shiny plotly

如果同时使用formattable和plotly错误" last_plot()中的错误:最后一个图不存在"如果运行以下代码,则会给出,这是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)))
)`

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

1 个答案:

答案 0 :(得分:3)

找到答案!它实际上是使用formattable ::但在样式函数上似乎是问题。在加载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)))
)