R 动态过滤器和反应式绘图
输入和打印输出之间的问题 嗨 我正在测试一个基本的ShinyApp,在这里我可以生成按地理位置和服务类型细分的商业服务图 我的想法是,我希望用户使用三个下拉菜单输入,每个输入取决于之前的选择,以子集数据,然后在ggplot中获得输出 但是,我在将输入连接到绘图输出时遇到问题(见下文)。选择时,输入工作正常且反应灵敏,但我无法确定如何将其链接到绘图,我感觉我没有使用正确的数据源(但不知道如何确保)。此外,我不知道如何添加第三个过滤器(用于“服务”),因为我不知道如何首先链接我的数据源 抱歉,这可能很简单,但如果您能提供一些帮助,我们将不胜感激 UIR 动态过滤器和反应式绘图,r,ggplot2,drop-down-menu,shiny,R,Ggplot2,Drop Down Menu,Shiny,输入和打印输出之间的问题 嗨 我正在测试一个基本的ShinyApp,在这里我可以生成按地理位置和服务类型细分的商业服务图 我的想法是,我希望用户使用三个下拉菜单输入,每个输入取决于之前的选择,以子集数据,然后在ggplot中获得输出 但是,我在将输入连接到绘图输出时遇到问题(见下文)。选择时,输入工作正常且反应灵敏,但我无法确定如何将其链接到绘图,我感觉我没有使用正确的数据源(但不知道如何确保)。此外,我不知道如何添加第三个过滤器(用于“服务”),因为我不知道如何首先链接我的数据源 抱歉,这可能
#Data
Test <- dataframe(
Geography1 = c("Region","Local Authority","County"...),
Geography2 = c("North West","Aldershot","Cheshire"...),
Service = c("Shop","Cafe","Library"...),
Overall_rating = c("Awesome","Good","Fantatstic"...),
Locations = c(4000, 1300, 1700...)
)
#SHINY APP
ui <- fluidPage(
titlePanel("Tool"),
sidebarLayout(
sidebarPanel(
uiOutput("geography1"),
uiOutput("geography2"),
uiOutput("service")),
mainPanel(
plotOutput("plot", height = "400px"))
)
)
#数据
测试很难判断应用程序中应该如何过滤提供的数据,但此代码至少会运行并具有交互性。希望从这里您可以了解如何调整数据集
#Data
Test <- data.frame(
Geography1 = c("Region","Local Authority","County"),
Geography2 = c("North West","Aldershot","Cheshire"),
Service = c("Shop","Cafe","Library"),
Overall_rating = c("Awesome","Good","Fantatstic"),
Locations = c(4000, 1300, 1700)
)
#SHINY APP
ui <- fluidPage(
titlePanel("Tool"),
sidebarLayout(
sidebarPanel(
uiOutput("geography1"),
uiOutput("geography2"),
uiOutput("service")),
mainPanel(
plotOutput("plot", height = "400px"))
)
)
server <- function(input, output) {
output$geography1 = renderUI({
selectInput(inputId = "geog1",
label = "Geography 1:",
choices = as.character(unique(Test$Geography1)),
selected = "Region")
})
datasub <- reactive({
Test[Test$Geography1 == input$geog1,]
})
output$geography2 = renderUI({
selectInput(inputId = "geog2",
label = "Geography2:",
choices = unique(datasub()[,"Geography2"]),
selected = unique(datasub()[,"Geography2"])[1])
})
datasub2 <- reactive({
datasub()[Test$Geography2 == input$geog2, ]
})
output$service = renderUI({
selectInput(inputId = "service",
label = "Service type:",
choices = unique(datasub2()[,"Service"]),
selected = unique(datasub2()[,"Service"])[1])
})
datasub3 <- reactive({
datasub()[Test$Service == input$service, ]
})
output$plot = renderPlot({
ggplot(datasub3(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
geom_bar(stat = "identity")
})
}
shinyApp(ui, server)
正如BigDataScientist所说,一个错误是您没有使用反应式数据集
#Data
Test <- data.frame(
Geography1 = c("Region","Local Authority","County"),
Geography2 = c("North West","Aldershot","Cheshire"),
Service = c("Shop","Cafe","Library"),
Overall_rating = c("Awesome","Good","Fantatstic"),
Locations = c(4000, 1300, 1700)
)
#SHINY APP
ui <- fluidPage(
titlePanel("Tool"),
sidebarLayout(
sidebarPanel(
uiOutput("geography1"),
uiOutput("geography2"),
uiOutput("service")),
mainPanel(
plotOutput("plot", height = "400px"))
)
)
server <- function(input, output) {
output$geography1 = renderUI({
selectInput(inputId = "geog1",
label = "Geography 1:",
choices = as.character(unique(Test$Geography1)),
selected = "Region")
})
datasub <- reactive({
Test[Test$Geography1 == input$geog1,]
})
output$geography2 = renderUI({
selectInput(inputId = "geog2",
label = "Geography2:",
choices = unique(datasub()[,"Geography2"]),
selected = unique(datasub()[,"Geography2"])[1])
})
datasub2 <- reactive({
datasub()[Test$Geography2 == input$geog2, ]
})
output$service = renderUI({
selectInput(inputId = "service",
label = "Service type:",
choices = unique(datasub2()[,"Service"]),
selected = unique(datasub2()[,"Service"])[1])
})
datasub3 <- reactive({
datasub()[Test$Service == input$service, ]
})
output$plot = renderPlot({
ggplot(datasub3(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
geom_bar(stat = "identity")
})
}
shinyApp(ui, server)
#数据
试井一个错误是您应该将renderPlot()
更改为datasub-Hi-Juergen。谢谢你在这方面的帮助。后续内容:该图通过评级(沿X轴的分类变量)显示位置(y轴)的数量。因此,我想让用户能够跨不同的地理层次结构过滤这些数据,例如西北部(地区)的额定位置数或东苏塞克斯(地方当局)的额定位置数。运行了您建议的代码(谢谢),这是一个很好的开始,但我遇到了“逻辑索引的长度必须是1或49311,而不是197244”的问题。数据未完全过滤。能否为可复制示例提供数据集?否则很难猜出哪里出了问题。我认为示例中的数据集对于过滤没有任何意义。