交互式柱形图无法正常工作

时间:2019-05-31 04:28:12

标签: r ggplot2 shiny interactive

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

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

代码如下:

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)

2 个答案:

答案 0 :(得分:0)

     @Html.ActionLink("Approve", "Approved", new { id = item.ClusterID }, new { @style = "display:none" })

这可行,但是您的图除了y轴上的类别外不会改变任何其他内容,因为it('should check cancel link is availabe', () => { const wrapper = shallow(<RoomDetails {...defaultProps} />); var anchorHref = wrapper.find('<a>').prop('href'); expect(anchorHref).toEqual('https:example.com/change') });仅按组计算变量。也许您想过滤任何东西?我为您提供了一些代码,您可以在其中更改selectInput时看到更改。

答案 1 :(得分: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)