Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Managing complicated control flow #83

Open
CedricMidoux opened this issue Feb 19, 2020 · 2 comments
Open

Managing complicated control flow #83

CedricMidoux opened this issue Feb 19, 2020 · 2 comments

Comments

@CedricMidoux
Copy link

CedricMidoux commented Feb 19, 2020

Hello and thank you for your package.

In this example, what is the correct way to write the condition inside metaRender for a better display with the Show code button.

library(shiny)
library(shinymeta)
library(ggplot2)

ui <- fluidPage(
  
  titlePanel("Old Faithful Geyser Data"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      radioButtons("column",
                   "Select column:",
                   choices = c("eruptions", "waiting"))
    ),
    
    mainPanel(
      outputCodeButton(plotOutput("plot"))
    )
  )
)

server <- function(input, output) {
  
  output$plot <- metaRender(renderPlot, {
    p <- ggplot(faithful, aes_string(..(input$column)))
    p <- p + geom_histogram(bins = ..(input$bins))
    if (..(input$bins<=30)) {
      p <- p + theme_dark()
    } else {
      p <- p + theme_bw()
    }
    p
  })
 
}

shinyApp(ui = ui, server = server)

Here, the Show code button show :

library(ggplot2)
p <- ggplot(faithful, aes_string("eruptions"))
p <- p + geom_histogram(bins = 30L)
if (TRUE) {
  p <- p + theme_dark()
} else {
  p <- p + theme_bw()
}
p

I would like it to simply display:

library(ggplot2)
p <- ggplot(faithful, aes_string("eruptions"))
p <- p + geom_histogram(bins = 30)
p <- p + theme_dark()
p

Many thanks in advance for your support!

@cpsievert
Copy link
Collaborator

cpsievert commented Feb 20, 2020

There are a couple different ways to currently approach this problem. The most straight-forward would be to duplicate code in each case (just make sure to that you're always returning a metaExpr():

output$plot <- metaRender2(renderPlot, {
    if (input$bins<=30) {
      metaExpr(ggplot(faithful, aes_string(..(input$column))) + geom_histogram(bins = ..(input$bins)) + theme_dark())
    } else {
      metaExpr(ggplot(faithful, aes_string(..(input$column))) + geom_histogram(bins = ..(input$bins)) + theme_bw())
    }
  })

Clearly that's not ideal if there's lots of code that needs duplicating. A more sophisticated approach that avoids duplication might be through clever use of quote() and eval():

output$plot <- metaRender2(renderPlot, {
    my_theme <- if (input$bins<=30) quote(theme_dark()) else quote(theme_bw())
    metaExpr({
      p <- ggplot(faithful, aes_string(..(input$column)))
      p <- p + geom_histogram(bins = ..(input$bins)) 
      p + eval(..(my_theme))
    })
  })

This generates valid code, but it's also not ideal in that it generates code that has an unnecessary eval() in it (e.g., p + eval(theme_dark())).

I don't immediately see an approach that both avoids duplication and generates "perfectly human-like" code...maybe @jcheng5 can think of a better alternative?

@cpsievert cpsievert reopened this Feb 20, 2020
@rstudio rstudio deleted a comment from CedricMidoux Feb 20, 2020
@cpsievert cpsievert changed the title Condition inside metaRender Managing complicated control flow Feb 20, 2020
@CedricMidoux
Copy link
Author

Hello,
Do you have a new approach for this problem?
Thank you in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants