Hello,
I am getting the following error when I try to run a line of code:
Error in shinyApp(ui = ui, server = server) : object 'server' not found
I am very new to programming to I apologize ahead of time for any simple mistakes
Here is all of my code:
At a high level, we want to create a scatter plot, where the user can select what is the x axis and the y axis of the scatter plot.
The size and color will be represented by the year attribute, and the user will be able to choose one of the following (for the x and the y axis):
new_sp_m014
new_sp_f014
new_sp_m65
new_sp_f65
library(shiny)
library(ggplot2)
library(tidyverse)
The file is: "https://intro-datascience.s3.us-east-2.amazonaws.com/who.csv" and store it in the tb dataframe
tb <- read.csv('https://intro-datascience.s3.us-east-2.amazonaws.com/who.csv')
glimpse(tb)
First, remove na's from iso2
tb = tb[!is.na(tb$iso2),]
Now create the dataframe 'tbCan', which is the iso2 for canada (CA)
tbCan = tb[tb$iso2=="CA",]
We will need the imputeTS package (only install if needed)
#install.packages('imputeTS')
library(imputeTS)
Now we can use 'na_interpolation for new_sp_m014
tbCan2 = tb[tb$iso2=="CA",]
tbCan$new_sp_m014 = na_interpolation(tbCan2$new_sp_m014)
Create the User Interface of our shiny app
Define the sidebarPanel, which is two choices (use 'selectInput'), one for the x axis of the scatter plot, and the other is the y axis for the scatter plot. (make sure to library shiny).
sidePanel <- sidebarPanel(
selectInput(inputId = "y", label = "y axis",
c("new_sp_m014", "new_sp_f014", "new_sp_m65", "new_sp_f65")))
Create the mainPanel to show the scatter plot.
mainPanel <- mainPanel(
plotOutput("plot")
)
Define UI for application (called ui)
Use a 'fluidPage' using the sidebarLayout, and your sidePanel and mainPanel
ui <- fluidPage(
# Application title
titlePanel("Exploring Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
#add the slider
sidePanel,
selectInput(inputId = "x",label = "x axis",
c("new_sp_m014", "new_sp_f014", "new_sp_m65", "new_sp_f65")
)),
# Show a plot of the generated distribution
mainPanel(
plotOutput('plot')
)
)
Now let's define the server
Use ggplot to render a scatter plot, using the tbCan dataframe, the input for the x-axis and the input for the y-axis.
Store the results in the 'server' variable
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(data = tbCan, aes_string(x=input$x, y=input$y)) + geom_point() + theme_bw()
})
}
Now run the shiny App
# Run the application
shinyApp(ui = ui, server = server)