简体   繁体   English

ggvis + layer_bars +填充

[英]ggvis + layer_bars + fill

This is an issue with ggvis, fill and layer_bars. 这是ggvis,fill和layer_bars的问题。 I'm trying to run the following code, and it doesn't work. 我正在尝试运行以下代码,但它不起作用。 Replacing my data by Iris seems to solve it, but I can't find why. 用Iris替换我的数据似乎可以解决问题,但是我找不到原因。 I also tried, as suggested on some posts, to add a group_by .. but it was not better. 正如某些帖子所建议的那样,我也尝试添加group_by ..,但这并不更好。 Any suggestion would bu welcomed ! 任何建议将不受欢迎!

The error I get is: 我得到的错误是:

ERROR : object 'coul' not found

Here is a code : 这是一个代码:

for (package in c('dtplyr', 'shiny', 'shinydashboard', 'ggvis')) {
  if (!require(package, character.only=T, quietly=T)) {
    tryCatch({
      install.packages(package)
      library(package, character.only = TRUE)}
      , error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
  }    
}
tbl.consommation <- structure(list(nom = c("A", "B", 
                                 "C", "D", "E", "F", "G", "H", 
                                 "I", "J", "K", "L", "M", "N", 
                                 "O", "P", "Q", "R", "S", 
                                 "T", "U"), 
                               pourcentage_consomme = c(0.8, 0.5, 
                                                            0.2, 0.1, 1.9, 0.3, 
                                                            0.2, 0.01, 0.1, 0.51, 
                                                            0.45, 0.5, 0.95, 0.13, 
                                                            0.66, 0.46, 0.42, 0.42, 
                                                            0.32, 0.5, 1.1), 
                               coul = c("#0000FF", "#FF0000", "#E2001C", 
                                        "#71008D", "#1C00E2", "#3800C6", "#FF0000", "#1C00E2", "#0000FF", 
                                        "#71008D", "#3800C6", "#0000FF", "#E2001C", "#0000FF", "#0000FF", 
                                        "#5500AA", "#5500AA", "#5500AA", "#3800C6", "#0000FF", "#0000FF"
                                        )), 
                          .Names = c("nom",  "pourcentage_consomme", "coul"), row.names = c(NA, -21L
                                      ), class = c("data.table", "data.frame"),  sorted = "nom")
ui <- dashboardPage(
  title="Test ggvis",
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Graphique", tabName = "Graph", icon = icon("signal"))
)
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "Graph",
          ggvisOutput("graphConso")
      )
    )

  )
)
server <- function(input, output) {
  tryCatch({
  graphConso <- 
    tbl.consommation %>% 
  ggvis(x = ~nom, y = ~pourcentage_consomme, fill =  ~coul) %>%
  layer_bars(width = 0.5) 
  graphConso %>% bind_shiny("graphConso")
}   , error=function(e){cat("ERROR :",conditionMessage(e), "\n")})

}
shinyApp(ui = ui, server = server)

The problem is caused by the construction of tbl.consommation . 问题是由tbl.consommation的构造引起的。 Apparently library dtplyr is not handling the creation of a data.frame via the structure function. 显然,库dtplyr不处理一个创建data.frame通过structure功能。

So for your app to work you have to 'convert' tbl.consommation to a data.frame with the function as.data.frame(...) . 因此, tbl.consommation您的应用正常工作,您必须使用功能as.data.frame(...)tbl.consommation转换为data.frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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