I am trying to use Shiny and ggvis to:
1) upload a data set
2) have the user select 2 columns (x, y)
3) return a ggvis plot displaying (x, y) from the uploaded data set
I've tried editing the examples from the Shiny Interactivity page as well as the movie explorer example. However, no chart is displayed.
I think my issue is around uploading the data set, but I don't know where to begin... Any suggestions?
Note - I've also tried this using rCharts, but I run into similar problems where no chart is displayed.
server.R
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
fileHeaderNames <- reactive({
infile <- input$datfile
if(is.null(infile))
return(NULL)
d <- read.csv(infile$datapath, header = T)
return(names(d))
})
# dynamic variable names
observe({
updateSelectInput(session, 'x', choices = fileHeaderNames())
updateSelectInput(session, 'y', choices = fileHeaderNames())
}) # end observe
# uploading data set
theData <- reactive({
validate(
need(input$datfile != "", "Please upload a file")
)
infile <- input$datfile
dat <- read.csv(infile$datapath,
header = T,
stringsAsFactors = F)
if(is.null(infile)) return(NULL)
data.frame(x = dat[, input$x],
y = dat[, input$y])
})
# A simple visualisation. In shiny apps, need to register observers
# and tell shiny where to put the controls
theData %>%
ggvis(~x, ~y) %>%
layer_points() %>%
bind_shiny("plot", "plot_ui")
})
ui.R
library(ggvis)
library(shiny)
shinyUI(pageWithSidebar(
div(),
sidebarPanel(
fileInput('datfile', ''),
selectInput('x', 'x:' ,'x'),
selectInput('y', 'y:', 'y'),
uiOutput("plot_ui")
),
mainPanel(
ggvisOutput("plot")
)
))