使用pickerInput()过滤数据并基于R

时间:2019-04-26 08:52:44

标签: r shiny

我目前正在开发闪亮的应用程序,我需要根据用户选择过滤数据。使用的输入控件是pickerInput()。我尝试了以下代码,但不起作用。

library("shiny")
library("dplyr")
library("shinyWidgets")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
      )
    ),
    mainPanel(
      plotOutput("test")        
    )
  )
)



server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )

  })

  filter1 <-  reactive({

    df %>% filter(ID %in% input$id)
})


  output$test <- renderPlot({

    ggplot(data = filter1, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)

有人可以帮助我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

呼叫反应型filter1时,您需要使用括号()。 请参阅以下内容:

library("shiny")
library("dplyr")
library("shinyWidgets")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
    )
    ,
    mainPanel(
      plotOutput("test")        
    ))
)

server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )
  })

  filter1 <-  reactive({
    df %>% filter(ID %in% input$id)
  })

  output$test <- renderPlot({
    myFilteredDf <- filter1()
    ggplot(data = myFilteredDf, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

这是从R Shiny开始时遇到的经典错误。如上所述,每当调用反应性数据对象filter1时,都需要添加括号。 该代码对我有用:

library("shiny")
library("dplyr")
library("shinyWidgets")
library("ggplot2")

mychoices <- c("A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
               "U", "V", "W", "X", "Y", "Z")
df <- data.frame("ID" = mychoices, "invoice" = runif(26, 100, 200))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("id1")
    )
  ,
  mainPanel(
    plotOutput("test")        
  )
)
)




server <- function(input, output) {

  output$id1 <- renderUI({
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = mychoices,
      options = list('actions-box' = TRUE),
      multiple = TRUE
    )

  })

  filter1 <-  reactive({

    df %>% filter(ID %in% input$id)
  })


  output$test <- renderPlot({
    filter1 = filter1()
    ggplot(data = filter1, aes(invoice)) + geom_histogram(binwidth=30, alpha=0.6)
  })
}

shinyApp(ui = ui, server = server)