如何在Shiny应用程序中使用ggplot创建反应图

时间:2022-12-01 07:12:05

To whom can help, this is my first post. I have spent several hours trying to figure out how to generate a bar plot using ggplot for a shiny app I want to create. The ui works find, however; the server function generates an empty plot. The issue is with renderPlot function. I believe I must not be passing the reactive values properly to the aes_string arguments in ggplot. C2 is a filtered dataset. The goal is to build a simple app in which the user selects a two variables, a dataset is filtered based upon those variables. The subsetted dataset is passed to ggplot data argument.

谁可以帮忙,这是我的第一篇文章。我花了几个小时试图弄清楚如何使用ggplot为我想要创建的闪亮应用程序生成条形图。然而,ui作品找到了;服务器功能生成一个空图。问题在于renderPlot函数。我相信我不能将反应值正确地传递给ggplot中的aes_string参数。 C2是过滤的数据集。目标是构建一个简单的应用程序,用户在其中选择两个变量,基于这些变量过滤数据集。子集化数据集将传递给ggplot数据参数。

       library(shiny)
library(dplyr)


ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "Demog",label = "Factor:",choices = c("HH Income" = "Income",
                                                                  "Age Group" = "Age",
                                                                  "US Region" = "Region") , selected = "Age"),
      selectInput(inputId = "Car",label = "VW Model:",choices = c("BEETLE" = "BEETLE",
                                                                  "CC" = "CC",
                                                                  "EOS" = "EOS",
                                                                  "GOLF" = "GOLF",
                                                                  "GTI" ="GOLF SPORTSWAGEN GTI",
                                                                  "JETTA" = "JETTA",
                                                                  "PASSAT" = "PASSAT",
                                                                  "TIGUAN" = "TIGUAN",
                                                                  "TOUAREG" = "TOUAREG") , selected = "BEETLE"),
      radioButtons(inputId = "Metric",label ="Measurement Type",choices = 
                     c("Conquest Volume Index" = "TotCmpConqVol_IDX","C/D Ratio" = "TotCmpCDRatio_IDX"), selected = "TotCmpConqVol_IDX" )                

    )
  ),
  mainPanel(
    tags$h1("The Bar Charts"),
    tags$h2("The metrics"),


    plotOutput("P1")

  )

)
server <- function(input, output){
  library(ggplot2)
  CONQDF <- read.csv("C:/Users/Reginald/Desktop/CONQ_VW/CONQUEST2.csv")

  C2 <- reactive(subset(CONQDF,input$Demog %in% levels(input$Demog)[1] & CONQDF$VW_Model == input$Car))

  output$P1 <- renderPlot({
    ggplot(C2(),aes_string(x="CompMake", y=input$Metric))+ geom_bar(stat = "identity")
                          })
}






shinyApp(ui,server)

2 个解决方案

#1


3  

The ui works find, however; the server function generates an empty plot.

然而,ui作品找到了;服务器功能生成一个空图。

This is most likely due to the fact that the function subset returns an empty dataset. In order to debug the code, first, I would print out in the console this part:

这很可能是因为函数子集返回空数据集。为了调试代码,首先,我将在控制台中打印出这部分:

C2 <- reactive(subset(CONQDF,input$Demog %in% levels(input$Demog)[1] & CONQDF$VW_Model == input$Car))

I believe that this part is wrong because input$Demog is just a character string and not a factor. That's why levels(input$Demog) = NULL and input$Demog %in% levels(input$Demog) = FALSE. Hence, as a result, you get an empty dataset.

我相信这部分是错误的,因为输入$ Demog只是一个字符串而不是一个因素。这就是为什么level(输入$ Demog)= NULL并在%level中输入$ Demog%(输入$ Demog)= FALSE。因此,您将获得一个空数据集。

To check this:

要检查一下:

output$P1 <- renderPlot({
    print(C2()) # print it out to the console.
    ggplot(C2(),aes_string(x="CompMake", y=input$Metric))+ geom_bar(stat = "identity")
})

If this is the case, you only need to re-think subsetting part.

如果是这种情况,您只需要重新考虑子集部分。

#2


1  

It looks like your C2 function can't see CONQDF (hence the blank plot). You can add () after CONQDF in your C2 call to run that read.csv every time, but you're probably better off moving the read.csv outside your server function altogether.

看起来你的C2功能看不到CONQDF(因此是空白图)。您可以在C2调用中在CONQDF之后添加()以便每次都运行read.csv,但是最好将read.csv移到服务器函数之外。

So move this line

所以移动这一行

CONQDF <- read.csv("C:/Users/Reginald/Desktop/CONQ_VW/CONQUEST2.csv")

to the top of your script, just below library(dplyr). This will make shiny read that file when the page first loads, instead of every time the input is updated, and will also place the resulting dataframe into the global environment, which will mean your C2 <- call will be able to see it.

在脚本的顶部,就在库(dplyr)下面。这将在页面首次加载时闪亮读取该文件,而不是每次更新输入时,并且还将结果数据帧放入全局环境中,这意味着您的C2 < - 调用将能够看到它。

I can't easily reproduce your app, so I can't test my answer. Please let me know whether or not it helps.

我无法轻松复制您的应用,所以我无法测试我的答案。请告诉我它是否有帮助。

#1


3  

The ui works find, however; the server function generates an empty plot.

然而,ui作品找到了;服务器功能生成一个空图。

This is most likely due to the fact that the function subset returns an empty dataset. In order to debug the code, first, I would print out in the console this part:

这很可能是因为函数子集返回空数据集。为了调试代码,首先,我将在控制台中打印出这部分:

C2 <- reactive(subset(CONQDF,input$Demog %in% levels(input$Demog)[1] & CONQDF$VW_Model == input$Car))

I believe that this part is wrong because input$Demog is just a character string and not a factor. That's why levels(input$Demog) = NULL and input$Demog %in% levels(input$Demog) = FALSE. Hence, as a result, you get an empty dataset.

我相信这部分是错误的,因为输入$ Demog只是一个字符串而不是一个因素。这就是为什么level(输入$ Demog)= NULL并在%level中输入$ Demog%(输入$ Demog)= FALSE。因此,您将获得一个空数据集。

To check this:

要检查一下:

output$P1 <- renderPlot({
    print(C2()) # print it out to the console.
    ggplot(C2(),aes_string(x="CompMake", y=input$Metric))+ geom_bar(stat = "identity")
})

If this is the case, you only need to re-think subsetting part.

如果是这种情况,您只需要重新考虑子集部分。

#2


1  

It looks like your C2 function can't see CONQDF (hence the blank plot). You can add () after CONQDF in your C2 call to run that read.csv every time, but you're probably better off moving the read.csv outside your server function altogether.

看起来你的C2功能看不到CONQDF(因此是空白图)。您可以在C2调用中在CONQDF之后添加()以便每次都运行read.csv,但是最好将read.csv移到服务器函数之外。

So move this line

所以移动这一行

CONQDF <- read.csv("C:/Users/Reginald/Desktop/CONQ_VW/CONQUEST2.csv")

to the top of your script, just below library(dplyr). This will make shiny read that file when the page first loads, instead of every time the input is updated, and will also place the resulting dataframe into the global environment, which will mean your C2 <- call will be able to see it.

在脚本的顶部,就在库(dplyr)下面。这将在页面首次加载时闪亮读取该文件,而不是每次更新输入时,并且还将结果数据帧放入全局环境中,这意味着您的C2 < - 调用将能够看到它。

I can't easily reproduce your app, so I can't test my answer. Please let me know whether or not it helps.

我无法轻松复制您的应用,所以我无法测试我的答案。请告诉我它是否有帮助。