【问题标题】:Rendering Shiny ggplot chart output after gather()在gather()之后渲染闪亮的ggplot图表输出
【发布时间】:2018-08-09 15:49:05
【问题描述】:

我正在尝试为 Shiny 构建一个 ggplot 输出。 我用gather() 将我的初始数据框收集成整洁的格式。

               variable             value
1                Region            Africa
2                Region            Europe
3                Region           Europe 
4                Region     Latin America
13              Country             Sudan
14              Country            Russia
15              Country    United Kingdom
16              Country              Peru
17              Country          Malaysia
35              Client              Bank
36              Client              Bank2
37              PG                  PG 1
38              PG                  PG 2
54                Status              High
55                Status            Signed
56                Status               Low

现在我正在尝试创建一个简单的图表,其中变量的变化应该导致情节的变化。

 # UI code

 ui <- fluidPage(theme = shinytheme("united"),
            sidebarLayout(
              sidebarPanel(
                           selectInput("dataInput", "Choose to filter by:",
                                       choices = c("Region",
                                                   "Country",
                                                   "Client",
                                                   "PG",
                                                   "Status"),
                                       selected = "Choose to display by")
              ),


              mainPanel(

                # Output
                tabsetPanel(type = "tabs",
                            # tabPanel("Map", leafletOutput(outputId = 'map', height = 850)),
                            tabPanel("Plot",  plotOutput("plot", height = 850)))
                )
              )
            )

我的服务器是这样的

 server <- function(input, output) {

 # 1. Select among columns
 selectedData <- reactive({

 data_pg_df1[!is.na(data_pg_df1$variable) & data_pg_df1$variable == input$dataInput, ]
 })

 # Output
 output$plot <- renderPlot({
ggplot(data_pg_df1, aes(selectedData)) + geom_bar()
 })

 }
 shinyApp(ui = ui, server = server)

我收到一个错误 不知道如何为 reactiveExpr/reactive 类型的对象自动选择比例。默认为连续。 警告:错误:列x 必须是一维原子向量或列表

试图解决,但失败了。

我的 ggplot 方法正确吗?或者我还需要在数据输入中添加“值”列。我不这么认为,因为我只想根据“变量”进行选择。 我错过了什么?

我想要的输出看起来像这样。我有 eval(),但想以一种整洁的方式来做,如上所述。

output$plot <- renderPlot({
  eval(parse(text = paste0("ggplot(data_pg_df, aes(x=",input$dataInput,")) + geom_bar()"  )))
})

【问题讨论】:

    标签: r ggplot2 shiny tidyverse


    【解决方案1】:

    您的问题在于如何从 reactive 中检索对象。 您可以尝试以下方法:

    output$plot <- renderPlot({
    data <- selectedData()
    ggplot(data_pg_df1, aes(data)) + geom_bar()
    })
    

    请参阅here 以获取反应性帮助,并查看here 以获取与 SO 相关的问题。 还要小心 ggplot2 语法...

    【讨论】:

    • 谢谢,目前它给了我错误不知道如何为 data.frame 类型的对象自动选择比例。默认为连续。警告:错误:美学必须是长度 1 或与数据 (60) 相同:x。但我会尝试解决它。以前有过。
    • 这里你的问题出在 ggplot 和 data_pg_df1 中(你需要这个或来自反应的“数据”对象吗?)。此外,您还错过了 geom_bar 中的“stat”输入。 (例如 stat = "bin")...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2018-01-25
    相关资源
    最近更新 更多