I am getting acquainted with ggvis and I am trying to use it in shiny. I am having trouble understanding how ggvis gets the data from the reactive Shiny expression. Here is the basic app from ggvis GitHub repository:
ui.R:
shinyUI(pageWithSidebar(
div(),
sidebarPanel(
sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
value = 10, step = 1),
uiOutput("plot_ui")
),
mainPanel(
ggvisOutput("plot"),
tableOutput("mtc_table")
)
))
server.R:
library(ggvis)
shinyServer(function(input, output, session) {
# A reactive subset of mtcars
mtc <- reactive({ mtcars[1:input$n, ] })
# A simple visualisation. In shiny apps, need to register observers
# and tell shiny where to put the controls
mtc %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
bind_shiny("plot", "plot_ui")
output$mtc_table <- renderTable({
mtc()[, c("wt", "mpg")]
})
})
Now mtc
is the reactive expression which is actually a function (or is it?), Its result is a data.frame. However it is piped to ggvis as a function. If you tried to pass the resulting data.frame like
mtc() %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
bind_shiny("plot", "plot_ui")
Shiny would start complaining along the lines that "Operation not allowed without an active reactive context". So what is actually going on?
The reason I am asking is that I would like to return additional objects which I want to use in ggvis. To be more precise I want to change x and y axis labels, where the labels are computed inside in the reactive expression, something like this:
mtc <- reactive({ list(data=mtcars[1:input$n, ],
labx = "Computed x axis label",
laby = "Computed y axis label")
})
mtc %>% ggvis(data=data,~wt,~mpg) %>%
layer_points() %>%
add_axis("x",title=labx) %>%
add_axis("y",title=laby) %>%
bind_shiny("plot", "plot_ui")
Is it possible to somehow exploit the structure of mtc()
output inside of ggvis call? Or it is only possible to pass the data.frame and then put your data in the data.frame?
Or is there another way to register ggvis object? In this question the ggvis output is registered with observe_ggvis
function, but it seems that it is not present in current ggvis version (0.3).
I am using ggvis version 0.3.0.1 and shiny 0.10.0 on R 3.1.1
See Question&Answers more detail:
os