I'm writing a Shiny app using ggvis. However, I have some problems with handling NULL values when I decided that I'll use ggvis (and dplyr) instead of ggplot2. The idea of the app is to load a file, display the columns of the file so the user can select what to plot on X and Y and than to display it with ggvis. My server.R:
library(shiny)
library(plyr)
library(ggplot2)
library(googleVis)
library(reshape2)
library(ggvis)
options(shiny.maxRequestSize=30*1024^2)
shinyServer(function(input, output, session) {
Data <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
if (input$datatype == "tel") {
mydata <- read.csv(inFile$datapath, skip=1, header=TRUE, sep=',')
source("clean_data.r", local=TRUE)
}
info <- list(mydata=mydata, all.names)
return(info)
})
output$chooseX <- renderUI({
df <- Data()$mydata
if (is.null(df)) return(NULL)
items=names(df)
#names(items)=items
selectInput("chooseX","Select X axis:",items, items[[117]])
})
output$chooseY <- renderUI({
df <- Data()$mydata
if (is.null(df)) return(NULL)
items=names(df)
selectInput("chooseY","Select Y axis:",items, items[[49]])
})
# allows pageability and number of rows setting
myOptions <- reactive({
list(
page=ifelse(input$pageable==TRUE,'enable','disable'),
pageSize=input$pagesize
)
} )
output$raw <- renderGvis({
if (is.null(input$file1)) { return() }
gvisTable(Data()$mydata[,1:50],options=myOptions())
})
reactive({
if (is.null(input$file1)) { return(NULL) }
df <- Data()$mydata
if(!is.null(input$chooseX)) { x <- paste(as.character(input$chooseX)) }
if(!is.null(input$chooseX)) { y <- paste(as.character(input$chooseY)) }
tel <- df %>% ggvis(~x, ~y) %>% layer_lines() %>%
set_options(width=400, height=600, keep_aspect=TRUE) %>%
bind_shiny("tel")
})
output$caption1 <- renderText( {
if (is.null(input$file1)) { return() }
"Cleaned data"
})
})
In the clean_data.R I just perform some operations on the data. The output is
structure(list(tel1= c(4.44445, 4.23688, 4.45421, 4.65202, 4.44689,
6.23443, 7.43101, 7.42124, 4.54701, 4.23688, 4.41758, 4.84493,
4.55189, 4.45421, 4.337, 4.65202, 4.54945, 4.45177, 4.43956,
4.34432), tel2= c(3.33089, 3.33089, 3.33334, 3.33334, 3.33089,
4.12943, 5.45054, 6.64957, 5.23565, 4.27839, 3.72894, 3.52625,
3.43102, 3.43102, 3.32845, 3.33334, 3.32845, 3.33334, 3.33334,
3.32845), tel3= c(1.11356, 0.90599, 1.12087, 1.31868, 1.116,
2.105, 1.98047, 0.77167, -0.688639999999999, -0.0415099999999997,
0.68864, 1.31868, 1.12087, 1.02319, 1.00855, 1.31868, 1.221,
1.11843, 1.10622, 1.01587)), .Names = c("tel1", "tel2", "tel2"
), row.names = c(NA, 20L), class = "data.frame")
In the ui.R I have nothing but a simple ggvisOutput("tel")
.
library(shiny)
library(ggvis)
shinyUI(pageWithSidebar(
headerPanel("Prototype v0.2"),
sidebarPanel(
fileInput('file1', 'Choose CSV File'),
radioButtons('datatype', 'Data Type',c('tel'='tel','Corrective Actions'="corrections"), 'tel'),
tags$head(tags$style(type="text/css",
"label.radio { display: inline-block; margin:0 10 0 0; }",
".radio input[type=\"radio\"] { float: none; }"))
),
mainPanel(
tabsetPanel(
tabPanel("Data Information",
h4(textOutput("caption1")),
checkboxInput(inputId = "pageable", label = "Pageable"),
conditionalPanel("input.pageable==true",
numericInput(inputId = "pagesize",
label = "Pupils per page",value=20,min=1,max=25)),
htmlOutput("raw"),
value = 1),
tabPanel("Tel Data",
ggvisOutput("tel"))
)
)) )
Before using ggvis, everything worked perfectly but it seems that ggvis has some problems handling NULL values. I checked https://groups.google.com/forum/#!msg/ggvis/BBTABX01RoQ/1UQMqgysVd0J and https://github.com/rstudio/ggvis/issues/333 but I still did not manage to find a workaround. The error message is:
Error in UseMethod("rename_") :
no applicable method for 'rename_' applied to an object of class "factor"
I turned on options(shiny.trace=TRUE)
and with it I receive:
SEND {"errors":[],"values":{"chooseX":null,"chooseY":null,"chooseY2":null,"caption1":"","y2axis":null,"raw":""},"inputMessages":[]}
SEND {"errors":{"chooseX":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"chooseY":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"chooseY2":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"y2axis":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"raw":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null}},"values":{"caption1":"Cleaned data"},"inputMessages":[]}
Any ideas for a workaround? Thank you very much!