我能';t使用SHINK中的SUMMARY(平均值函数)绘制条形图

我能';t使用SHINK中的SUMMARY(平均值函数)绘制条形图,r,shiny,R,Shiny,我试图计算每个物种的平均踏板长度(或宽度),然后使用shinny应用程序将其绘制在条形图中。但是总结中的平均函数一直给我带来问题 library(datasets) library(shiny) library(dplyr) library(ggplot2) data("iris") # Define UI for application that draws a histogram ui <- fluidPage( # Application title titleP

我试图计算每个物种的平均踏板长度(或宽度),然后使用shinny应用程序将其绘制在条形图中。但是总结中的平均函数一直给我带来问题

library(datasets)
library(shiny)
library(dplyr)
library(ggplot2)
data("iris")

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Analyze Iris table"),

    # Sidebar with a dropdown menu selection input for key meausre compoenent
    sidebarLayout(
        sidebarPanel(
            selectInput("yInput", "Measuring element: ", 
                            colnames(iris), selected = colnames(iris)[2]), 
            selectInput('xInput', 'Grouper: ', 
                            colnames(iris), selected = colnames(iris)[5])
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("barPlot")
        )
    )
)

这里,它是一个字符串元素,因此转换为
sym
bol并计算(
!!

-输出


我认为您的问题与IRIS中变量的名称有关。他们有一个。。尝试删除itI将其标记为的副本,并且@YitongLi似乎得到了您的确认。从那以后,又有人重新打开了它。你的意图是什么?@camille我重新打开了它,因为它只是部分被骗了。还有
ggplot
aes\u字符串
,它需要modification@akrun我只是想检查OP是否希望他们的关闭被覆盖。一个
aes\u字符串
选项可以很好地添加到上一篇文章中,尽管它很软-deprecated@camille您的dupe标签将解决第一个问题,即
summary
,它没有任何需要修复的
ggplot
组件谢谢!数据现在已更正,但图形仍然不正确:(我将!!rlang::sym()添加到aes(x=…)中,因为该数据随输入而更改,但出现了“找不到”错误rlang@YitongLi为什么不正确?它根据您提供的输入给出输出。检查
iris%%>%groupby(Sepal.Width)%%>%summary(n=n(),mean_y=mean(Petal.Length))%%>%ggplot(,aes(x=Sepal.Width,y=mean_y))+geom_bar(stat='identity')
@YitongLi你检查过我的更新代码了吗,因为我使用的是
aes_字符串而不是
aes
。另外,如果你没有安装包
rlang
,那么
install.packages(“rlang”)
server <- function(input, output) {

    by_xInput <- reactive({


        iris %>% 
        group_by(input$xInput) %>% 
        summarize(n = length(input$xInput), mean_y = mean(input$yInput))

        })

    output$barPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        ggplot(data = by_xInput(), aes(x = input$xInput, y = mean_y)) + 
            geom_bar(stat = 'identity')

        })

}
shinyApp(ui = ui, server = server)
library(dplyr)
library(shiny)
library(ggplot2)
server <- function(input, output) {

    by_xInput <- reactive({


      iris %>% 
        group_by_at(input$xInput) %>% 
        # n() can replace the length
        # convert string to symbol and evaluate (!!)
        summarize(n = n(), mean_y = mean(!! rlang::sym(input$yInput)))


        })

    output$barPlot <- renderPlot({

        # as the input is a string, use `aes_string`
        ggplot(data = by_xInput(), aes_string(x = input$xInput, y = "mean_y")) + 
            geom_bar(stat = 'identity')

        })

}
shinyApp(ui = ui, server = server)