1

I am making a shiny application where the user specifies the independent variables and as a result shiny displays a time series plot with plotly, where on-however each point shows the selected parameters.

enter image description here enter image description here

If I know the exact number of variables that the user selects, I am able to construct the time series plot without a problem. Let's say there are 3 parameters chosen:

ggp <- ggplot(data = data.depend(), aes(x = Datum, y = y, tmp1 = .data[[input$Coockpit.Dependencies.Undependables[1]]], tmp2 = .data[[input$Coockpit.Dependencies.Undependables[2]]], tmp3 = .data[[input$Coockpit.Dependencies.Undependables[3]]])) + 
     geom_point()
ggplotly(ggp)

where data.depend() looks like enter image description here

and the selected parameters are stored in a character vector enter image description here

So the problem is that for each parameter I want to include in the tooltip, I have to hard code it in the aes function as tmpi = .data[[input$Coockpit.Dependencies.Undependables[i]]]. I would however like to write generic function that handles any amount of selected parameters. Any comment suggestions are welcome.

EDIT: Below a minimal working example:

data.dummy <- data.frame(Charge = c(1,2,3,4,5), Datum = c(as.Date("2020-01-01"),as.Date("2020-01-02"),as.Date("2020-01-03"),as.Date("2020-01-04"),as.Date("2020-01-05")), y = c(4,5,6,4,5), ZuluftTemperatur = c(52,51,54,58,49), Durchflussgeschwindigkeit = c(690, 716,722,710,801), ZuluftFeuchtigkeit= c(3.9,4.1,3.8,3.0,4.9))
ChosenParams <- c("ZuluftTemperatur", "ZuluftFeuchtigkeit", "Durchflussgeschwindigkeit")
ggp <- ggplot(data = data.dummy, aes(x = Datum, y = y,  tmp1 = .data[[ChosenParams[1]]], tmp2 = .data[[ChosenParams[2]]], tmp3 = .data[[ChosenParams[3]]])) + geom_point()
ggplotly(ggp)

Result:

enter image description here

So this works at the "cost" of me knowing the user is choosing three parameters and therefore I write in aes tmpi = .data[[ChosenParams[i]]]; i=1:3. I am interested in a solution with the same result but where I don't have to write tmpi = .data[[ChosenParams[i]]] i-number of times

Thank you!

Rok Bohinc
  • 47
  • 6
  • Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()` and use the `reprex`-package. – mnist Aug 17 '21 at 07:17
  • 1
    I suspect you'd be better off converting your data to long format (`pivot_longer()`) before plotting, but as @mnist says, without a MRE, it's not easy to help. – Limey Aug 17 '21 at 07:24
  • Now with a MRE... – Rok Bohinc Aug 17 '21 at 08:08

1 Answers1

2

One solution is to use eval(parse(...)) to create the code for you:

library(ggplot2)
library(plotly)

data.dummy <- data.frame(Charge = c(1,2,3,4,5), Datum = c(as.Date("2020-01-01"),as.Date("2020-01-02"),as.Date("2020-01-03"),as.Date("2020-01-04"),as.Date("2020-01-05")), y = c(4,5,6,4,5), ZuluftTemperatur = c(52,51,54,58,49), Durchflussgeschwindigkeit = c(690, 716,722,710,801), ZuluftFeuchtigkeit= c(3.9,4.1,3.8,3.0,4.9))
ChosenParams <- c("ZuluftTemperatur", "ZuluftFeuchtigkeit", "Durchflussgeschwindigkeit")

ggp <- eval(parse(text = paste0("ggplot(data = data.dummy, aes(x = Datum, y = y, ",
                                paste0("tmp", seq_along(ChosenParams), " = .data[[ChosenParams[", seq_along(ChosenParams), "]]]", collapse = ", "),
                                ")) + geom_point()"
                                )
                  ))

            
ggplotly(ggp)

Just note that this is not very efficient and in some cases it is not advised to use it (see What specifically are the dangers of eval(parse(...))?). There might also be a way to use quasiquotation in aes(), but I am not really familiar with it.

EDIT: Added a way to do it with quasiquotation.

I had a look a closer look at quasiquotations in aes() and found a nicer way to do it using syms() and !!!:

data.dummy <- data.frame(Charge = c(1,2,3,4,5), Datum = c(as.Date("2020-01-01"),as.Date("2020-01-02"),as.Date("2020-01-03"),as.Date("2020-01-04"),as.Date("2020-01-05")), y = c(4,5,6,4,5), ZuluftTemperatur = c(52,51,54,58,49), Durchflussgeschwindigkeit = c(690, 716,722,710,801), ZuluftFeuchtigkeit= c(3.9,4.1,3.8,3.0,4.9))
ChosenParams <- c("ZuluftTemperatur", "ZuluftFeuchtigkeit", "Durchflussgeschwindigkeit")
names(ChosenParams) <- paste0("tmp", seq_along(ChosenParams))
ChosenParams <- syms(ChosenParams)

ggp <- ggplot(data = data.dummy, aes(x = Datum, y = y, !!!ChosenParams)) + geom_point()
ggplotly(ggp)
Gilean0709
  • 1,098
  • 6
  • 17
  • @RokBohinc Because I was interested in how it works withtout using `eval(parse(...))` I had a closer look at quasiquotations in `aes()` and saw a nicer, cleaner way to do it, see my edit. – Gilean0709 Aug 17 '21 at 09:24
  • Works even better. Where did you dig out the information about syms() and the quasinotation (I assume this is the !!! operator). Were can I learn more about that? – Rok Bohinc Aug 17 '21 at 10:04
  • @RokBohinc In the help pages of `aes()` you find a section called Quasiquotation and in there if you click on quasiquotation you get to a help page "Force parts of an expression" and there I found the information. Or you just search `nse-force` in the R help to get there. – Gilean0709 Aug 17 '21 at 10:18