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

Deprecate init_chunks #22

Closed
Polkas opened this issue Apr 26, 2022 · 7 comments
Closed

Deprecate init_chunks #22

Polkas opened this issue Apr 26, 2022 · 7 comments
Labels
blocked bug Something isn't working core

Comments

@Polkas
Copy link
Contributor

Polkas commented Apr 26, 2022

init_chunks() and other wrappers functions like chunks_push() are designed to work only in nested shiny namespaces, like teal is built.
If we put the init_chunks and the wrappers to the simply shiny app we will get an error:

Warning: Error in [[: wrong arguments for subsetting an environment
  50: teal.code::init_chunks [/Users/maciejnasinski/NESThub/teal.code/R/chunks.R#976]
  49: server [#3]
Error in `*tmp*`[[session$ns(character(0))]] : 
  wrong arguments for subsetting an environment

This error comes from the fact that session$ns(character(0)) is assumed to never be empty. This is true when init_chunks is used in the nested module so the session$ns(character(0)) will return some string then. The teal apps are used in such nested namespaces so it works correctly nevertheless the simple shiny apps could not use teal.code chunks wrappers.

BTW the init_chunks have useless lines of code, looks like there is a test code leaved when it was developed.

teal.code/R/chunks.R

Lines 976 to 977 in 93b334f

session$userData[[session$ns(character(0))]]$chunks <- "A"
suppressWarnings(rm(envir = session$userData, list = session$ns(character(0))))

1 incorrect and 2 correct apps.

App example for the app with using wrappers and get ERROR.

library(shiny)

ui <- fluidPage(
  titlePanel("Old Faithful Geyser Data"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      verbatimTextOutput("textR")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  init_chunks()
  
  chunky_r <- reactive({
    chunks_push(substitute(set.seed(1234)))
    chunks_push(substitute(hist(rnorm(100), breaks = breaks),
                           env = list(breaks = input$bins)))
    chunks_safe_eval()
  })
  
  output$distPlot <- renderPlot({
    chunky_r()
  })
  
  output$textR <- renderText({
    paste(chunks_get_rcode(), collapse = "\n")
  })
}

shinyApp(ui = ui, server = server)

The example app with init_chunks and additional layer in form of a module. This will work correctly.

library(shiny)

simpleModuleUI <- function(id) {
  ns <- NS(id)
  tagList(
    plotOutput(ns("distPlot")),
   verbatimTextOutput(ns("textR"))
  )
}

simpleModule <- function(id, bins) {
  moduleServer(id,
               function(input, output, session) {
                 ns <- session$ns
                 teal.code::init_chunks()

                 chunky_r <- reactive({
                   chunks_push(substitute(set.seed(1234)))
                   chunks_push(substitute(hist(rnorm(100), breaks = breaks),
                                          env = list(breaks = bins)))
                   chunks_safe_eval()
                 })

                 output$distPlot <- renderPlot({
                   chunky_r()
                 })

                 output$textR <- renderText({
                   paste(chunks_get_rcode(), collapse = "\n")
                 })
               })
}

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
          simpleModuleUI("sth")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  observeEvent(input$bins, {
    simpleModule("sth", bins = input$bins)
  }
  )
}

shinyApp(ui = ui, server = server)

THe app example with chunks used directly and the app is working correctly.

library(shiny)

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    chunky <- teal.code::chunks$new()

 chunky_r <- reactive({
    chunky$push(substitute(set.seed(1234)))
    chunky$push(substitute(hist(rnorm(100), breaks = breaks),
                           env = list(breaks = input$bins)))
   chunky
  })

  output$distPlot <- renderPlot({
    chunky_r()$eval()
  })

  output$textR <- renderText({
    paste(chunky_r()$get_rcode(), collapse = "\n")
  })
}

shinyApp(ui = ui, server = server)

Discussed with @mhallal1 and @nikolas-burkoff

@Polkas Polkas added the core label Apr 26, 2022
@gogonzo gogonzo added the bug Something isn't working label Apr 26, 2022
@insightsengineering insightsengineering deleted a comment from gogonzo Apr 26, 2022
@gogonzo
Copy link
Contributor

gogonzo commented Apr 26, 2022

Example 3 is a way we'd like to go. We should remove get_chunks_object, and remove init_chunks and remove default value from chunks argument in chunks_push, chunks_reset, ... etc.
This leads to big changes in all downstream packages which would require to avoid init_chunks and also needs chunks argument in get_rcode.

It will be decided when we would like to address this issue.

@nikolas-burkoff
Copy link
Contributor

So I agree with @gogonzo although I think it's probably nicer for users to do things like

chunks_safe_eval(chunks = chunky)
chunks_push(substitute(set.seed(1234)), chunks = chunky)

Instead of chunky$<x>

@gogonzo
Copy link
Contributor

gogonzo commented Apr 26, 2022

@nikolas-burkoff yes, we can use chunks_push(<expression>, chunky) equivalently.

@gogonzo
Copy link
Contributor

gogonzo commented May 6, 2022

The scope of this issue is:

  • Deprecate init_chunks
  • Deprecate get_chunks_object

@gogonzo
Copy link
Contributor

gogonzo commented Jul 21, 2022

Candidate to close: new chunks class will have this functionality.

@nikolas-burkoff
Copy link
Contributor

Yup closing these are deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked bug Something isn't working core
Projects
None yet
Development

No branches or pull requests

3 participants