How to make a simple Shiny with radio buttons, followed by drop down menu's and a interactive histogram of the previous made choises?

Hi all

As follow up of my previous topic related R Markdown, I would like to make a kind of stand-alone Shiny app. Not the big deal with servers and so on.

The goal is to import the txt file with the data, to remove the NA and rows with empty values...

I use the code:

#First the exported file from the Infinity server needs to be in the correct folder and read in:
setwd("G:/My Drive/Traineeship Advanced Bachelor of Bioinformatics 2022/Internship 2022-2023/internship documents/")
server_data <- read.delim(file="PostCheckAnalysisRoche.txt", header = TRUE, na.strings=c(""," ","NA"))


#Create a subset of the data to remove/exclude the unnecessary columns:
subset_data_server <- server_data[,c(-5:-7,-9,-17,-25:-31)]
#Remove the rows with blank/NA values:
df1 <- na.omit(subset_data_server)
#The difference between the ResultTime and the FirstScanTime is the turn around time:
df1$TS_start <- paste(df1$FirstScanDate, df1$FirstScanTime)
df1$TS_end <- paste(df1$ResultDate, df1$ResultTime)
df1$TAT <- difftime(df1$TS_end,df1$TS_start,units = "mins")

On my R Markdown document I works good. I hope this give the same result on the Shiny app. Now the data is kind of prepared I would like to make a histogram/barplot to give the amount of samples over time (1 hour interval) and to use a dropdown menu to select an extra filter. eg: show the histogram, representing the amount of sample each hour for $Source(location) A. Another example is show the histogram, representing the amount of sample each hour of $test IGF...

I used following code to make a barplot of the amount of samples each hour of all the samples in r markdown:


par(mar = c(4, 4, .1, .1))
ggplot(data = df_aggr_First_scan,
       mapping = aes(x = df_aggr_First_scan$hour, y = df_aggr_First_scan$`amount of samples`)) +
  geom_bar(stat="identity", boundary = 0, color="blue", fill="lightblue") +
  labs(title="amount of first-scan times over the day",x="hours", y = "amount of samples") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(limits = c(-0.5, 24), breaks = seq(0, 23, 1))

ggplot(data = df_aggr_Result,
       mapping = aes(x = df_aggr_Result$hour, y = df_aggr_Result$`amount of samples`)) +
  geom_bar(stat="identity", boundary = 0, color="blue", fill="lightblue") +
  labs(title="amount of results over the day",x="hours", y = "amount of samples")+ 
  theme(plot.title = element_text(hjust = 0.5)) + 
  scale_x_continuous(limits = c(-0.5, 24), breaks = seq(0, 23, 1))

But now before making the histogram, I should create a filter on the data , based on the selectInput variable. I tried already some codes, but the aggregate seems the give an error, since It cannot find any rows?

library(lubridate)

selectInput("Validation source", label="Who/What performed the validation:",
            choices = df1$ValidationUser, selected = "~SYSValDaemon~")

Input <- selectInput
df1_filtered <- df1[df1$ValidationUser == 'selectInput']

#Calculate the amount of Result-samples each hour:
df2 <- as.data.frame(hour(hms(df1_filtered$ResultTime)))
df_aggr_Result <- aggregate(df2, by=list(df2$`hour(hms(df1$ResultTime))`), FUN = length)
#Renaming
names(df_aggr_Result)[names(df_aggr_Result) == "Group.1"] <- "hour"
names(df_aggr_Result)[names(df_aggr_Result) == "hour(hms(df1$ResultTime))"] <- "amount of samples"


renderPlot({
  barplot(df_aggr_Result, probability = TRUE,
       breaks = seq(0, 23, 1),
       xlab = "hour",
       main = "amount of samples")

})

It is still a draft script and there is room for (esthetical) improvements. Since it is not really possible to run code chunks (with dynamic renderplots) separately , it takes a lot of time to debug the script. An extra addition could be that first there are radiobuttons on top to chose which colum you would like to filter on (eg $test), under it a drop down menu to chose the value of the filter and under that the histogram.

Hope you can help!

Thanks a lot!

I think you would benefit from rereading shiny introductions and creating some primitive shiny apps along with said tutorials to build some shiny instincts; as shiny does not work in the way you've proceeded here.

What you have done is first create a selectInput, give it an ID "Validation source" that you never refer to again (so will not be accessing its contexts; you then directly assign the generic selectInput creation function to the name Input (and I guess thats fine in the sense that you dont go on to use that anywhere anyway)
finally you are directly check ingif the contents of df1$ValidationUser match the exact literal string "selectInput" which can't be what you intend.

Hi

Thank you! I have already read some tutorials and a lot of them works with the x or y value in the histogram that you adapt directly, but here I have an extra filter step before the setup of the graph.

This is my current code

selectInput(inputId='Validation_source', label='Who/What performed the validation:',
            choices = df1$ValidationUser, selected = '~SYSValDaemon~')

Val_Source <- as.character(reactive({req(input$validation_source())}))

df1_filtered <- df1[df1$ValidationUser == Val_Source()]

Now I am stuck with a 'closure' error and I can't figure it out.

Thanks a lot!

Its super awkward to help you as theres no reprex; as your data is private etc.
a reasonable guess would be

selectInput(inputId='Validation_source', label='Who/What performed the validation:',
            choices = df1$ValidationUser, selected = '~SYSValDaemon~')

Val_Source <- reactive({as.character(req(input$validation_source))])

# and maybe df_1filtered should be reactive

df1_filtered <- reactive({df1[df1$ValidationUser == req(Val_Source())]})

the above assumes df1 is fixed and not reactive

Hi

How can I give you a reprex?

IF you aren't familiar with best practices for shiny reprexes, check out

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.