Show Sidebar Hide Sidebar

Shiny app using Plotly in R

Shiny app to capture plotly events on UN Data

Shiny app

Code

ui.r

library(shiny)
library(plotly)

shinyUI(fluidPage(

  # Application title
  titlePanel("Ideal Points"),

  sidebarPanel(
    h3("Ideal Points Estimation"),
    # Select Justices name here
    selectizeInput("name",
                   label = "Country Name(s) of Interest",
                   choices = unique(ideal$Name),
                   multiple = T,
                   options = list(maxItems = 5, placeholder = 'Select a name'),
                   selected = "United States of America"),
    # Term plot
    plotOutput("termPlot", height = 200),
    helpText("Data: Bailey, Michael, Anton  Strezhnev and Erik Voeten. Forthcoming.  'Estimating Dynamic State Preferences from United Nations Voting Data.' Journal of Conflict Resolution. ")
  ),

  # Show a plot of the generated distribution
  mainPanel(
    plotlyOutput("trendPlot")
  )
)
)


server.r

shinyServer(function(input, output, session) {

  output$trendPlot <- renderPlotly({

    if (length(input$name) == 0) {
      print("Please select at least one country")
    } else {
      df_trend <- ideal[ideal$Name == input$name, ]
      ggplot(df_trend) +
        geom_line(aes(x = Year, y = Ideal.point, by = Name, color = Name)) +
        labs(x = "Year", y = "Ideology", title = "Ideal Points for Countries") +
        scale_colour_hue("clarity", l = 70, c = 150) + ggthemes::theme_few()
    }

  })
})

global.r

ideal <- read.csv("Data/UN_IdealPoints.csv", stringsAsFactors = F)

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.