R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(shiny) library(tidyverse) library(plotly) library(DT) library(shinythemes)

Import Data

dat <- read_csv(“/mnt/data/3nwNdQZyT-S8DXUGcp_ktQ_f03ef50b3f2b4f30a44215b5acd7d4f8_cces_sample_coursera (1).csv”) dat <- dat %>% select(c(“pid7”, “ideo5”, “newsint”, “gender”, “educ”, “CC18_308a”, “region”)) dat <- drop_na(dat)

UI

ui <- navbarPage( theme = shinytheme(“superhero”), “Survey Analysis”,

# Page 1 tabPanel(“Page 1”, sidebarLayout( sidebarPanel( selectInput(“educ”, “Education Level:”, choices = c(“No high school”, “High school graduate”, “Some college”, “2-year”, “4-year”, “Post-grad”)) ), mainPanel( tabsetPanel( tabPanel(“Tab 1”, plotOutput(“plot1”)), tabPanel(“Tab 2”, plotOutput(“plot2”)) ) ) ) ),

# Page 2 tabPanel(“Page 2”, sidebarLayout( sidebarPanel( selectInput(“gender”, “Gender:”, choices = c(“Male”, “Female”)), selectInput(“region”, “Region:”, choices = c(“Northwest”, “Midwest”, “South”, “West”)) ), mainPanel( plotOutput(“plot3”) ) ) ),

# Page 3 tabPanel(“Page 3”, sidebarLayout( sidebarPanel( selectInput(“newsint”, “Interest in News:”, choices = c(“Most of the time”, “Some of the time”, “Only now and then”, “Hardly at all”)) ), mainPanel( dataTableOutput(“table1”, height = 500) ) ) ) )

Server

server <- function(input, output) {

# Page 1, Tab 1 output\(plot1 <- renderPlot({ ggplot(dat %>% filter(educ == input\)educ), aes(x = pid7)) + geom_bar() + labs(title = “Distribution of Party ID by Education Level”, x = “Party ID”, y = “Count”) })

# Page 1, Tab 2 output\(plot2 <- renderPlot({ ggplot(dat %>% filter(educ == input\)educ), aes(x = ideo5)) + geom_bar() + labs(title = “Distribution of Ideology by Education Level”, x = “Ideology”, y = “Count”) })

# Page 2 output\(plot3 <- renderPlot({ ggplot(dat %>% filter(gender == input\)gender & region == input$region), aes(x = CC18_308a)) + geom_bar() + labs(title = “Approval of President Trump by Gender and Region”, x = “Approval”, y = “Count”) })

# Page 3 output\(table1 <- renderDataTable({ dat %>% filter(newsint == input\)newsint) }) }

Run the application

shinyApp(ui = ui, server = server)