Thanks for that. I've made the change to the brackets, but it is still throwing the same error. The error code is saying that the plotOutput() is causing the error, but that's probably because it's calling the server function? Fuller code below...
Can you see what's the problem?
Define UI
#==========
ui <- fluidPage(
fluidRow(
column(10, titlePanel(img(src='logo.svg', style='width:300px;', align='left'), windowTitle = 'pupilinform')),
column(2, style = "margin-top: 50px;", actionButton('dlbutt', 'Download PDF'))
),
fluidRow(
br(),
column(3, selectInput('topic', 'Topic', choices = c('Exercise', 'Sleeping', 'Life skills', 'Missing school'))),
column(3, selectInput('by', 'Plot by', choices = c('D1_01','YearGroup','SurveyYear'))),
column(3, selectInput('main', 'Analysing', choices = c('Cheltenham','Gloucester','Forest of Dean'))),
column(3, selectInput('comp', 'Comparator', choices = c('County')))
),
plotOutput('heatmap')
)
# Define server logic
#====================
server <- function(input, output) {
plotdata <- reactive({
by <- input$by
main <- input$main
comp <- input$comp
idvars <- c('District', by) #add nearest neighbour here
qvars <- qlist[groupQ=='yes' & qtype=='CHK' & topic==input$topic][['var']]
vars <- c(idvars, qvars)
a1 <- dat[, ..vars] #select the columns for this analysis
a2 <- melt(a1, id.vars=idvars, measure.vars=qvars) #change to long format
a3 <- a2[District==main,]
a4 <- a3[, .N, keyby=.(by=get(by), variable, value)] #count up values by comparators
a5 <- dcast(a4, by + variable ~ value, value.var='N', fun.aggregate = sum) #change to wide format
a6 <- a5[, .(Y, N, Sum=sum(Y,N), Percent=Y/sum(Y, N)), keyby=.(by, variable)]#calculate percentages
setnames(a6, old = 'by', new = paste0(by))
a7 <- a6[, Facet:=main]
c1 <- a2[grepl('*',District),] #wildcard for county
c2 <- c1[, .N, keyby=.(by=get(by), variable, value)] #count up values by comparators
c3 <- dcast(c2, by + variable ~ value, value.var='N', fun.aggregate = sum) #change to wide format
c4 <- c3[, .(Y, N, Sum=sum(Y,N), Percent=Y/sum(Y, N)), keyby=.(by, variable)]#calculate percentages
setnames(c4, old = 'by', new = paste0(by))
c5 <- c4[, Facet:=comp]
plotdat <- rbindlist(list(a7, c5), use.names = T)
return(plotdat)
})
plotheight <- reactive({
length(qlist[groupQ=='yes' & qtype=='CHK' & topic==input$topic][['var']])
})
output$heatmap <- renderPlot({
ggplot(plotdata(), aes(x=by, y=variable) + geom_tile(aes(fill = Percent), colour = "white")
},
height=plotheight()*50)
}
# Create Shiny object
#====================
shinyApp(ui = ui, server = server)