I'm working on an R Shiny App that plots monthly percent changes in political party registrations. I'm still pretty new to interactive web apps (and Stack Overflow), and just haven't been able to find answers to these quesions -- thanks/sorry in advance.
In an older version of the app (here) I was able to let the user select the region, and I had manually put % monthly changes directly in the original dataframe.
What I'm trying to do now is enable the app to:
- Allow the user to choose/input a specific political party, which are each stored as columns in my df
- Then have the app itself calculate and add a new column with % monthly change for the selected party & region, so I don't have to do that manually for each party in the original df.
I can't figure out how to let the user select by party name / df column. I've tried:
selectizeInput(inputId = 'Party', label= 'Party',
choices = colnames(df_2016)
but that doesn't work.
I also have no clue how to do 2 lol.
Thanks in advance for any help you can provide; would realy appreciate if anyone could point me in the right direction or toward resources to learn how to do this. The relevant files are here if needed.
Here's the code for my UI and Server:
UI:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(ggthemes)
library(shinythemes)
library(lubridate)
df_2016 = read.csv('df_2016.csv')
df_2020 = read.csv('df_2020.csv')
# Define UI for application
fluidPage(theme = shinytheme('cerulean'),
# Application title
titlePanel("NJ Voter Registration"),
sidebarLayout(
# Drop-down menu with region options
mainPanel(
selectizeInput(inputId = 'County', label= 'Region',
choices = df_2016$County),
),
mainPanel(
tabsetPanel(
tabPanel('Home',
"Data is sourced from the NJ Division of Elections Voter Registration Statistics Archive, which can be accessed at https://www.state.nj.us/state/elections/election-information-svrs.shtml",
"Please use the drop-down menu above to select whether to view statewide statistics, or data for a specific county.",
),
tabPanel('2016 Data',
'The dataframe for your selection is provided here.',
tableOutput('tableonesix')
),
tabPanel('2020 Data',
'The dataframe for your selection is provided here.',
tableOutput('tabletwozero')
)
)
)
)
)
Server:
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(ggthemes)
library(shinythemes)
library(lubridate)
df_2016 = read.csv('df_2016.csv')
df_2020 = read.csv('df_2020.csv')
function(input, output) {
output$tableonesix=renderTable(
df_2016 %>%
filter(County==input$County)
)
output$tabletwozero=renderTable(
df_2020 %>%
filter(County==input$County)
)
}
question from:
https://stackoverflow.com/questions/65852077/adding-and-selecting-dataframe-columns-in-r-shiny 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…