shiny change data frame and related values with action Button

Hi,
I developed a reporting app with shiny for my hospital.
I am not a programmer, I am a hand surgeon :slight_smile:
Everything runs fine, no with the new year I want to switch between old year( Last period) and new year (actual period) in the same app.
Calculations and plots for a period always include actual year and previous year. 2017 and 2018 for 2018, 2018 and 2019 for 2019
There are a lot of calculations and variables done outside the main app. I put all data-frames and variables in two lists. Consequently there are two lists with calculations for 2018, y_prev ( with data from 2018 and 2017) and 2019 , y_pres (data from 2019 and 2018).
I load one list and from the list the variables and data frames are set
fall <- y_pres$fall. ### data frame
df.amb <- y_pres$df.amb ## data frame
df.klinik <- y_pres$df.klinik
df.stat <- y_pres$df.stat
bmon <- y_pres$bmon ## variable
br <- y_pres$br ## variable
off <- y_pres$off. ## data frame
solist <- y_pres$solist
akt_jahr <- y_pres$j[3]
vor_jahr <- y_pres$j[2]
coln_vor <- paste("J",vor_jahr,sep="")
coln_akt <- paste("J",akt_jahr,sep="")
col_tabs <- c("variable", coln_vor, coln_akt)

Now I defined two action buttons
actionButton("alt","Vorjahr"),
actionButton("akt","Aktuell")
and I want to switch between those two lists and change the values of the variables depending on the list.

How do I achieve this ?
Any other idea how to change variables and data frames for the visualization of the different periods ?

Thanks.
Peter

Sure, you can do this. I'd suggest one change up front, which is to use radioButtons instead of two action buttons; it'll be easier to code and probably be more user-friendly.

What you want to do is create a reactive expression called, say, y_data. This will return either y_pres or y_prev depending on which radio button is selected.

Your radio buttons might look like this:

radioButtons("dataset", "Choose a dataset",
  c("Previous year" = "prev", "Current year" = "pres")
)

And in your server function:

y_data <- reactive({
  if (input$dataset == "prev")
    y_prev
  else if (input$dataset == "pres")
    y_pres
  else
    stop("Unexpected dataset")
})

Then, in your calculations, use y_data() to retrieve the current dataset, for example, y_data()$fall. I'm assuming that all of those retrievals are happening in a reactive expression or an output. If not, you'll need to restructure your app to do so. These two videos should be very helpful to you if you are having trouble understanding reactive programming: https://www.rstudio.com/resources/videos/effective-reactive-programming/

2 Likes

Thanks a lot, Joe.
This is exactly what I am looking for. Will implement it this evening.
Peter

Hi. I am desperated.
Included this code at the beginning of server part and the respective code in UI

y_data <- reactive({
                if (input$yearset == "prev")
                        y_prev
                else if (input$yearset == "akt")
                        y_akt
        })

e.g. output is then generated by

  • filtering for departement (abt)
  • plotting with a plotter function loaded from function.R, because I need it several times.
output$pstat <- renderPlot({
df <- y_data()$df.stat %>% filter(abt==input$var1)
                plotter_abt(df,mon, drg, "Eff_Gewichte")
plotter_abt <- function(dd,xname, yname, ybez) {
        x_var <- enquo(xname)
        y_var <- enquo(yname)
        ggplot(dd, aes(x = !!x_var, y = !!y_var, group=jahr, colour=jahr))+ 
                geom_point() + geom_line() + theme_bw() + 
                guides(colour = FALSE, alpha = FALSE, size = FALSE) + 
                ggtitle(ybez, "Vorjahr (schwarz)") + labs(x = "Monat", y = ybez) + 
                scale_x_continuous(breaks = y_data()$br) + scale_y_continuous(limits = c(0, NA))
}

I always get:
Error: couldn't find y_data

and very strange in on part of my program one table is rendered correct:

output$koff <- renderTable(y_data()$off,spacing = "xs",striped = TRUE)

and only in this part the output of the table changes , when I change "yearset".
Strange behavior, I can't explain.

I tried nearly everything, even if I put y_data()$df.stat directly anywhere I get errors.
Even if I include the plotting routine directly within renderPlot() it doesn't work.
I study your both videos. I liked them, learned so much, but I didn't find my problems.

Before I decided to implement changes in years everything worked fine.

In plotter_abt, change y_data() to dd. Unless you want it to be the unfiltered version of y_data()? In which case your plotter_abt will need an additional argument.

The general idea is that a generic plotting function like this should take all the data it needs as arguments.

1 Like

Thanks,
no I got it. App is running and change between years works fine.
Peter

This topic was automatically closed 7 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.