Use of reactive variable with if from shiny to R Markdown

19 Views Asked by At

I try to make an application which permits to make a report R Markdown.

The application works and all outputs come out well. But when I click on the button that makes the report it fails. There are no problems with simple reactive variables. But I have error messages indicating that it does not find the function for reactives variables containing if else. These are outputs plot or outputs text.

I share my script and my RMarkdown. I can't explain that it works for simple variable but no for these.

I define all the variables I use in a new environnement which I use in r markdown.

I tried to create first a reactive variable with if else and then create output with it.

Here is my code :

library(shiny)
library(ggplot2)
library(stats)

# #Define server logic

shinyServer(
  function(input, output, session) {
    ## #Update Input
    
    n <- reactive({
      input$n
    })
    
    observe({
      updateSelectInput(session, "nb", choices = 0:input$n)
    })
    
    observe({
      updateSelectInput(session, "n1", choices = 0:input$n)
    })
    
    ## Distribution loi binomiale de parametre n et p0 et p1
    
    p0 <- 1 / 3
    Pd <- 0.375
    p1 <- p0 + Pd * (1 - p0)
    alpha <- 0.05
    
    q <- reactive({
      qbinom(alpha, as.numeric(n()), p0, lower.tail = FALSE)
    })
    
    ## Calcul de beta
    
    beta <- reactive({
      round(pbinom(as.numeric(q()), as.numeric(n()), p1, lower.tail = TRUE), 4)
    })
    power <- reactive({
      power.prop.test(
        n = as.numeric(n()),
        p1 = p1,
        p2 = p0,
        alternative = "one.sided",
        sig.level = 0.05
      )$power
    })
    
    ## Affichage du texte et de la valeur de beta
    
    output$beta <- renderText({
      paste("La potencia de la prueba es de : ",
            round((power() * 100), 2),
            "%  debido al bajo número de catadores.")
    })
    
    ## Affichage sortie alpha
    
    nb <- reactive({
      input$nb
    })
    
    binom_test_result <- reactive({
      binom.test(x = as.numeric(nb()),
                 as.numeric(n()),
                 p0,
                 alternative = "greater")
    })
    
    p_value <- reactive({
      round(binom_test_result()$p.value, 4)
    })
    
    es <- reactive({
      round((binom_test_result()$estimate) * 100, 2)
    })
    
    output$t <- renderText({
      result <- binom_test_result()
      paste("Resultado de la prueba binomial :\n")
    })
    
    ### Option 1
    
    output$texto <- renderText({
      result <- binom_test_result()
      if (result$p.value < 0.2) {
        text <-
          paste(
            "Los productos A y B son significativamente diferentes con un  P-value de : ",
            format.pval(result$p.value)
          )
        
      } else{
        text <-
          paste(
            "Los productos A y B no son significativamente diferentes con un  P-value de : ",
            format.pval(result$p.value),
            "\nEl riesgo de que todavía exista una diferencia es de  :",
            beta()
          )
      }
      return(text)
      
    })
    
    ##### OR
    
    ### Option 2
    
    texto_output <- reactive({
      result <- binom_test_result()
      if (result$p.value < 0.2) {
        text <-
          paste(
            "Los productos A y B son significativamente diferentes con un  P-value de : ",
            format.pval(result$p.value)
          )
      } else {
        text <-
          paste(
            "Los productos A y B no son significativamente diferentes con un  P-value de : ",
            format.pval(result$p.value),
            "\nEl riesgo de que todavía exista una diferencia es de  :",
            beta()
          )
      }
      return(text)
    })
    
    output$texto <- renderText({
      texto_output()
    })
    
    # #Render report on click
    
    report_loc <- reactiveVal()
    observeEvent(input$render_report_button, {
      ## #always write into a new directory
      
      output_dir <- tempfile()
      dir.create(output_dir, showWarnings = FALSE)
      
      ## #store data from shiny in env for report
      
      render_env <- new.env()
      
      
      render_env$beta <- beta()
      render_env$p_value <- p_value()
      render_env$texto <- texto()
      
      
      render(input = "C:/Users/--/sample_markdown_report.Rmd",
             output_dir = output_dir,
             envir = render_env)
      
      # #update report loc to have rendered file
      
      report_loc(list.files(output_dir, full.names = TRUE))
      
    })
0

There are 0 best solutions below