Shiny: Error in <-: replacement has length zero when using wrapping X Axis Label

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))

You just need req(). Try this

output$q15 <- renderPlot({
    req(input$county1)
...
})

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật