0

我创建了一个闪亮的应用程序。交互式柱形图出现故障 - 它没有显示两列,而是显示一列。此外,如果我选择不同的变量,图表不会自动更新。

如果有人可以帮助解决它,我将不胜感激。太感谢了!!

这是代码:

library(shiny)
library(ggplot2)
library(plotly)


a=c("Female","Female","Male","Female","Male")
b=c("Yes","No","No","Yes","No")
c=c("Yes","Yes","Yes","No","No")
Mydata=data.frame(gender=a,SeniorCitizen=b,Churn=c)

ggplot(Mydata)+geom_bar(aes(gender,fill=Churn))+
  ggtitle("Categorical Variable vs Target Variable")+theme(plot.title = element_text(hjust = 0.5))

ui = shinyUI(fluidPage(
  headerPanel(title="Features Review"),
  sidebarPanel(
    selectInput("Variable","Select A Categorical Variable",
                list("gender"="gender","SeniorCitizen"="SeniorCitizen"
                ))),

  mainPanel(
    titlePanel(title="Categorical Variable Visualization"),plotOutput("CPlots"))

  ))


server = shinyServer(function(input, output) {

  output$CPlots = reactivePlot(function(){
    ggplot(Mydata)+geom_bar(aes(input$Variable,fill=Churn))+
      ggtitle("Categorical Variable vs Target Variable")+theme(plot.title = element_text(hjust = 0.5))
  })
})


shinyApp(ui,server)
4

2 回答 2

0

根据您的评论和@DSGym 的建议,我认为您需要以下代码:

library(shiny)
library(ggplot2)
library(plotly)

a = c("Female", "Female", "Male", "Female", "Male")
b = c("No", "No", "No", "Yes", "No")
c = c("Yes", "Yes", "Yes", "No", "No")
Mydata = data.frame(gender = a,
                    SeniorCitizen = b,
                    Churn = c)

ui = shinyUI(fluidPage(
    headerPanel(title = "Features Review"),
    sidebarPanel(selectInput(
        "Variable",
        "Select A Categorical Variable",
        list("gender" = "gender", "SeniorCitizen" = "SeniorCitizen")
    )),

    mainPanel(
        titlePanel(title = "Categorical Variable Visualization"),
        plotOutput("CPlots")
    )

))

server = shinyServer(function(input, output) {
    output$CPlots = renderPlot(
        ggplot(Mydata, aes(
            x = get(input$Variable), fill = Churn
        )) +
            geom_bar() +
            ggtitle("Categorical Variable vs Target Variable") +
            theme(plot.title = element_text(hjust = 0.5))
    )
})

shinyApp(ui, server)
于 2019-06-01T03:35:52.330 回答
0
library(shiny)
library(ggplot2)
library(plotly)


a=c("Female","Female","Male","Female","Male")
b=c("Yes","No","No","Yes","No")
c=c("Yes","Yes","Yes","No","No")
Mydata=data.frame(gender=a,SeniorCitizen=b,Churn=c)

ggplot(Mydata)+geom_bar(aes(gender,fill=Churn))+
    ggtitle("Categorical Variable vs Target Variable")+theme(plot.title = element_text(hjust = 0.5))

ui = shinyUI(fluidPage(
    headerPanel(title="Features Review"),
    sidebarPanel(
        selectInput("Variable","Select A Categorical Variable", list("gender"="gender","SeniorCitizen"="SeniorCitizen"))),

    mainPanel(
        titlePanel(title="Categorical Variable Visualization"),plotOutput("CPlots"))

))


server = shinyServer(function(input, output) {

    df <- reactive({
        if(input$Variable == "SeniorCitizen") {
            data <- Mydata %>%
                filter(SeniorCitizen == "Yes")
            return(data)
        } else {
            data <- Mydata
            return(data)
        }
    })

    output$CPlots = renderPlot({

        ggplot(data=df(), aes(x = Churn, y = input$Variable,fill=Churn)) +
            geom_bar(stat="identity", color="black") +
            theme_minimal() + 
            ggtitle("Categorical Variable vs Target Variable")+theme(plot.title = element_text(hjust = 0.5))

    })
})


shinyApp(ui,server)

这可行,但是除了 y 轴上的类别之外,您的绘图不会改变任何内容,因为geom_bar只计算每组的变量。也许你想过滤任何东西?我为您提供了一些代码,当您更改 selectInput 时,您可以在其中看到更改。

于 2019-05-31T09:06:32.290 回答