[英]R shiny error when using reactive to plot
我有這個數據集:
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())
結果是我可以選擇輸入但無法可視化繪圖。 錯誤“未知列區域或未知參與者類型
請幫我找出錯誤
謝謝
*** 更新
感謝 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 - 如上所述,如果您使用當前擁有的完整代碼編輯問題,將會有所幫助。
幾件事會有所幫助:
df
應該是數字(或者在嘗試取平均值之前用as.numeric
轉換reactive
表達式可以使用!!as.symbol
和input$selection
來過濾來自df
的字符串名稱aes_string
作為變量名稱。編輯:
為了進一步解釋!!as.symbol
,首先考慮input$selection
的結果。 如果您在閃亮的代碼中使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.