【发布时间】:2020-03-10 20:22:16
【问题描述】:
我有这个数据集:
Area <- c("Mexico", "USA", "USA", "Canada").
Type_of_participants <- c("Doctor", "Doctor", "Engineer", "Dancer".
Salary <- c("4000", "6000", "8000", "5000").
我正在尝试根据 Area(level1) 和 Type_of_participants(level2) 的用户输入绘制工资基数,但没有出现任何内容。当我在这里查找时,我将 aes 修改为 aes_string。请帮我找出错误
我的代码
`ui <- fluidPage(
titlePanel("Survey Results"),
sidebarLayout(
sidebarPanel(strong("Overview Plot"),
br(),
###1a.Area input
selectInput("selection","Var",
choices = c("Area","Type_of_participants"),
selected = "Area"),
uiOutput("choice_selection")
),
mainPanel(
plotOutput("Overview"))
`server <- function(input, output) {
output$choice_selection <- renderUI({
checkboxGroupInput("baseinput","Detail",
unique(df[,input$selection])
)`
})
dt1 <- reactive({
df %>%
group_by(input$selection,Type) %>%
filter (input$selection %in% input$baseinput) %>%
summarise(avg_salary_by_area = mean(Salary, na.rm = TRUE)) %>%
select(input$selection, Type, avg_Salary_by_area)
})
output$Overview <- renderPlot({
ggplot(data= dt1())+
aes(fill = Type)+
geom_bar(x=input$selection, y = avg_salary_by_area,stat="identity",
position = position_dodge())
结果是我可以选择输入但无法可视化绘图。错误“未知列区域或未知参与者类型
请帮我找出错误
谢谢
***更新
感谢弗利克先生,我已经修复了我的代码,但它仍然提示错误“找不到对象区域”。请帮忙指教。非常感谢
`dt1 <- reactive({
df[df[,input$selection] %in% input$baseinput,] %>%
group_by(input$selection,Type) %>%
summarise(avg_score_by_area = mean(Score, na.rm = TRUE))
})
output$Overview <- renderPlot({
ggplot(data= dt1(),aes_string(x= input$selection,
y = "avg_score_by_area",fill = "Type"))+
geom_bar(stat="identity",
position = position_dodge())`
【问题讨论】:
-
您的服务器功能似乎不完整。也许这会有所帮助:Shiny: Change column used in ggplot2 dynamically
-
您好,非常感谢您的帮助。我试图修改我的代码如下。它仍然有错误“找不到对象'区域'”。请告知 dt1 % group_by(input$selection,Type) %>% summarise(avg_salary_by_area = mean(Score, na.rm = TRUE)) }) output$Overview
-
嗨@Suzie - 请不要在 cmets 中发布代码。相反,请编辑问题以反映其新状态