library(shiny) library(ggvis) options(shiny.launch.browser=TRUE) ## Scaled and unscaled values dat <- data.frame(x = 1:3, y = 1:3, f = c("red", "green", "black")) dat dat %>% ggvis(x = ~x, y = ~y, fill = ~f) %>% layer_points(size := 1000) dat %>% ggvis(x = ~x, y = ~y, fill := ~f) %>% layer_points(size := 1000) ## A simple shiny app demonstrating reactivity runApp(list( ui = basicPage( sliderInput('n', 'Number', 1, 100, value = 50), textOutput('text') ), server = function(input, output) { output$text <- renderText({ paste("The value of n is ", input$n) }) } )) ## Reactive computation parameters faithful %>% ggvis(x = ~waiting) %>% layer_histograms(binwidth = input_slider(min=1, max=20, value=11)) ## Reactive properties mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points( size := input_slider(10, 400, value=50, label="size"), fill := input_select(c("red", "blue"), label="color") ) ## Reactive data source dat <- data.frame(time = 1:10, value = runif(10)) # Add a new data point every 2 seconds ddat <- reactive({ invalidateLater(2000, NULL) dat$time <<- c(dat$time[-1], dat$time[length(dat$time)] + 1) dat$value <<- c(dat$value[-1], runif(1)) dat }) ddat %>% ggvis(x = ~time, y = ~value, key := ~time) %>% layer_points() %>% layer_paths() ## Reactive data source 2 library(tourr) aps <- 2 fps <- 30 mat <- rescale(as.matrix(flea[1:6])) tour <- new_tour(mat, grand_tour(), NULL) start <- tour(0) proj_data <- reactive({ invalidateLater(1000 / fps, NULL); step <- tour(aps / fps) data.frame(center(mat %*% step$proj), species = flea$species) }) proj_data %>% ggvis(~X1, ~X2, fill = ~species) %>% layer_points() %>% scale_numeric("x", domain = c(-1, 1)) %>% scale_numeric("y", domain = c(-1, 1)) %>% set_options(duration = 0) ## Direct interaction # This function receives information about the hovered # point and returns a string to display all_values <- function(x) { if(is.null(x)) return(NULL) paste0(names(x), ": ", format(x), collapse = "
") } mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_points() %>% add_tooltip(all_values, "hover") ## Movies app runApp('movies') ## Brushing app runApp('brush-summary')