使用闪亮控制小部件在闪亮 Jmeter 板中更改输出

cl25kdpy  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(201)

下面是一个代码复制了我正在工作的一个shinydashboard。

library(shiny)
library(lubridate)
library(DT)

DF = data.frame(ID = c ("1","2","3","4","5"),
                product = c("A","B","C","A","C"),
                sentiment = c("good","medium","bad","medium","bad"),
                online = c(1,0,1,1,0),
                ooh = c(0,1,0,1,1),
                Date= c("2020-08-11", "2020-09-16","2021-10-20", "2020-11-25", "2022-11-27"), 
                event = c(1,1,0,0,0))

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Product Sentiment"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("sentiment", label = "Select sentiment", choices = unique(DF$sentiment)), 
      dateRangeInput("daterange", h5("Date range"), 
                     start = ymd("2020-08-11"), 
                     end = ymd("2022-09-06"), 
                     min = min(DF$Date), 
                     max = max(DF$Date),
                     format = "yyyy-mm-dd"
      ) 
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("DT_Table")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  filtered_data <- reactive({
    DF %>%
      filter(sentiment == input$sentiment &
               (Date >= input$daterange[1] & Date <= input$daterange[2]))
  })
  
  output$DT_Table <- DT::renderDataTable(
    filtered_data(),
    options = list(pageLength = 5, autoWidth = TRUE, dom='t')
  )
  
}

# Run the application 
shinyApp(ui = ui, server = server)

目前,当您运行 Jmeter 板时,表的第一个输出基于过滤器情感good。但是,我希望 Jmeter 板首先加载dataframe中的所有行-然后,用户根据情绪进行过滤。我该怎么做?

kognpnkq

kognpnkq1#

我在selectInput()中手动追加空白("")。
之后,在reative()处添加if-else逻辑,仅当input$sentiment不为空时才过滤sentiment

library(shiny)
library(lubridate)
library(DT)

DF = data.frame(ID = c ("1","2","3","4","5"),
                product = c("A","B","C","A","C"),
                sentiment = c("good","medium","bad","medium","bad"),
                online = c(1,0,1,1,0),
                ooh = c(0,1,0,1,1),
                Date= c("2020-08-11", "2020-09-16","2021-10-20", "2020-11-25", "2022-11-27"), 
                event = c(1,1,0,0,0))

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Product Sentiment"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("sentiment", 
                  label = "Select sentiment", 
                  choices = c("",unique(DF$sentiment))), 
      dateRangeInput("daterange", h5("Date range"), 
                     start = ymd("2020-08-11"), 
                     end = ymd("2022-09-06"), 
                     min = min(DF$Date), 
                     max = max(DF$Date),
                     format = "yyyy-mm-dd"
      ) 
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput("DT_Table")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  filtered_data <- reactive({
    if(input$sentiment == ""){
      DF %>% filter((Date >= input$daterange[1] & Date <= input$daterange[2]))
    } else {
      DF %>%
        filter(sentiment == input$sentiment &
                 (Date >= input$daterange[1] & Date <= input$daterange[2]))
    }
  })
  
  output$DT_Table <- DT::renderDataTable(
    filtered_data(),
    options = list(pageLength = 5, autoWidth = TRUE, dom='t')
  )
  
}

# Run the application 
shinyApp(ui = ui, server = server)

相关问题