I just discovered the shinymeta
package and tried to use it on a simple app. In this app, you can create a new column which takes the lagged values of an existing column. The name of the new column depends on the input chosen which means I have to use !!
and :=
in the mutate
function.
However, when I try to display the code necessary to remake the modified table (with a new column), shinymeta
seems confused and mixes everything inside mutate
. The code given by shinymeta
works but it is different of what I expected.
- Code expected:
mutate(!!lagged_name := lag(!!sym("mpg")))
- Code obtained :
mutate(`:=`(!!lagged_name, lag(!!sym("mpg"))))
Although the code obtained works, I'm worried it confuses the users when they try to see the code behind the shiny app. Does anyone know how to display a "more usual" code in this example?
PS: since shinymeta
is quite new and not on CRAN yet, I don't know if I should post this here or as an issue on github
Reproducible example:
library(dplyr)
library(shiny)
library(shinymeta)
ui <- fluidPage(
selectInput("choice", "Select a column", choices = c("mpg", "drat", "hp"), multiple = F),
checkboxInput("lag", "Compute lag value"),
outputCodeButton(tableOutput("table"), label = "Show code")
)
server <- function(input, output, session) {
data1 <- metaReactive({
data <- head(mtcars)
data$time <- rep(seq(1:3))
data$ID <- rep(c("A", "B"), each = 3)
data
})
data2 <- metaReactive2({
if (input$lag){
metaExpr({
lagged_name <- paste0(..(input$choice), "_lagged")
..(data1()) %>%
select(ID, time, sym(..(input$choice))) %>%
group_by(ID) %>%
mutate(!!lagged_name := lag(!!sym(..(input$choice))))
})
}
else {
metaExpr({
..(data1()) %>%
select(ID, time, sym(..(input$choice)))
})
}
})
output$table <- metaRender(renderTable, {
..(data2())
})
observeEvent(input$table_output_code, {
code <- expandChain(data1(), data2())
displayCodeModal(code)
})
}
shinyApp(ui, server)