在select输入中观察事件后刷新ggplot

时间:2017-11-18 21:22:38

标签: r charts shiny bar-chart

我正在尝试制作一个条形图,该条形图根据插入组合框中的值而变化(选择输入)。

我使用observe事件控制选择输入,但我无法刷新ggplot。

这就是我得到的:#comobox选择专家数量

inputPanel(
  selectInput("n_experts", label = "Choose:",
           choices = c("All", "Top50","Top10"), selected = "All")
)

 observeEvent(input$n_experts, {
     if(input$n_experts == "Top10"){
          data <<- read.csv(*csvFile*)
          ggplot(data, aes(x = V1, y = V2, fill = winner)) + geom_bar(stat = "identity") + 
          scale_fill_manual(values=c("local" = "dark blue","visitor" = "red"))+
    coord_flip()
    print("button top pressed")
  }else if(input$n_experts == "All"){
     data <<- read.csv(*csvFile*)
     ggplot(data, aes(x = V1, y = V2, fill = winner)) + geom_bar(stat = "identity") + 
      scale_fill_manual(values=c("local" = "dark blue","visitor" = "red"))+
      coord_flip()
    print("button all pressed")
 }
   })

代码已编辑为给定示例

library(shiny)
library(ggplot2)


ui <- fluidPage(

  inputPanel(
    selectInput("n_experts", label = "Choose:",
                choices = c("All", "Top50","Top10"), selected = "All")
  ),
  mainPanel(plotOutput("plot2"))
  )

server <- function(input,output){

  dat <- reactive({
    input <- input$n_experts
    if(input == "All"){
      info <<- read.csv(file1)
    }else if (input == "Top10"){
      info <<- read.csv(file2)
    }

  })

  info[["winner"]] = ifelse(metapicks[["V2"]] >= 0, "local", "visitor")

  output$plot2<-renderPlot({
                 ggplot(info, aes(x = V1, y = V2, fill = winner)) + geom_bar(stat = "identity") + 
                      scale_fill_manual(values=c("local" = "dark blue","visitor" = "red"))+
                      coord_flip()},height = 600,width = 800)}
shinyApp(ui, server)

0 个答案:

没有答案