Recently, I developed a straightforward application that incorporates a selectInput
widget and a bar plot using rCharts
. My goal is to generate the plot with a legend showcasing all three books, but only display information about the selected book by default. For instance, switching from Book1
to Book2
will initially show details about Book2
, while the legend includes Book1
and Book3
(both deactivated by default) - allowing me to switch between them as needed. I suspect this issue lies within JavaScript, and despite my attempts to address it, no changes have occurred. Any suggestions on how to resolve this? Thank you!
library(shiny)
library(rCharts)
books <- c('Book1', 'Book2', 'Book3')
df <- data.frame(book = rep(books, each = 10),
year = rep(2000:2009, 3),
sale = sample(100:1000, 30, replace = T))
ui <- shinyUI(
fluidPage(
HTML("
<script>
$( document ).ready(function() {
if ( $(\"select#book div.selectize-dropdown div[data-value='Book1']\").hasClass('selected')) {
console.log('true');
$('#nvd3Plot .nv-legend g.nv-series').eq(1).addClass('disabled');
$('#nvd3Plot .nv-legend g.nv-series').eq(2).addClass('disabled');
} else {
console.log('false');
}
});
</script>"),
selectInput('book', 'Select a book', choices = books, selected = 'Book1'),
showOutput("nvd3Plot", "nvd3")
)
)
server <- function(input, output, session) {
output$nvd3Plot <- renderChart2({
chartObject <- nPlot(sale ~ year, group = "book", data = df, type = "multiBarChart")
chartObject$chart(
showControls = FALSE
)
return(chartObject)
})
}
shinyApp(ui, server)
Update
After stumbling upon this solution, I am uncertain how to implement it in R.