首页 文章

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

提问于
浏览
5

谁可以帮忙,这是我的第一篇文章 . 我花了几个小时试图弄清楚如何使用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

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

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

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

    我认为这部分是错误的,因为 input$Demog 只是一个字符串而不是一个因素 . 这就是 levels(input$Demog) = NULLinput$Demog %in% levels(input$Demog) = FALSE 的原因 . 因此,您将获得一个空数据集 .

    要检查一下:

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

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

  • 3

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

    所以移动这一行

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

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

    我不能轻易地重现你的应用程序,所以我无法测试我的答案 . 请告诉我它是否有帮助 .

相关问题