This is an issue with ggvis, fill and 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. I also tried, as suggested on some posts, to add a group_by .. but it was not better. 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
. Apparently library dtplyr
is not handling the creation of a data.frame
via the structure
function.
So for your app to work you have to 'convert' tbl.consommation
to a data.frame
with the function as.data.frame(...)
.