我正在尝试创建一个闪亮的仪表板,用户可以在其中根据下拉列表选择一个县。我的 X 轴值是长字符串,我想将其包装起来以便正确渲染。当我不使用
scale_x_discrete(labels = wrap_format(10))
时,它会渲染仪表板而不会出现任何错误。当我do使用scale_x_discrete(labels = wrap_format(10))
时,它会打开仪表板并显示以下消息:
错误于 <-: replacement has length zero
然后,当我从下拉列表中选择任何县时,它会正确呈现仪表板。在我的
selectInput
声明中,我尝试使用 NULL 或 County A 更改 Selected=
选项或不包括 Selected
但它仍然给了我同样的错误。
下面是我的代码和示例数据。任何见解将不胜感激。
代码
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)
样本数据
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))
你只需要
req()
。 试试这个
output$q15 <- renderPlot({
req(input$county1)
...
})