I am trying to use Shiny and ggvis for:
1) load a dataset
2), user can select 2 columns (x, y)
3) return the ggvis (x, y) graph display from the loaded dataset
I tried editing examples on the Interactivity page as well as movie explorer . However, the chart is not displayed.
I think my problem is loading the dataset, but I donβt know where to start ... Any suggestions?
Note. I also tried this with rCharts, but I am having similar problems when the chart is not 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))
})
observe({
updateSelectInput(session, 'x', choices = fileHeaderNames())
updateSelectInput(session, 'y', choices = fileHeaderNames())
})
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])
})
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")
)
))