谁可以帮忙,这是我的第一篇文章.我花了几个小时试图弄清楚如何使用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)
Run Code Online (Sandbox Code Playgroud)
然而,用户界面工作正常;服务器函数生成一个空图。
这很可能是由于该函数subset返回一个空数据集。为了调试代码,首先,我会在控制台中打印出这部分:
C2 <- reactive(subset(CONQDF,input$Demog %in% levels(input$Demog)[1] & CONQDF$VW_Model == input$Car))
Run Code Online (Sandbox Code Playgroud)
我认为这部分是错误的,因为input$Demog它只是一个字符串而不是一个因素。这就是为什么levels(input$Demog) = NULL和input$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")
})
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,你只需要重新考虑子集部分。
| 归档时间: |
|
| 查看次数: |
7778 次 |
| 最近记录: |