I am trying to create a Shiny dashboard where the user can select a county based on a dropdown. My X axis values are long strings and I want to wrap it so that it renders properly. When I don't use scale_x_discrete(labels = wrap_format(10))
it renders the dashboard without any errors. When I do use scale_x_discrete(labels = wrap_format(10))
it opens the dashboard with the following message:
Error in <-: replacement has length zero
Then when I select any county from the dropdown list it renders the dashboard correctly. In my selectInput
statement I tried varying the Selected=
option using NULL or County A or not including Selected
but it still gave me the same error.
Below is my code and sample data. Any insights would be much appreciated.
Code
library(dplyr)
library(ggplot2)
library(shiny)
library(scales)
library(bslib)
# Define UI for application that draws a histogram
ui <- page_sidebar(
title="commuters",
sidebar=sidebar(
selectInput("county1","County",choices = repex_1$homeco),
width = 300
),
card(
plotOutput("q15")
)
)
server <- function(input, output, session) {
selected <- reactive(repex_1 %>% filter(homeco == input$county1))
output$q15 <- renderPlot({
selected() %>%
ggplot(aes(x=Reason, y=perc,label = scales::percent(round(perc,digits=3)))) +
geom_bar(position="dodge2",stat="identity",width=0.5) +
geom_text(position = position_dodge(width = .9), vjust = -0.5, size = 3) +
labs(x = "Reason", y = "Percent",title="Reasons to Use Transit instead of Driving") +
theme(axis.text.y = element_text(size = 12), axis.title.y = element_text(size = 12, face =
"bold"),axis.title.x = element_text(size = 12, face = "bold")) +
theme(legend.title = element_text(size=12),legend.text=element_text(size=12)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
theme_bw() +
scale_x_discrete(labels = wrap_format(10))
}, res = 96)
}
# Run the application
shinyApp(ui = ui, server = server)
Sample Data
reprex_1 <- structure(list(homeco = c("County A", "County B", "County C",
"County A", "County B", "County C", "County A", "County B", "County C",
"County A", "County B", "County C", "County A", "County B", "County C",
"County A", "County B", "County C", "County A", "County B", "County C",
"County A", "County B", "County C", "County A", "County B", "County C"
), Reason = c("Transit stop is close to work and home", "Transit stop is close to work and home",
"Transit stop is close to work and home", "Cheaper than driving",
"Cheaper than driving", "Cheaper than driving", "Fits your schedule",
"Fits your schedule", "Fits your schedule", "Faster than driving",
"Faster than driving", "Faster than driving", "Reasonable in cost",
"Reasonable in cost", "Reasonable in cost", "Consistently on time",
"Consistently on time", "Consistently on time", "Avoids travel stress",
"Avoids travel stress", "Avoids travel stress", "Safer than driving",
"Safer than driving", "Safer than driving", "Better for the environment",
"Better for the environment", "Better for the environment"),
perc = c(0.161346249, 0.155637211, 0.123051117, 0.284589346,
0.23883723, 0.247860801, 0.253375207, 0.149919656, 0.124809683,
0.255869188, 0.422164466, 0, 0.193191535, 0.237246884, 0.124809683,
0.242922466, 0.450836235, 0, 0.196705713, 0.102326166, 0,
0.212404782, 0.082591416, 0.123051117, 0.264445847, 0.160410022,
0)), class = c("tbl_df", "tbl", "data.frame"),row.names = c(NA, -27L))