我有这个数据集:
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())
结果是我可以选择输入但无法可视化绘图。报错“unknown column Area or unknow Type of participants”
请帮我找出错误
谢谢
*** 更新
感谢 Flick 先生,我已经修复了我的代码,但它仍然提示错误“找不到对象区域”。请帮忙指教。非常感谢
`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())`
最佳答案
@Suzie - 如上所述,如果您使用当前拥有的完整代码编辑您的问题,将会有所帮助。
一些有用的东西:
- Salary 在您的
df
中应该是数字(或者在尝试取平均值之前用as.numeric
转换 - 您的
reactive
表达式可以将!!as.symbol
与input$selection
一起使用,以根据df< 中的字符串名称进行过滤
- 绘图可以使用
aes_string
作为变量名。
编辑:
要进一步解释!!as.symbol
,首先考虑input$selection
的结果是什么。如果您在 Shiny 的代码中使用 browser()
,并检查 input$selection
返回的内容,您将看到类似 "Area"
的内容(它返回一个字符串)。但是字符串不适用于您的 filter
- 它需要一个符号来表示数据框中的列。 (符号是对象的名称,如 df
或 mtcars
等)
首先,您要将字符串转换为符号。您可以使用 as.symbol()
或 rlang::sym()
来做到这一点。您可以在控制台中尝试一下。如果您执行 as.symbol("df")
,它将返回符号 df
。如果您输入 eval(as.symbol("df"))
,它将与仅输入 df
本身相同(并且它将显示数据框的内容) .
另一个问题是 tidyverse
函数在特殊上下文中计算代码表达式(例如,在数据框中搜索名称)。在这种情况下,dplyr
知道名称 Area
在 df
(列名称之一)的上下文中。这是一个复杂的因素,因为引用了参数。要解决此问题,您需要使用 bang-bang !!
运算符取消引号(将名称替换为其值)。
将两者放在一起你会得到 !!as.symbol()
。
值得注意的是,varSelectInput
是 selectInput
的新替代品,可以考虑在这些情况下使用。
更多信息:
library(tidyverse)
library(shiny)
Area <- c("Mexico", "USA", "USA", "Canada")
Type_of_participants <- c("Doctor", "Doctor", "Engineer", "Dancer")
Salary <- c(4000, 6000, 8000, 5000)
df <- data.frame(Area, Type_of_participants, Salary)
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(Area, Type_of_participants) %>%
filter(!!as.symbol(input$selection) %in% input$baseinput) %>%
summarise(avg_salary_by_area = mean(Salary, na.rm = TRUE))
})
output$Overview <- renderPlot({
ggplot(data = dt1(), aes_string(x = input$selection, y = "avg_salary_by_area", fill = "Type_of_participants")) +
geom_bar(stat="identity", position = position_dodge())
})
}
shinyApp(ui, server)
关于使用响应式(Reactive)绘图时出现 R Shiny 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60625362/