Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205793

Issue with Shiny Input

$
0
0

I'm trying to make an app that changes based on the political question and demographics selected. I'm running into an issue with how I call my inputs, I think. Here is a dput version of my data frame GSS2018

structure(list(`Gss year for this respondent` = c(2018, 2018), 
    `Race of respondent` = c("White", "White"), `What is rs race 1st mention` = structure(c(1L, 
    1L), .Label = c("White", "Black", "Hispanic"), class = "factor"), 
    `Respondents sex` = c("Male", "Female"), `Political party affiliation` = c("Not str republican", 
    "Ind,near dem"), `Age of respondent` = c("43", "74"), `Rs religious preference` = c("Protestant", 
    "Catholic"), `How often r attends religious services` = structure(c(6L, 
    3L), .Label = c("Never", "Lt once a year", "Once a year", 
    "Sevrl times a yr", "Once a month", "2-3x a month", "Nrly every week", 
    "Every week", "More thn once wk"), class = "factor"), `Region of interview` = c("New england", 
    "New england"), `Marital status` = c("Never married", "Separated"
    ), `Rs highest degree` = structure(3:4, .Label = c("Graduate", 
    "Bachelor", "Junior college", "High school", "Lt high school"
    ), class = "factor"), `Total family income` = structure(c(NA, 
    2L), .Label = c("Under $20000", "$20000 to 39999", "$40000 to 59999", 
    "$60000 to 74999", "$75000 to $89999", "$90000 to $109999", 
    "$110000 to 169999", "$170000 or over"), class = "factor"), 
    `Was r born in this country` = c("Yes", "Yes"), `Were rs parents born in this country` = c("Both in u.s", 
    "One parent"), `R has a moral opposition to abortion` = c("Morally opposed", 
    "It depends"), `Change abortion laws in r's state to make it easier or harder to get an abortion` = c("Make it harder", 
    "Stay the same as now"), `Abortion if woman wants for any reason` = c("No", 
    "Yes"), `Woman's health seriously endangered` = c("Yes", 
    "Yes"), `Pregnant as result of rape` = c("Yes", "Yes"), `Married--wants no more children` = c("No", 
    "Yes"), `Strong chance of serious defect` = c("Yes", "Yes"
    ), `Low income--cant afford more children` = c("No", "No"
    )), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))

My code (locally I fixed some issues like Not applicable and No answer and such):

ui <- fluidPage(
    titlePanel(title = h2("GSS Data and Politics", align = "center")),
    tabsetPanel(type = "tabs",
        tabPanel("Stacked Single Variable",
                 selectInput(inputId = "politics1",
                             label = "Select political question",
                             choices = colnames(GSS2018[,14:21]),
                             selected = "R has a moral opposition to abortion"),
                 selectInput(inputId = "dropdown1",
                             label = "Select X Variable",
                             choices = colnames(GSS2018[2:13]),
                             selected = NULL),
                 plotOutput(outputId = "plot1")
)))

server <- function(input, output, session) {
    observe({
        eventReactive(
            input$dropdown1,{input$politics1})
    GSS1 <- GSS2018       
    GSS2 <- GSS1 %>%
            group_by(get(input$dropdown1)) %>%
            count(get(input$politics1)) %>%
            mutate(Percent = round(n/sum(n), digits = 2))

        names(GSS2)[1] <- input$dropdown1
        names(GSS2)[2] <- input$politics1
    })

I think the issue might be here, in how I call the inputs with get(input$...):

output$plot1 <- renderPlot({
GSS2 %>%
    ggplot(aes(x = get(input$dropdown1),
               y = Percent,
               fill = get(input$politics1))) +
    geom_bar(stat = "identity", position = position_stack()) +
    coord_flip() +
    geom_text(aes(label = Percent), position = position_stack(vjust = 0.5),
              color="white", size=3.5)+
    theme(legend.position="bottom", legend.direction="horizontal")+
    labs(title = "Views on Abortion",
         fill = "")+
    xlab(input$dropdown1) +
    scale_fill_manual(values = c("#619CFF","#F8766D","#00BA38"))
})
}
shinyApp(ui, server)

When I run this same code independently with my own versions of input (input$dropdown 1 <- "", input$politics1 <- ""), the code works perfectly, but when I run this code in the app it gives me the error of Warning: Error in get: object 'Race of respondent' not found, where the Race of respondent is the input$dropdown1. What can I do to fix this?


Viewing all articles
Browse latest Browse all 205793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>