0
votes

I am trying to get two text fields to display based on a reactive user defined ID. However, what I have currently is not displaying any text for the desired fields. I thought it had to do with the fact that there are 25 data points with the same ID_no (I am using the data to plot, but since it was unrelated to my question, I removed the plot). However, after removing the data in troubleshooting, no text was being displayed still. I am pretty sure my error is in lines 51 and 52 of the code, but I am unsure how to correct it. Any help with troubleshooting is appreciated.

#Check packages to use in library
{
  library('shiny') #allows for the shiny app to be used
  library('magrittr')
}

#Data

ID_no <- 123
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee1")
date <- Sys.Date()
ID_1 <-data.frame(ID_no, Data_val, employee_name, date)

ID_no <- 456
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee2")
date <- Sys.Date()-10
ID_2 <-data.frame(ID_no, Data_val, employee_name, date)

data <-rbind(ID_1, ID_2)
IDchoices <- as.character(unique(data$ID_no))

# UI

  ui <- fluidPage(
    fluidRow(
      column(4,
             wellPanel(
              selectInput(inputId = "ID", label="Select ID:", choices = IDchoices, selected = "1", multiple = FALSE, selectize = TRUE)
            ),
             wellPanel(span(h5(strong("Employee:")), h5(textOutput("Staff_name"))),
                       span(h5(strong("Date:")),h5(textOutput("Date"))))
     )
    )
   )

#SERVER

server <- function(input, output, session)
{
 filteredData <- reactive({
    m <- data %>% filter(
      ID_no %in% input$ID
    )
    m
  })


 output$Staff_name <- renderText({ filteredData()$employee_name })
 output$Date <- renderText({ filteredData()$date })
}

#Run the Shiny App to Display Webpage

shinyApp(ui=ui, server=server)
1
Are you using stats::filter or dplyr::filter in the reactive part on the server side? If you're using stats::filter you get an object of the class mts and then subsetting with ...()$Date won't work. If you're using dplyr::filter to get the subset, you should change renderText({ filteredData()$Date }) to renderText({ filteredData()$date }). - Michal Majka
In your dataset there is no variable called Date but date - Michal Majka
@MichalMajka My apologies, this typo has been corrected, but did not fix the original issue. I am using the dplyr package for the filter. - User247365
You should also attach dplyr package or write explicitly dplyr::filter. If you don't do this then shiny will use stats::filter function. dplyr will automatically import pipes from magrittr package. - Michal Majka

1 Answers

2
votes

There are a couple of typos where you were trying to access column name Date instead of date and output$staff_name instead of output$Staff_name Also the filter function used like mentioned in the comments is from the wrong package. Either you need to specify dplyr::filter or load the dplyr library before running this code.

This should work for you:

  output$Staff_name <- renderText({ as.character(filteredData()$employee_name[1])})
  output$Date <- renderText({ format(filteredData()$date[1],"%Y-%m-%d") })

This is the entire code that works for me:

#Check packages to use in library
{
  library('shiny') #allows for the shiny app to be used
  library('magrittr')
}

#Data

ID_no <- 123
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee1")
date <- Sys.Date()
ID_1 <-data.frame(ID_no, Data_val, employee_name, date)

ID_no <- 456
Data_val <- sample(0:100, 25)
employee_name <- as.character("Employee2")
date <- Sys.Date()-10
ID_2 <-data.frame(ID_no, Data_val, employee_name, date)

data <-rbind(ID_1, ID_2)
IDchoices <- as.character(unique(data$ID_no))

# UI

ui <- fluidPage(
  fluidRow(
    column(4,
           wellPanel(
             selectInput(inputId = "ID", label="Select ID:", choices = IDchoices, selected = "1", multiple = FALSE, selectize = TRUE)
           ),
           wellPanel(span(h5(strong("Employee:")), h5(textOutput("Staff_name"))),
                     span(h5(strong("Date:")),h5(textOutput("Date"))))
    )
  )
)

#SERVER

server <- function(input, output, session)
{
  filteredData <- reactive({
    m <- data %>% dplyr::filter(
      ID_no %in% input$ID
    )
    m
  })


  output$Staff_name <- renderText({ as.character(filteredData()$employee_name[1]) })
  output$Date <- renderText({ format(filteredData()$date[1],"%Y-%m-%d") })
}

#Run the Shiny App to Display Webpage

shinyApp(ui=ui, server=server)