I am attempting to adapt an example (2.1.1) from the tutorial found here on interactive plots in shiny. I have a shiny app as follows:
Data:
seats = data.table(
ID = c("1","2","3","4","5","6"),
Row = c("A", "A", "A", "B", "B", "B"),
SeatNum = c(1,2,3,1,2,3),
y = c(1,1,1,2,2,2),
price = 45)
ui.R
fluidPage(
title = 'Select Table Rows',
h1('A Client-side Table'),
fluidRow(
column(6, DT::dataTableOutput('x1')),
column(6, plotOutput('x2', height = 500)),
column(3, verbatimTextOutput('x4')),
column(4, verbatimTextOutput('x5'))
),
hr(),
)
server.R
shinyServer(function(input, output, session) {
output$x1 = DT::renderDataTable(seats, editable = "row", server = FALSE)
# highlight selected rows in the scatterplot
output$x2 = renderPlot({
s = input$x1_rows_selected
par(mar = c(4, 4, 1, 1))
plot(seats$SeatNum, seats$y)
if (length(s)) points(seats[s, , drop = FALSE], pch = 19, cex = 2)
})
output$x4 = renderPrint({
s = input$x1_rows_selected
if (length(s)) {
cat('Combined price \n of all seats:\n\n')
cat(sum(seats[s,]$price))
}
})
output$x5 = renderPrint({
s2 = input$x1_rows_selected
if (length(s2)) {
cat('Total number of seats selected:\n\n')
cat(length(s2))
}
})
})
Upon launching the app I can select any of the first three rows and the plot reacts appropriately. However, from row 4 onwards the plot does not respond. I've played around with the if (length(s)) points(seats[s, , drop = FALSE], pch = 19, cex = 2)
line but I don't understand its behavior.