You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny仪表盘使用scale_x_discrete换行X轴标签时出现赋值长度为零错误

问题解决:Shiny仪表盘X轴换行启动报错问题

问题描述

开发Shiny仪表盘时,为让X轴长字符串标签换行显示,添加scale_x_discrete(labels = wrap_format(10))后,启动时触发报错Error in <-: replacement has length zero,但手动选择任意县后仪表盘可正常渲染。调整selectInput的selected参数(设为NULL、具体县名或省略)均无法解决该问题。

问题原因

启动时默认加载的County数据中存在perc=0的行,这些行对应的Reason标签在ggplot处理时,会导致wrap_format函数接收到空标签列表,从而触发赋值长度为0的错误。手动选择County后数据重新渲染,错误被规避。

解决方案

方案1:过滤掉perc为0的行(推荐)

在响应式数据中直接过滤掉perc=0的记录,确保ggplot只处理有意义的数据行,避免空标签问题:

# 修改server中的响应式数据部分
selected <- reactive(repex_1 %>% filter(homeco == input$county1, perc > 0))

方案2:自定义安全的换行标签函数

若需保留perc=0的行,可自定义包装函数,先检查标签是否为空再应用换行逻辑:

# 自定义安全换行函数
safe_wrap_format <- function(width) {
  function(x) {
    if (length(x) == 0) {
      return(character(0))
    }
    scales::wrap_format(width)(x)
  }
}

# 替换原scale_x_discrete代码
scale_x_discrete(labels = safe_wrap_format(10))

方案3:指定初始选中无0值的县

在selectInput中设置初始选中的County为不存在perc=0的县(如County A),仅适用于特定数据场景:

selectInput("county1","County",choices = unique(repex_1$homeco), selected = "County A")

修改后的完整代码(方案1示例)

library(dplyr)
library(ggplot2)
library(shiny)
library(scales)
library(bslib)

# 示例数据
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))

# 定义UI
ui <- page_sidebar(
 title="commuters",
  sidebar=sidebar(
   selectInput("county1","County",choices = unique(repex_1$homeco)),
width = 300
 ),
 card(
   plotOutput("q15")
  )
 )

# 定义Server
server <- function(input, output, session) {
 # 过滤掉perc为0的行
 selected <- reactive(repex_1 %>% filter(homeco == input$county1, perc > 0))
 
 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)
  }

# 启动应用
shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者Krishnan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 10:44:52