1
votes

I am new to shiny and am practicing making a small app. I am trying to make two columns reactive to changes in the choices the user makes but cannot seem to find a straightforward solution online. I am hoping someone can help me here.

1) How do I set up my code below so that when users changes the years in sliderInput, the plotly graph below is updated to show data only for the selected years?

2) How do I set up my code below so that when the user chooses some countries, only those countries appear in the plotly graph below?

3) How to format the content of sliderInput so that for year, instead of showing "2,012", it shows "2012" in the dashboard on the slider?

library(shiny)
library(plotly)
library(shinydashboard)

#Creating dataset
`Country Name`<- c("country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3")

Year <- c(2010, 2010, 2010,
          2011, 2011, 2011,
          2012, 2012, 2012,
          2013, 2013, 2013)

GDP <- c(2345, 2465, 3985,
         3453, 3748, 4847,
         5674, 4957, 5763,
         6475, 9387, 7564)

dataset <- data.frame(`Country Name`, Year, GDP)

#Creating shiny app
ui <- dashboardPage(
  dashboardHeader(title = "Top 3 Countries"),
  dashboardSidebar(

    sliderInput(inputId = "Year",
                label = "Choose a timeframe",
                min = min(dataset$Year), 
                max = max(dataset$Year),
                value = c(min(dataset$Year),max(dataset$Year)), 
                ticks = TRUE,
                round = TRUE,
                step = 1),

    selectizeInput("countries",
                   "Select Country:",
                   choices = c("country 1",
                               "country 2",
                               "country 3"),
                   selected = "country 2",
                   multiple = TRUE
    )
  ),
  dashboardBody(plotlyOutput("top3countries"))) 

server <- function(input, output) {

  output$top3countries <- renderPlotly({

    plot_ly(dataset, 
            x = ~Year,
            y = ~GDP,
            color = ~`Country Name`,
            mode = 'lines+markers')
  })
}

shinyApp(ui=ui, server=server)
1
Please show a small reproducible example - akrun
Hi @akrun, I made the update. - Ulrich

1 Answers

0
votes

We can filter the dataset to make this happen

library(shiny)
library(plotly)
library(shinydashboard)
library(dplyr)

#Creating dataset
`Country Name`<- c("country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3",
                   "country 1", "country 2", "country 3")

Year <- c(2010, 2010, 2010,
          2011, 2011, 2011,
          2012, 2012, 2012,
          2013, 2013, 2013)

GDP <- c(2345, 2465, 3985,
         3453, 3748, 4847,
         5674, 4957, 5763,
         6475, 9387, 7564)

dataset <- data.frame(`Country Name`, Year, GDP, 
           check.names = FALSE, stringsAsFactors = FALSE)

-ui

ui <- dashboardPage(
  dashboardHeader(title = "Top 3 Countries"),
  dashboardSidebar(

    sliderInput(inputId = "Year",
                label = "Choose a timeframe",
                min = min(dataset$Year), 
                max = max(dataset$Year),
                value = c(min(dataset$Year),max(dataset$Year)), 
                ticks = TRUE,
                round = TRUE,
                step = 1),

    selectizeInput("countries",
                   "Select Country:",
                   choices = c("country 1",
                               "country 2",
                               "country 3"),
                   selected = "country 2",
                   multiple = TRUE
    )
  ),
  dashboardBody(plotlyOutput("top3countries"))) 

-server

server <- function(input, output) {



  output$top3countries <- renderPlotly({



    dataSub <- dataset %>%
                  filter(Year >= as.numeric(input$Year[1]), 
                         Year <= as.numeric(input$Year[2]), 
                         `Country Name` %in% input$countries)

    plot_ly(dataSub, 
            x = ~Year,
            y = ~GDP,
            color = ~`Country Name`,
            mode = 'lines+markers')
  })
}

-run

shinyApp(ui=ui, server=server)

-output

enter image description here