I have a shiny App, I wanted to be reactive for the inputs choice and show the datatable when I press the "Go" button.
For inputs I want to have the choice between "All value" of my variable and each value.
I have some problem to fix my app.
First try
library(shiny)
library(dplyr)
library(DT)
# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
YES = paste(BA, MA, sep = ""))
ui <- fluidPage(
titlePanel("BA"),
column(4,
uiOutput("filter1"),
uiOutput("filter2"),
uiOutput("filter3"),
actionButton("button", "GO")),
column(8,
DT::dataTableOutput("my_table"))
)
server <- function(input, output, session) {
All_BA <- reactive({
bdd %>% distinct(BA)
})
# my reactives inputs for filter 1
output$filter1 <- renderUI({
selectInput("filter1", "Filtre numéro 1",
choices = c("All_BA", bdd %>% select(BA)))
})
All_MA <- reactive({
bdd %>% filter(BA %in% input$filter1) %>%
distinct(MA)
})
# my reactives inputs for filter 2
output$filter2 <- renderUI({
selectInput("filter2", "Filtre numéro 2",
choices = c("All_MA", bdd %>% filter(BA %in% input$filter1) %>% select(MA)),
selected = "All_MA")
})
All_Y <- reactive({
bdd %>% filter(BA %in% input$filter1 |
MA %in% input$filter2) %>% distinct(YES)
})
# my reactives inputs for filter 3
output$filter3 <- renderUI({
selectInput("filter3", "Filtre numéro 3",
choices = c("All_Y", bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2) %>% select(YES)),
selected = "All_Y")
})
df <- eventReactive(input$button, {
bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2,
YES %in% input$filter3)
})
output$my_table <- DT::renderDataTable({
df()
})
}
# Run the application
shinyApp(ui = ui, server = server)
second try (didn't work cause of rectivity problem and the code doesnt seem to be optimized)
library(shiny)
library(dplyr)
library(DT)
# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
YES = paste(BA, MA, sep = ""))
ui <- fluidPage(
titlePanel("BA"),
column(4,
uiOutput("filter1"),
uiOutput("filter2"),
uiOutput("filter3"),
actionButton("button", "GO")),
column(8,
DT::dataTableOutput("my_table"))
)
server <- function(input, output, session) {
All_BA <- reactive({
bdd %>% distinct(BA)
})
# my reactives inputs for filter 1
if(input$filter1 == "All_BA"){
bdd <- reactive({
bdd %>%
filter(BA %in% All_BA())
})
}else{
bdd <- reactive({
bdd %>%
filter(BA %in% input$filter1)
})
}
output$filter1 <- renderUI({
selectInput("filter1", "Filtre numéro 1",
choices = c("All_BA", bdd() %>% select(BA)))
})
All_MA <- reactive({
bdd() %>% filter(BA %in% input$filter1) %>%
distinct(MA)
})
# my reactives inputs for filter 2
if(input$filter2 == "All_MA"){
bdd2 <- reactive({
bdd() %>%
filter(MA %in% All_MA())
})
}else{
bdd2 <- reactive({
bdd() %>%
filter(MA %in% input$filter2)
})
}
output$filter2 <- renderUI({
selectInput("filter2", "Filtre numéro 2",
choices = c("All_MA", bdd2() %>% select(MA)),
selected = "All_MA")
})
All_Y <- reactive({
bdd2 %>% filter(BA %in% input$filter1 |
MA %in% input$filter2) %>% distinct(YES)
})
# my reactives inputs for filter 3
if(input$filter3 == "All_Y"){
bdd3 <- reactive({
bdd2() %>%
filter(YES %in% All_Y())
})
}else{
bdd3 <- reactive({
bdd2() %>%
filter(YES %in% input$filter3)
})
}
output$filter3 <- renderUI({
selectInput("filter3", "Filtre numéro 3",
choices = c("All_Y", bdd3() %>% select(YES)),
selected = "All_Y")
})
df <- eventReactive(input$button, {
bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2,
YES %in% input$filter3)
})
output$my_table <- DT::renderDataTable({
df()
})
}
# Run the application
shinyApp(ui = ui, server = server)
The problem lies in the filtering of the table.
If nothing is selected input$filter1 has value 'All_BA', and the filter return no value, and similarly for the other inputs.
In fact the filter works if all 3 input values are selected.
Change it to:
df <- eventReactive(input$button, {
res <- bdd
if(input$filter1 != "All_BA")
res <- res %>% filter(BA %in% input$filter1)
if(input$filter2 != "All_MA")
res <- res %>% filter(MA %in% input$filter2)
if(input$filter3 != "All_Y")
res <- res %>% filter(MA %in% input$filter3)
res
})
(I worked on the first example).
Hope this helps
Related
I show you my shiny application, but I have a problem, I cannot update the selectimput, I have used updateSelectInput but it does not work.
I have two selectInputs inside a tabsetPanel, since I need to update the table with two filters, one is the category and the other the subcategory.
here my code.
library(shiny)
library(tidyverse)
library(DT)
cat1<-rep("LINEA BLANCA", 75)
cat2<- rep("VIDEO", 75)
subcat1<-rep("LAVADORAS", 40)
subcat2<- rep("REFRIS", 35)
subcat3<- rep("TV", 40)
subcat4<- rep("SONIDO", 35)
vent<-sample(100:900, 150, replace=T)
segm1<-rep("AAA", 25)
segm2<-rep("BBB", 25)
segm3<-rep("CCC", 25)
segm4<-rep("ABB", 25)
segm5<-rep("ACC", 25)
segm6<-rep("BAC", 25)
db<- tibble(segment=c(segm1,segm2,segm3,segm4,segm5,
segm1),CATEGORIA=c(cat1,cat2), SUBCAT=c(subcat1,subcat2, subcat3, subcat4), vent=vent)
ui <- fluidPage(
# App title
titlePanel("EXAMPLE"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Ana_inv", uiOutput("selectcat"), uiOutput("selectsubcat"),DT::dataTableOutput("ana_inv")),
#tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
)
server <- function(input, output, session) {
output$selectcat <- renderUI({
selectInput("Cat", "Seleccione Categoria", choices = c("ALL",as.vector(db$CATEGORIA)))
})
output$selectsubcat <- renderUI({
#opciones<- db_prueba %>% filter(CATEGORIA==input$CAT)
selectInput("Subcat", "Seleccione Subcategoria", choices = c("ALL",as.vector(db$SUBCAT)))
})
activar<- reactive({
req(input$Cat)
req(input$Subcat)
opciones<- db %>% filter(CATEGORIA==input$Cat)
if(input$Cat == "TODOS") {
filt1 <- quote(CATEGORIA != "#?><")
} else {
filt1 <- quote(CATEGORIA == input$Cat)
}
if (input$Subcat == "TODOS") {
filt2 <- quote(SUBCAT != "#?><")
} else {
filt2 <- quote(SUBCAT == input$Subcat)
}
db %>%
filter_(filt1) %>%
filter_(filt2) %>% group_by(segment)%>%
summarise(SKUs=n(),
vta=sum(vent))
})
# Return the formula text for printing as a caption ----
output$ana_inv <- DT::renderDataTable({
activar()
})
}
shinyApp(ui = ui, server = server)
So I need that if the category "LINEA BLANCA" is selected in the subcategory it only shows "REFRIS" and "LAVADORAS", but also if someone selects "ALL" in the category he can also select each subcategory, that is, it can be filtered by subcategory assuming I only want to see subcategories.
I have tried many ways but none works, any ideas? you can run the application in R to get an idea of what I want.
Try this
server <- function(input, output, session) {
output$selectcat <- renderUI({
selectInput("Cat", "Seleccione Categoria", choices = c("ALL",as.vector(db$CATEGORIA)))
})
output$selectsubcat <- renderUI({
req(input$Cat)
if (input$Cat=="ALL"){ df <- db
}else df <- db %>% filter(CATEGORIA %in% input$Cat)
selectInput("Subcat", "Seleccione Subcategoria", choices = c("ALL",as.vector(df$SUBCAT)))
})
activar<- reactive({
req(input$Cat,input$Subcat)
if (input$Cat=="ALL"){ df <- db
}else df <- db %>% filter(CATEGORIA %in% input$Cat)
if (input$Subcat=="ALL"){ df <- df
}else df <- df %>% filter(SUBCAT == input$Subcat)
df %>%
group_by(segment) %>%
summarise(SKUs=n(),
vta=sum(vent))
})
# Return the formula text for printing as a caption ----
output$ana_inv <- DT::renderDataTable({
activar()
})
}
I'd like to create a dynamic and hierarchical sidebarLayout using the selectInput
variables information. I have a pet information data frame (myds) and for example, I choose dog option in "selectedvariable1" pet, then in "selectedvariable3" the options need to be "collie" or "pit-bull", not "birman" or "bobtail" because the option in "selectedvariable1"is a dog, not a cat.
In my example:
# Packages
library(shiny)
# Create my data set
pet<-c("dog","dog","cat","cat")
fur<-c("long","short","long","short")
race<-c("collie","pit-bull","birman","bobtail")
sweetness<-c("high","medium","high","medium")
myds<-data.frame(pet,fur,race,sweetness)
# Create the pet shiny dash
ui <- fluidPage(
titlePanel(title="My Pet Dashboard"),
sidebarLayout(
sidebarPanel(
uiOutput("selectedvariable1"),
uiOutput("selectedvariable2"),
uiOutput("selectedvariable3"),
uiOutput("selectedvariable4")
),
mainPanel(
textOutput("idSaida")
)
)
)
server <- function(input, output,session){
currentvariable1 <- reactive({input$selectedvariable1})
currentvariable2 <- reactive({input$selectedvariable2})
currentvariable3 <- reactive({input$selectedvariable3})
currentvariable4 <- reactive({input$selectedvariable4})
output$selectedvariable1 <- renderUI({
selectInput("selectedvariable1",
label = "Pet type",
choices = unique(myds$pet),
selected = TRUE )
})
data2 <- reactive({
data2 <- subset(myds, fur %in% unique(currentvariable2()))
})
output$selectedvariable2 <- renderUI({
data2 <- subset(myds, pet %in% unique(currentvariable1()))
selectInput("selectedvariable2",
label = "Fur style",
choices = unique(data2$fur),
selected = TRUE )
})
data3 <- reactive({
data3 <- subset(data2, fur %in% unique(currentvariable2()))
})
output$selectedvariable3 <- renderUI({
selectInput("selectedvariable3",
label = "Race name",
choices = unique(data3$race),
selected = TRUE )
})
data4 <- reactive({
data4 <- subset(data2, fur %in% unique(currentvariable3()))
})
output$selectedvariable4 <- renderUI({
selectInput("selectedvariable4",
label = "Sweetness behaviour",
choices = unique(data4$sweetness),
selected = TRUE )
})
}
shinyApp(ui, server)
##
Please, anyone can help me with this question?
Try this
server <- function(input, output,session){
# currentvariable1 <- reactive({input$selectedvariable1})
# currentvariable2 <- reactive({input$selectedvariable2})
# currentvariable3 <- reactive({input$selectedvariable3})
# currentvariable4 <- reactive({input$selectedvariable4})
output$selectedvariable1 <- renderUI({
selectInput("selectedvariable1",
label = "Pet type",
choices = unique(myds$pet),
selected = TRUE )
})
data2 <- reactive({
req(input$selectedvariable1)
data2 <- subset(myds, pet %in% input$selectedvariable1)
})
output$selectedvariable2 <- renderUI({
req(data2())
#data2 <- subset(data2(), pet %in% unique(currentvariable1()))
selectInput("selectedvariable2",
label = "Fur style",
choices = unique(data2()$fur),
selected = TRUE )
})
data3 <- reactive({
req(input$selectedvariable2,data2())
data3 <- subset(data2(), fur %in% input$selectedvariable2)
})
output$selectedvariable3 <- renderUI({
req(data2())
selectInput("selectedvariable3",
label = "Race name",
choices = unique(data2()$race), ## use data3() instead of data2(), if you wish to subset from data3()
selected = TRUE )
})
data4 <- reactive({
req(input$selectedvariable3,data2())
data4 <- subset(data2(), race %in% input$selectedvariable3)
})
output$selectedvariable4 <- renderUI({
req(data4())
selectInput("selectedvariable4",
label = "Sweetness behaviour",
choices = data4()$sweetness,
selected = TRUE )
})
}
shinyApp(ui, server)
Something like this should work, add this to server function and adapt to your code:
observeEvent(input$selectedvariable1,{
if (input$selectedvariable1=="dog") {
updateSelectInput("selectedvariable3", choices=c("collie","pit-bull"))
}
})
I have three selectInputs, and I would like the selection in the first one (Continent) to change the possible selections in the second one (Country) and third one (State). So, for example, if someone choose "B" in the first input box, then can choose only "A" in the second box and "BB" in the last box.
And at the moment it is possible to select all the names for the box State.
code:
library(shiny)
library(readxl)
library(shinydashboard)
library(dplyr)
library(DT)
df <- data.frame(Continent = c("A","A","B","C"),
Country = rep("A",4),
State = c("AA","AA","BB","BB"),
Population = round(rnorm(4,100,2)),stringsAsFactors = FALSE)
is.not.null <- function(x) !is.null(x)
ui <- fluidPage(
titlePanel("TEST"),
sidebarLayout(
sidebarPanel( width = 3,
uiOutput("continent"),
uiOutput("country"),
uiOutput("state")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", DT::dataTableOutput("table_subset"))
)
)
)
)
ui = dashboardPage(
header,
sidebar,
body
)
################################################
server = shinyServer(function(input,output){
data <- df
output$table <- DT::renderDataTable({
if(is.null(data)){return()}
DT::datatable(data, options = list(scrollX = T))
})
output$continent <- renderUI({
selectInput(inputId = "Continent", "Select Continent",choices = var_continent(), multiple = F)
})
output$country <- renderUI({
selectInput(inputId = "Country", "Select Country",choices = var_country(), multiple = T)
})
output$state <- renderUI({
selectInput(inputId = "State", "Select State",choices = var_state(), multiple = T)
})
var_continent <- reactive({
file1 <- data
if(is.null(data)){return()}
as.list(unique(file1$Continent))
})
continent_function <- reactive({
file1 <- data
continent <- input$Continent
continent <<- input$Continent
if (is.null(continent)){
return(file1)
} else {
file2 <- file1 %>%
filter(Continent %in% continent)
return (file2)
}
})
var_country <- reactive({
file1 <- continent_function()
continent <- input$Continent
file2 <- data
if(is.null(continent)){
as.list(unique(file2$Country))
} else {
as.list(unique(file1$Country))
}
})
country_function <- reactive({
file1 <- data
country <- input$Country
country <<- input$Country
if (is.null(country)){
return(file1)
} else {
file2 <- file1 %>%
filter(Country %in% country)
return (file2)
}
})
var_state <- reactive({
file1 <- country_function()
country <- input$Country
file2 <- data
if(is.null(country)){
as.list(unique(file2$State))
} else {
as.list(unique(file1$State))
}
})
state_function <- reactive({
file1 <- data
state <- input$State
state <<- input$State
if (is.null(state)){
return(file1)
} else {
file2 <- file1 %>%
filter(State %in% state)
return (file2)
}
})
df <- reactive({
file1 <- data
continent <- input$Continent
country <- input$Country
state <- input$State
if (is.null(continent) & is.not.null(country) & is.not.null(state)){
file2 <- file1 %>%
filter(Country %in% country, State %in% state)
} else if (is.null(country) & is.not.null(continent) & is.not.null(state)){
file2 <- file1 %>%
filter(State %in% state, Continent %in% continent)
} else if (is.null(state) & is.not.null(country) & is.not.null(continent)){
file2 <- file1 %>%
filter(Country %in% country, Continent %in% continent)
} else if (is.null(continent) & is.null(country) & is.not.null(state)){
file2 <- file1 %>%
filter(State %in% state)
} else if (is.null(continent) & is.null(state) & is.not.null(country)){
file2 <- file1 %>%
filter(Country %in% country)
} else if (is.null(country) & is.null(state) & is.not.null(continent)){
file2 <- file1 %>%
filter(Continent %in% continent)
} else {
file2 <- file1 %>%
filter(Country %in% country, State %in% state, Continent %in% continent)
}
file2
})
output$table_subset <- DT::renderDataTable({
DT::datatable(df(), options = list(scrollX = T))
})
})
shinyApp(ui, server)
Maybe this is what you are looking for. In my opinion your approach is overly complicated. Therefore I reduced the code considerably. Besides the outputs there are now basically three parts in the server:
A reactive which filters the dataset
Three reactives to get the selected values
Three reactives to get the availabe choices depending on the other inputs. The available choices for Country is the list of countries after filtering for continent, the avialbel choices for States the list of states after filtering by Continent and Country
Reproducible code:
library(shiny)
library(shinydashboard)
library(dplyr)
library(DT)
df <- data.frame(Continent = c("A","A","B","C"),
Country = rep("A",4),
State = c("AA","AA","BB","BB"),
Population = round(rnorm(4,100,2)),stringsAsFactors = FALSE)
is.not.null <- function(x) !is.null(x)
ui <- fluidPage(
titlePanel("TEST"),
sidebarLayout(
sidebarPanel( width = 3,
uiOutput("continent"),
uiOutput("country"),
uiOutput("state")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", DT::dataTableOutput("table_subset"))
)
)
)
)
# ui = dashboardPage(
# header,
# sidebar,
# body
# )
################################################
server = shinyServer(function(input,output){
data <- df
output$table <- DT::renderDataTable({
if(is.null(data)){return()}
DT::datatable(data, options = list(scrollX = T))
})
output$continent <- renderUI({
selectInput(inputId = "Continent", "Select Continent",choices = var_continent(), multiple = F)
})
output$country <- renderUI({
selectInput(inputId = "Country", "Select Country",choices = var_country(), multiple = T)
})
output$state <- renderUI({
selectInput(inputId = "State", "Select State",choices = var_state(), multiple = T)
})
# Filtered data
data_filtered <- reactive({
filter(df, Continent %in% continent(), Country %in% country(), State %in% state())
})
# Get filters from inputs
continent <- reactive({
if (is.null(input$Continent)) unique(df$Continent) else input$Continent
})
country <- reactive({
if (is.null(input$Country)) unique(df$Country) else input$Country
})
state <- reactive({
if (is.null(input$State)) unique(df$State) else input$State
})
# Get available categories
var_continent <- reactive({
file1 <- data
if(is.null(data)){return()}
as.list(unique(file1$Continent))
})
var_country <- reactive({
filter(data, Continent %in% continent()) %>%
pull(Country) %>%
unique()
})
var_state <- reactive({
filter(data, Continent %in% continent(), Country %in% country()) %>%
pull(State) %>%
unique()
})
output$table_subset <- DT::renderDataTable({
DT::datatable(data_filtered(), options = list(scrollX = T))
})
})
shinyApp(ui, server)
\
I'm a really beginner in R Shiny.
I have a similar problem as at the link below.
multiple group_by in shiny app
Instead of making a table which worked out/I managed by following the instructions in the link above.
I would like to make a plot, preferably with hchart. In which i would to switch the information because of the group by. The difficult part / or the thing that doesn't work is putting the group_by on the x-axis.
## hier de tabel versie
df2 <- readRDS("Data.rds")
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
titlePanel("Dashboard"),
sidebarLayout(
sidebarPanel(
uiOutput("groups")
),
mainPanel(
DT::dataTableOutput("summary")
)
)
)
server <- function(input, output) {
mydata <- reactive({
data <- df2
data
})
output$groups <- renderUI({
df <- mydata()
selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
})
summary_data <- reactive({
req(input$grouper)
mydata() %>%
dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
dplyr::summarise(aantal = n()) %>%
dplyr::arrange(desc(aantal))
})
output$summary <- DT::renderDataTable({
DT::datatable(summary_data())
})
}
shinyApp(ui, server)
The above code works, but i tried to make a plot like this:
df2 <- readRDS("Data.rds")
library(shiny)
library(highcharter)
library(dplyr)
ui <- fluidPage(
titlePanel("Dashboard"),
sidebarLayout(
sidebarPanel(
uiOutput("groups")
),
mainPanel(
highchartOutput("plotje")
)
)
)
server <- function(input, output) {
mydata <- reactive({
data <- df2
data
})
output$groups <- renderUI({
df <- mydata()
selectInput(inputId = "grouper", label = "Group variable", choices = c("L","Lt","Lp"), selected = "L")
})
summary_data <- reactive({
req(input$grouper)
mydata() %>%
dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
dplyr::summarise(aantal = n()) %>%
dplyr::arrange(desc(aantal))
})
output$plotje <- renderHighchart({
data <- summary_data()
hchart(data, "column", hcaes(x = "grouper" , y = aantal)) # --> de plot zelf komt in het output deel van de UI
})
}
shinyApp(ui, server)
Could someone help me out?!
Thanks in advance!
Kind regards,
Steffie
You have the grouper column in the input$grouper var.
It's just a matter of unquoting it.
The line hchart(data, "column", hcaes(x = "grouper" , y = aantal)) should be:
hchart(data, "column", hcaes(x = !!input$grouper , y = aantal))
Full example (with iris data as you didn't provide an example of your own data):
library(shiny)
library(DT)
library(highcharter)
library(dplyr)
ui <- fluidPage(titlePanel("Dashboard"),
sidebarLayout(
sidebarPanel(uiOutput("groups")),
mainPanel(DT::dataTableOutput("summary"),
highchartOutput("plot"))
))
server <- function(input, output) {
mydata <- reactive({
iris
})
output$groups <- renderUI({
df <- mydata()
selectInput(
inputId = "grouper",
label = "Group variable",
choices = c("Petal.Length", "Species"),
selected = "Species"
)
})
summary_data <- reactive({
req(input$grouper)
mydata() %>%
dplyr::group_by(!!!rlang::syms(input$grouper)) %>%
dplyr::summarise(aantal = n()) %>%
dplyr::arrange(desc(aantal))
})
output$summary <- DT::renderDataTable({
DT::datatable(summary_data())
})
output$plot <- renderHighchart({
req(input$grouper)
data <- summary_data()
hchart(data, "column", hcaes(x = !!input$grouper, y = aantal))
})
}
shinyApp(ui, server)
I'm creating a shiny app where a user can select a state parent_location and a county name from two drop downs. They can also select a variable of interest layer which will then produce a summary statistics table. I've got my code working up to this point.
What I need to do is select other similar counties (contained in the cluster column) and then display summary statistics for this county as well. I can't seem to figure out how to A) display multiple summary statistics tables and B) dynamically create a list of similar counties.
Code that works
library(shiny)
library(tidyverse)
library(lubridate)
eviction_county_2010 <- read.csv("./eviction_county_2010.csv")
ui <- fluidPage(
sliderInput(inputId = "year",
label = "Select a Year:",
min = 2010,
max = 2016,
value = 2010,
step = 1),
radioButtons(inputId = "layer",
label = "Select a Dataset to View:",
choices = c("Eviction Filing Rate"="eviction_filing_rate", "Percent Rent Burden"="rent_burden",
"Percent Renter Occupied"="pct_renter_occupied", "Poverty Rate"="poverty_rate")),
selectInput(inputId = "state",
label = "Select a State:",
eviction_county_2010$parent_location),
selectInput(inputId = "county",
label = "Select a County:",
choices = NULL),
mainPanel(
h2("Summary of the variable"),
verbatimTextOutput("sum")
)
)
server <- function(input, output, session) {
observe({
x <- filter(eviction_county_2010,parent_location == input$state) %>%
select(name)
updateSelectInput(session,"county","Select a County:",choices = unique(x))}
)
output$sum <- renderPrint({
ec <- eviction_county_2010 %>%
filter(parent_location == input$state) %>%
filter(name == input$county)
summary(ec[,input$layer])
})
}
# Run the application
shinyApp(ui = ui, server = server)
Code I've attempted for displaying for similar counties. It returns Error in .getReactiveEnvironment()$currentContext(): Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) I'm not sure which part needs to be placed inside a reactive expression.
ui <- fluidPage(
sliderInput(inputId = "year",
label = "Select a Year:",
min = 2010,
max = 2016,
value = 2010,
step = 1),
radioButtons(inputId = "layer",
label = "Select a Dataset to View:",
choices = c("Eviction Filing Rate"="eviction_filing_rate", "Percent Rent Burden"="rent_burden",
"Percent Renter Occupied"="pct_renter_occupied", "Poverty Rate"="poverty_rate")),
selectInput(inputId = "state",
label = "Select a State:",
eviction_county_2010$parent_location),
selectInput(inputId = "county",
label = "Select a County:",
choices = NULL),
mainPanel(
h2("Summary of the variable"),
verbatimTextOutput("sum")
)
)
server <- function(input, output, session) {
ec <- eviction_county_2010 %>%
filter(parent_location == input$state) %>%
filter(name == input$county)
sel_clust <- unique(ec$cluster)
sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
sim_cty <- unique(sim_cty$GEOID)
sim_cty <- append(sim_cty, unique(ec$GEOID))
observe({
x <- filter(eviction_county_2010,parent_location == input$state) %>%
select(name)
updateSelectInput(session,"county","Select a County:",choices = unique(x))}
)
output$sum <- renderPrint({
df1 <- eviction_county_2010 %>%
filter(GEOID == sim_cty[1])
df2 <- eviction_county_2010 %>%
filter(GEOID == sim_cty[2])
df3 <- eviction_county_2010 %>%
filter(GEOID == sim_cty[3])
df4 <- eviction_county_2010 %>%
filter(GEOID == sim_cty[4])
df5 <- eviction_county_2010 %>%
filter(GEOID == sim_cty[5])
summary(df1[,input$layer])
summary(df2[,input$layer])
summary(df3[,input$layer])
summary(df4[,input$layer])
summary(df5[,input$layer])
})
}
# Run the application
shinyApp(ui = ui, server = server)
Is this even possible? What am I doing wrong here?
Move this section:
ec <- eviction_county_2010 %>%
filter(parent_location == input$state) %>%
filter(name == input$county)
sel_clust <- unique(ec$cluster)
sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
sim_cty <- unique(sim_cty$GEOID)
sim_cty <- append(sim_cty, unique(ec$GEOID))
To a reactive({}) statement. I think that's where your error is.
For example:
ec <- reactive({
eviction_county_2010 %>%
filter(parent_location == input$state) %>%
filter(name == input$county)
sel_clust <- unique(ec$cluster)
sim_cty <- eviction_county_2010[ sample(which(eviction_county_2010$cluster == sel_clust), 4),]
sim_cty <- unique(sim_cty$GEOID)
sim_cty <- append(sim_cty, unique(ec$GEOID))
})
Then later in your sever code use:
ec() %>%
...stuff...