I am trying to create a shiny app for a class project. My idea is to have users input through checkboxes and then I used this info to filter my data and then perform a cluster analysis through kmeans and plot the results. I keep getting the following error:
Error in !is.null(value) && value : invalid 'y' type in 'x && y'
My code is below any help will be appreciated.
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(cluster)
library(fpc)
data_cost<-readRDS("Data/data2.rds")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Live in the City of Your Dreams"),
fluidRow(
column(3,h4("What's Your Budget"),sliderInput(inputId ="budget","Whats Your Budget",
min = 10000,
max = 1000000,
value = 30, step=10000)
),
column(9,h4("Your Results",plotOutput('plot')))),
#horizontal Ruler
hr(),
#create lower grid
fluidRow(
column(3,h4("Transportation"), checkboxInput(inputId ="trans","Public", "Private"),
br(),
h4("Education"),checkboxInput(inputId ="Educ","Preschool", "Primary")),
column(3,h4("Basket of Goods"),checkboxInput(inputId = "BOG","Loaf of White Bread", "Eggs", "Cheese", "Water", "Wine","Beer", "Chicken", "Apples", "Oranges", "Rice", "Tomatoes")),
column(3,h4("Housing"), checkboxInput(inputId ="Housing","Purchase-Center", "Purchase-Suburb", "Rent-Center", "Rent-Suburb"),
column(3,h4("Cost of Meal"),checkboxInput(inputId ="meal","Meal Inexpensive","Mid-Range", "McDonalds"),
br(),
h4("Amenities",checkboxInput(inputId ="amen","Fitness Club", "Tennis Court","Cinema"))))
))
# Define server logic required to draw a histogram
server <- function(input, output) {
#
# output$distPlot <- renderPlot({
# # generate bins based on input$bins from ui.R
# x <- faithful[, 2]
# bins <- seq(min(x), max(x), length.out = input$bins + 1)
#
# # draw the histogram with the specified number of bins
# hist(x, breaks = bins, col = 'darkgray', border = 'white')
# })
# store country names
count_names<-rownames(data_cost)
#create an index table to match column numbers of data with user selection
index<-data.frame(c("Public", "Private","Preschool", "Primary","Loaf of White Bread", "Eggs", "Cheese", "Water", "Wine","Beer", "Chicken", "Apples", "Oranges", "Rice", "Tomatoes","Purchase-Center", "Purchase-Suburb", "Rent-Center", "Rent-Suburb","Meal Inexpensive","Mid-Range", "McDonalds","Fitness Club", "Tennis Court","Cinema"),
as.double(c(16,17,27,28,4,5,6,7,8,9,11,12,13,14,15,25,26,20,21,1,2,3,22,23,24)))
# store user input into one vector
res<-c(input$trans,input$Educ,input$BOG,input$Housing,input$meal,input$amen)
#create ancillary function to identify user inputed columns for data
check<-function(value)
{
return(data[value==index[,2],1])
}
#identify the indices for the columsn
col_ind<-apply(res,1,check)
# reduce data for clustering
red_data<-data_cost[,col_ind]
#perform clusteirng
clus<-kmeans(red_data,iter.max = 100,centers=3)$cluster
#output Plot
output$plot<-clusplot(red_data, clus$cluster, color=TRUE, shade=TRUE,
labels=2, lines=0)
}
# # Run the application
shinyApp(ui = ui, server = server)