How to subset the values of a shiny widget based on the values of another shiny widget

会有一股神秘感。 提交于 2020-05-17 08:47:12

问题


I have the shiny app delow in which the user uploads a file. Then there is a column named EventDate which contains Dates. Those dates are passed to a date range Input. There is also a second widget which takes as inputs the unique values of the 1st column of the uploaded file. When the actionbutton is pressed the data frame is subseted based on the picker input.

The issue is that Im trying to subset the picker input values based on the date range so I use:

#dat<-subset(dat, EventDate>=input$dateRange[1]&EventDate<=input$dateRange[2])

but then everything is empty. Its like 'nothing selected'. How can I subset the picker Input values based on the date range input?

library(shiny)
library(DT)
library(shinyWidgets)

ui <- pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    fileInput("file1", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),
    uiOutput("dr"),
    uiOutput("picker"),
    actionButton("go","Go")
  ),
  mainPanel(
    DTOutput("dtable")
  )
)

server <- function(input, output, session) {
  output$dr<-renderUI({

    inFile <- input$file1
    df<-data.frame(read.csv(inFile$datapath, header = TRUE))
    df$EventDate <-as.Date(df$EventDate, "%Y-%m-%d")

    dateRangeInput('dateRange',
                   label = 'Date range Input',
                   start = min(df$EventDate) ,end= max(df$EventDate) 
    )

  })
  filteredCSV <- reactiveVal(NULL)

  CSV <- eventReactive(input[["file1"]], {
    dat <- read.csv(input[["file1"]]$datapath, header = TRUE)
    dat<-data.frame(subset(dat, as.Date(EventDate)>=as.Date(input$dateRange[1], "%Y-%m-%d") & 
                         as.Date(EventDate)<=as.Date(input$dateRange[2], "%Y-%m-%d")))

    filteredCSV(dat)
    dat
  })


  output[["picker"]] <- renderUI({
    req(CSV())
    choices <- unique(as.character(CSV()[,1]))
    pickerInput("select", "Select ID", 
                choices = choices, 
                multiple = TRUE, options = list(`actions-box` = TRUE),
                selected = choices)
  })

  observeEvent(input[["go"]], {
    req(CSV())
    filteredCSV(CSV()[CSV()[,1] %in% input[["select"]],])
  })

  output[["dtable"]] <- renderDT({
    req(filteredCSV())
    datatable(
      filteredCSV(), 
      options = list(scrollX = TRUE, pageLength = 5)      
    )
  })

}

shinyApp(ui = ui, server = server)

来源:https://stackoverflow.com/questions/61501058/how-to-subset-the-values-of-a-shiny-widget-based-on-the-values-of-another-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!