Reputation: 29
I have plotted scatterplot for each country, and I am trying to add a correlation coefficient under the scatterplot, but I keep getting errors saying "Selections can't have missing values." even after using na.rm
Can someone help me with this??
I appreciate any help you can provide.
data link EuropeIndia
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(plotly)
library(DT)
library(tidyverse)
library(car)
library(ggpubr)
covid <- read.csv("EuropeIndia.csv")
title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
'COVID 19 Vaccinations')
# Define UI for application
ui <- fluidPage(
headerPanel(title = title),
# Application title
titlePanel("COVID vaccinations: Deaths Vs All variables"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("location", "1. Select a country",
choices = covid$location, selectize = TRUE, multiple = FALSE),
br(),
helpText("2. Select variables for scatterplot"),
selectInput(inputId = "y", label = "Y-axis:",
choices = c("total_deaths", "new_deaths"),
selected = "Deaths",),
br(),
selectInput(inputId = "x", label = "X-axis:",
choices = names(subset(covid,select = -c(total_deaths,new_deaths,
iso_code, continent,date,location), na.rm =TRUE)),
selectize = TRUE,
selected = "Comparator variables")
),
mainPanel(
textOutput("location"),
#plotOutput("Scatterplot"),
tabsetPanel(
type = "tabs",
tabPanel("Scatterplot", plotlyOutput("scatterplot"),
verbatimTextOutput("correlation"),
verbatimTextOutput("interpretation")),
tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
tabPanel("Dataset", DTOutput("dataset")))
)
)
)
# Define server logic
server <- function(input, output) {
output$location <- renderPrint({locationfilter <- subset(covid, covid$location == input$location)})
output$summary <- renderPrint({summary(covid)})
output$dataset <- renderDT(
covid, options = list(
pageLength = 50,
initComplete = JS('function(setting, json) { alert("done"); }')
)
)
output$scatterplot <- renderPlotly({
ggplotly(
ggplot(subset(covid, covid$location == input$location),
aes(y = .data[[input$y]], x = .data[[input$x]],col = factor(stringency_index)))+
geom_smooth()+geom_point()+labs(col ="Stringency Index")
)
})
output$correlation <- renderText({
x= subset(covid, covid$location == input$location) %>% dplyr::select(as.numeric(!!!input$x, na.rm =TRUE))
y= subset(covid, covid$location == input$location) %>% dplyr::select(as.numeric(!!!input$y, na.rm = TRUE))
var(x,y, na.rm = T, use)
cor(x,y, method = 'pearson', na.rm =T)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 284
Reputation: 91
First of all you should select just one Country from the selection list.
For error checking I propose you the next code.
library(shiny)
library(plotly)
library(DT)
library(tidyverse)
library(car)
library(ggpubr)
covid <- read.csv("EuropeIndia.csv")
title <- tags$a(href='https://ourworldindata.org/covid-vaccinations?country=OWID_WRL',
'COVID 19 Vaccinations')
# Define UI for application
ui <- fluidPage(
headerPanel(title = title),
# Application title
titlePanel("COVID vaccinations: Deaths Vs All variables"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("location", "1. Select a country",
choices = covid$location[1], selectize = TRUE, multiple = FALSE),
br(),
helpText("2. Select variables for scatterplot"),
selectInput(inputId = "y", label = "Y-axis:",
choices = c("total_deaths", "new_deaths"),
selected = "Deaths",),
br(),
selectInput(inputId = "x", label = "X-axis:",
choices = names(subset(covid,select = -c(total_deaths,new_deaths,
iso_code, continent,date,location), na.rm =TRUE)),
selectize = TRUE,
selected = "Comparator variables")
),
mainPanel(
textOutput("location"),
#plotOutput("Scatterplot"),
tabsetPanel(
type = "tabs",
tabPanel("Scatterplot", plotlyOutput("scatterplot"),
verbatimTextOutput("correlation"),
verbatimTextOutput("interpretation")),
tabPanel("Summary of COVID data", verbatimTextOutput("summary")),
tabPanel("Dataset", DTOutput("dataset")))
)
)
)
# Define server logic
server <- function(input, output) {
output$location <- renderPrint({locationfilter <- subset(covid, covid$location == input$location)})
output$summary <- renderPrint({summary(covid)})
output$dataset <- renderDT(
covid, options = list(
pageLength = 50,
initComplete = JS('function(setting, json) { alert("done"); }')
)
)
output$scatterplot <- renderPlotly({
ggplotly(
ggplot(subset(covid, covid$location == input$location),
aes(y = .data[[input$y]], x = .data[[input$x]],col = factor(stringency_index)))+
geom_smooth()+geom_point()+labs(col ="Stringency Index")
)
})
output$correlation <- renderText({
x <- covid[covid$location == input$location, input$x]
y <- covid[covid$location == input$location, input$y]
xy = data.frame(x,y)
xy = xy[complete.cases(xy),]
var(xy)
cor(xy,method = 'pearson')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Upvotes: 1