0

我正在尝试从闪亮的应用程序呈现 .Rmd 文件。我正在使用闪亮的应用程序,因此按照我想要的方式过滤数据集,然后使用过滤后的数据使用 rmarkdown 创建报告。

这是我尝试过的:

# 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)

源文件data cleaning.R读取数据集并将它们合并在一起,命名为merged.

这是我得到的错误:

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.)
4

2 回答 2

0

尝试修改output$data_out <- renderDataTable(filtered_data)
output$data_out <- renderDataTable(filtered_data())?

于 2020-05-13T09:49:22.110 回答
0

这对我有用。我替换了这部分代码:

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

有了这个:

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

render_markdown()

现在一切似乎都运行良好。

于 2020-05-14T08:50:39.647 回答