0

I am trying to render a .Rmd file from a shiny app. I am using the shiny app so filter the data set the way I want and then use the filtered data to create a report using rmarkdown.

This is what I have tried:

# load packages
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(rmarkdown)
library(readxl)
library(dplyr)
library(tidyr)
library(stringr)
library(DT)

# data processing
source("data cleaning.R", echo = FALSE)

# Define UI
ui <- dashboardPage(

  dashboardHeader(title = "Student Wellbeing Dashboard",
                  titleWidth = 400),

  dashboardSidebar(width = 400,
                   sidebarMenu(                                               
                     checkboxGroupButtons(inputId = "select_sections", 
                                          label = "Select Sections", choices = unique(dictionary$Dependent),
                                          individual = TRUE, 
                                          checkIcon = list(yes = tags$i(class = "fa fa-circle", 
                                                                        style = "color: steelblue"), 
                                                           no = tags$i(class = "fa fa-circle-o", 
                                                                       style = "color: steelblue")))
                   )
  ),

  dashboardBody(
    tags$head(
      tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
    ),

    fluidRow(
      dataTableOutput("data_out")
    )
  )
)

# Define server logic
server <- function(input, output) {

  filtered_data <- reactive(
    merged %>%
      select(-Question.Id, -Answer.Id) %>%
      filter(Dependent %in% input$select_sections)
  )

  output$data_out <- renderDataTable(filtered_data())

  epp_data <- filtered_data()

  rmarkdown::render("markdown.Rmd", output_file = "html_document",
                    params = list(data = epp_data)
  )

}

# Run the application 
shinyApp(ui = ui, server = server)

The source file data cleaning.R reads to datasets and merges them together named merged.

This is the error I am getting:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  54: stop
  53: .getReactiveEnvironment()$currentContext
  52: getCurrentContext
  51: .dependents$register
  50: filtered_data
  49: server [E:/Projects and porfolio/Projects/fiverr/project6-update student health/health-report/abawbawbr.R#55]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

2 Answers2

0

Try modify output$data_out <- renderDataTable(filtered_data)
into output$data_out <- renderDataTable(filtered_data()) ?

Jrm_FRL
  • 1,394
  • 5
  • 15
  • It was a typo. The `renderDataTable()` part works perfectly. The problem I am facing is from `rmarkdown` part. – Wahiduzzaman Khan May 13 '20 at 10:01
  • and what about skipping the intermediate `epp_data` and call `render("markdown.Rmd", output_file = "html_document", params = filtered_data())` ? Your problem reminds me something similar due to assign a new variable to a reactive one. – Jrm_FRL May 13 '20 at 10:05
  • Was this sorted out? It would be nice to place some closing comments or accept an answer. – Lazarus Thurston Dec 13 '21 at 17:01
0

This is what worked for me. I replaced this part of the code:

rmarkdown::render("markdown.Rmd", output_file = "html_document",
                    params = list(data = epp_data)
  )

with this:

render_markdown <- function(){
        rmarkdown::render("markdown.Rmd",
                          output_format = html_document(),
                          output_file = "report",
                          params = list(data = reactive(filtered_data()))
        )
    }

render_markdown()

And now everything seems to work just fine.