shiny app histogram - connected to ID the customer enters

Hi everyone,
I'm building a shiny app and I want my customer to enter a participant ID, which is connected to specific participant with specific data. With that ID I want to build a histogram which shows on x axis the dates and on Y axis the amount of a variable. how can I do that?
Do I need to write it in the server? If so, where and how? because I saw the syntax is a little bit different in shiny app from R.
Thank you!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

No problem, its just that the problem is not in my code but something I don't know how to write in shiny because I can't find the right tutorial for that. So maybe you could help me with that. I don't know how to tell R to take the variables from the csv I have, and show it on shiny histogram, after the customer entered ID that is connected to my data. Basically the problem is that I don't know how to tell R that the variable equals the data on the histogram and the ID that the customer entered is the ID that I have on my csv. Maybe a general example will help me understand how to do it.
Thank you!

1 Like

Hi @eliyac,

This should generally work, if you replace the variable names with variables in your data. I have denoted what needs to be changed with comments.

library(shiny)
library(dplyr)
library(magrittr)

ui <- fluidPage(
  textInput('id', 'Participant ID', placeholder = 'Enter ID here...'),
  fileInput('csv', 'Input CSV', accept = '.csv'),
  plotOutput('hist')
)

server <- function(input, output, session) {
  data <- reactive({
    req(input$csv)
    read.csv(input$csv$datapath)
  })
  
  output$hist <- renderPlot({
    
    if (input$id==NULL) {
      data() %$%
        hist(var_name) # replace var name
    } else {
      hist() %>% 
        filter(idvar == input$id) %$% # replace idvar
        hist(var_name) # replace var name
    }
  })
  
}

shinyApp(ui, server)

@mattwarkentin Thank you!
I tried to change the code a bit because it had errors. Now the main problem is filtering the data file by the input ID (the filter function fails).

This is how the server looks now:

server <- function(input, output, session) {
data <- reactive({
req(input$csv)
read.csv(input$csv$datapath)
})
output$hist <- renderPlot({
hist(data()$dep_1) %>%
filter(id == input$id) %$%
hist(dep_1)
})
}
Can you help me understand how to filter correctly?
Thanks again

You need to subset the data before you pass it to the hist() function.

data() %>%
  filter(id %in% input$id) %$%
  hist(var_name)

@mattwarkentin
I did that, but I got a match error (Warning: Error in match: 'match' requires vector arguments [No stack trace available]), so I tried to put my histogram inside the reactive function.
Now there is no error but no histogram too.

This is the code now:

server <- function(input, output, session) {
data <- reactive({
req(input$csv)
read.csv(input$csv$datapath)
output$hist <- renderPlot({
data() %>%
filter(id %in% input$id)
hist(dep_1)
})
})
}

what should I do now?
Thank you!

This code works for me:

library(shiny)
library(dplyr)
library(magrittr)

ui <- fluidPage(
  textInput('id', 'Participant ID', placeholder = 'Enter ID here...'),
  fileInput('csv', 'Input CSV', accept = '.csv'),
  plotOutput('hist')
)

server <- function(input, output, session) {
  
  data <- reactive({
    req(input$csv)
    req(input$id)
    read.csv(input$csv$datapath)
  })
  
  output$hist <- renderPlot({
    data() %>% 
      filter(id %in% input$id) %$%
      hist(dep_1)
  })
}

shinyApp(ui, server)

I tried to run the code you sent and it's still not working..
What can be the problem? maybe something with the packages?
Thank you!

What error message are you receiving?

This is the error I'm getting in the web page:
"'match' requires vector arguments"
In the console, I get two errors:
Warning in force(expr) :
strings not representable in native encoding will be translated to UTF-8
Warning: Error in match: 'match' requires vector arguments
[No stack trace available]

We are chasing our own tail. Best bet to solve this is to share as much code as necessary so that someone else can reproduce the error.

This is the code that I am trying to run, can anybody help?

library(shiny)
library(dplyr)
library(magrittr)

ui <- fluidPage(
textInput('id', 'Participant ID', placeholder = 'Enter ID here...'),
fileInput('csv', 'Input CSV', accept = '.csv'),
plotOutput('hist')
)

server <- function(input, output, session) {

data <- reactive({
req(input$csv)
req(input$id)
read.csv(input$csv$datapath)
})

output$hist <- renderPlot({
data() %>%
filter(id %in% input$id) %$%
hist(dep_1)
})
}
shinyApp(ui, server)

Okay, I was able to reproduce your error. The most likely issue is that you have not changed the parts of the code so that they match up with your specific data/variables. You need to change the parts below:

data() %>% 
  filter(id %in% input$id) %$%
  hist(dep_1)

You need to change the first id in the filter() function to whatever the ID variable is in YOUR data set. Also, you need to change dep_1 in hist() to the name of the variable you want to plot in YOUR data set. Try this.

This is how it looks like now:
output$hist <- renderPlot({
data() %>%
filter(part_1 %in% input$id) %$%
hist(dep_1)
dep_1 is my variable, I changed "id" to my variable, and now I get an error that says "object 'parti1' not found", maybe I need to write it in different way?

You need to share the structure of your data, if possible. The code works on my end, so any differences are probably due to incompatibility with your specific data structure.

image

It looks like that

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.