【发布时间】:2016-01-22 13:19:08
【问题描述】:
我正在尝试构建我的第一个闪亮的应用程序,让用户可以探索哥本哈根不同类型的停车违规行为。但是我很难根据用户输入对要绘制的数据进行子集化 - 基本上我正在尝试创建一个交互式版本的下图,用户可以在其中选择 type_1、type_2 等。
#example data
df <- data.frame(c('a', 'b', 'c', 'd', 'e', 'f', 'g'), c(rep_len(55, 5), 0, 44),c(rep_len(12, 5), 6, 6), c(0, 2, 0, 3, 0, 7, 8), c(0, 0, 4, 0, 8, 5, 3), c(5, 0, 6, 0, 11,12, 9))
names(df) <- c("street", "type_1", "type_2", "type_3", "type_4", "type_5")
#Example of static plot that i'm trying to make interactive
ggplot(subset(df, type_5 > 5))+geom_bar(aes(x=reorder(street,type_5), y=type_5),stat = "identity")+coord_flip()+xlab("Streetname")
我的ui.R和server.R如下。我的原始数据有超过 2000 个街道名称,这就是为什么我试图对数据进行子集化以仅包括 type_x > 一些门槛的停车违规行为
# Define the overall UI
ui <- shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Parking violations"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("type", "Type of violation:",
choices=colnames(df), selected = "type_1" ),
hr(),
helpText("Data from Parking Copenhagen")
),
# Create a spot for the barplot
mainPanel(
plotOutput("pplot")
)
)
)
)
# Define a server for the Shiny app
server <- shinyServer(function(input, output) {
#Subset the data based on user input
p.df <- reactive({
subset(df, input$type > 5)
})
# Fill in the spot we created for a plot
output$pplot <- renderPlot({
pdat.df <- p.df()
# Render a barplot
p <- ggplot(pdat.df, aes(x=pdat.df$street, y=input$type)) + geom_bar(stat = "identity")+coord_flip()
print(p)
})
})
shinyApp(ui=ui, server = server)
下面的结果不是我想要的。我已经尝试过shiny not displaying my ggplot as I'd expect. 此处建议的选项,但在ggplot()(如我的静态示例中)或geom_bar() 中的任何一个子集都没有运气
非常感谢任何建议。谢谢!
【问题讨论】:
-
我无法测试您的代码,但可能与“input$type > 5”和“choices=colnames(df), selected = "type_1"" 有关,type_1 是一个因素吗?因为你过滤了一个数字。
-
type_1-5 是数字,所以我认为这不是问题?
-
或者在你的响应式()表达式中 df 5); df
-
@MLavoie 不幸的是没有改变任何东西
-
在作为答案发布之前尝试替换为 p