sliderInput闪亮 - 不能正确GGPLOT2设置和更新(sliderInput in sh

2019-09-30 02:39发布

这是我建立一个应用程序,我想知道的是,我失去了我的代码中的功能或参数的第一次。 我想显示一个正常的柱状图,其动态过滤“公司”,我想看到显示的性能变量(如“利润”)和日期范围。

我相信我的错误是不精确的结果shiny电话,因为我要的是相当简单的。 你可以通过运行排序得到它(但按规定没有可能相互作用的shiny )下面的代码:

library(ggplot2)

sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                        300,1000,630,
                                        410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))

    ggplot(data = sample_data, aes(x=Year, y = Profits_MM, 
    fill=as.factor(Company_Name))) + geom_col(position="dodge")

但我只提到它这样一个可能有什么我想作为输出的想法。 我已经将有光泽的代码实际如下:

rm(list=ls()); gc()

library(shiny)
library(ggplot2)
library(dplyr)


sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                        300,1000,630,
                                        410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))


# UI
ui <- fluidPage(

  sidebarLayout(

    # Input(s)
    sidebarPanel(

      checkboxGroupInput(inputId = "sel_com",
                         label = "Company Selection:",
                         choices = c("Company 1","Company 2","Company 3"),
                         selected = "Company 1"),


      selectInput(inputId = "y", 
                  label = "Performance Variable",
                  choices = c("Profits (in Millions)" = "Profits_MM", 
                              "Sales (in Millions)" = "Sales_MM"),
                  selected = "Profits_MM"),


      sliderInput("year","Year Selection:",
                  min=2016,
                  max=2018,
                  value=c(2017,2018),
                  step=1)


    ),

    # Output(s)
    mainPanel(
      plotOutput(outputId = "barplot")
    )
  )
)

# Server
server <- function(input, output, session) {

  companies_sel <- reactive({

    req(input$sel_com)

    filter(sample_data, Company_Name %in% input$sel_com)

  })

  year_sample <- reactive({

    req(input$year)

    if(length(input$year)>1){

      Years = seq(input$year[1],input$year[2])

      filter(companies_sel(), Year %in% Years)

    }  

    if(length(input$year==1)){

      filter(companies_sel(), Year %in% input$year)

    }

  })


  output$barplot <- renderPlot({

    ggplot(data = year_sample(), aes_string(x=input$year, y = input$y, fill=as.factor(input$sel_com))) +

      geom_col(position="dodge")

  })

}

shinyApp(ui = ui, server = server)

我能得到一些输出,但只为一家公司的时间和不改变的范围大小。 也许我不明白使用的observeEvent功能,这可能会在此发挥作用。 那么,如何让inputSlider -也许其他的选择-一个充分互动ggplot2图?

这是在ouptut错误的例子:

Answer 1:

试试这个代码:

   library(ggplot2)

sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                      300,1000,630,
                                      410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))

ggplot(data = sample_data, aes(x=Year, y = Profits_MM, 
                               fill=as.factor(Company_Name))) + geom_col(position="dodge")






library(shiny)
library(ggplot2)
library(dplyr)


sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3",
                                        "Company 1","Company 2","Company 3"),
                         Profits_MM = c(20,100,80,
                                        45,120,70,
                                        50,110,90),
                         Sales_MM = c(200,800,520,
                                      300,1000,630,
                                      410,1150,770),
                         Year=c(2016,2016,2016,
                                2017,2017,2017,
                                2018,2018,2018))


# UI
ui <- fluidPage(

  sidebarLayout(

    # Input(s)
    sidebarPanel(

      checkboxGroupInput(inputId = "sel_com",
                         label = "Company Selection:",
                         choices = c("Company 1","Company 2","Company 3"),
                         selected = "Company 1"),


      selectInput(inputId = "y", 
                  label = "Performance Variable",
                  choices = c("Profits (in Millions)" = "Profits_MM", 
                              "Sales (in Millions)" = "Sales_MM"),
                  selected = "Profits_MM"),


      sliderInput("year","Year Selection:",
                  min=2016,
                  max=2018,
                  value=c(2017,2018),
                  step=1)


    ),

    # Output(s)
    mainPanel(
      plotOutput(outputId = "barplot")
    )
  )
)

# Server
server <- function(input, output, session) {

  companies_sel <- reactive({

    req(input$sel_com)

    sample_data_gg<-filter(sample_data, Company_Name %in% input$sel_com)
  #  print(sample_data_gg)
    sample_data_gg

  })

  year_sample <- reactive({

    req(input$year)
    sample_data_gg <- sample_data
    if(length(input$year)>1){

      Years = seq(input$year[1],input$year[2])

      sample_data_gg<-filter(companies_sel(), Year %in% Years)

    }  

    if(length(input$year==1)){

      sample_data_gg<-filter(companies_sel(), Year %in% input$year)

    }
  #  print(sample_data_gg)
    sample_data_gg
  })


  output$barplot <- renderPlot({
    sample_data_gg <- year_sample()
    y <- input$y

    ggplot(data = sample_data_gg, aes(x=Year, y =get( y ), fill=Company_Name)) +

      geom_col(position="dodge")

  })

}

shinyApp(ui = ui, server = server)

我喜欢存储year_sample()第一,所以我没有使用inputaes_string在我ggplot 。 另外,我喜欢get其他由它来引用列string名称。

最好!



文章来源: sliderInput in shiny — cannot set it correctly in ggplot2 and update it
标签: r ggplot2 shiny