Create a shiny app that has a scatter plot based on income and population from 2 seperate CSV files

I have two different CSV files that have the same amount of rows and the same column headings. Although one is for population and the other is for income. I am trying to make a shiny app that lets the user select the country and it will update the scatter plot to show the population and income for each year. They have 194 rows and 252 columns so assigning them individually would take a good bit of time so wondering if there is an easier way of going about it?

incomeTotal = read.csv("Income.csv", header = T, sep = ",")
popTotal = read.csv("Population.csv", header = T, sep = ",")
fullData = merge(popTotal, incomeTotal)

UI:

ui <- dashboardPage(
dashboardHeader(title = "Country Income and Population"),

dashboardSidebar(
  sidebarMenu(
    
    selectInput("nation", "Select a Country: ", choices = sort(unique(fullData))
    )
  )
),

dashboardBody(
  
  fluidRow(
    box(width = 6, solidHeader = TRUE, status = "primary",
        title = "Total", 
        plotlyOutput("popIncome", width = '750px', height = '300px')
        
    )
  )
)
)

Server:

server <- function(input, output, session) {
  
  output$popIncome <- renderPlotly({
       
    ggplot(fullData, mapping = aes(x = popTotal, y = incomeTotal)) + geom_point()
    
  })
  
}

Thanks

If data are in same row order

full_data <- cbind(popTotal, incomeTotal)

then

... select(-the_dupe_columns) 

and rename

If you have a shared column, such as "city" you can do an inner join.

1 Like