How to use reactive functions in .rmd file

rmarkdown
shiny

#1

I am trying to create slides from a webpage using .R and .rmd files.

My server logic code is written in .R which includes some reactive methods.
Started writing corresponding .rmd file for creating slides.
But unable to import reactive methods from server.R to this .rmd file.
Please help me.

Thanks and regards,


#2

RMarkdown is for creating static documents (html, PDF, Word, etc). How are you imagining reactive methods working in a static document? If you want reactive methods I think sticking in Shiny is your only sensical approach... unless I'm misunderstanding.


#3

Agreed, Rmarkdown is for creating static documents.

My requirement is to create presentation slides out of project report.
This project report(html pages) generation is done using shiny where some reactive methods being used.
So currently, I am thinking of extending the same to slides presentation. But only obstacles I am seeing are reactive methods.

How to avoid/replace/reuse reactive things here?

Thanks,


#4

You can rewrite your reactive methods so they look like this:

myReactivePlot = function({

  thePlot = ggplot(...)

})

myActualDrawGraph = renderPlot({

  print(myReactivePlot())

})

And now you can call print(myReactivePlot()) from an RMarkdown document.

I think that's what you meant?


#5

Thanks chris, I am just pasting the code snippets here..

R Program

server <- function(input, output, session) {
  # Create reactive values to pass to download files
  subtaskList.Reactive <- reactive({input$subtaskList})
  subtask.Reactive <- reactive({subtask$Program})
  task.Reactive <- reactive({input$task})
  observeEvent(input$Program,{
  .
  .
 }
ui <- navbarPage(
    title = "IT Portfolio Dashboard",
  # Define elements within task Dashboard tab    
  tabPanel("Project Dashboard",           
           includeCSS("LDshiny.css"),            
           fluidRow(column(2, downloadButton('TaskReport', "Generate report")), class="download_this_thing"),
	.
	.  
}
#application
shinyApp(ui = ui, server = server)

R Markdown Program (.rmd)

```{r echo=FALSE,warning=FALSE }
backgroundFactors <- c("On Target", "At Risk", "In Trouble", NA)
backgrounColors <- c("#00E100", "#EAFF09", "#FF0009", "#FFFFFF")
options(knitr.table.format = "html")
# Pull Task Level Data
tasklisttemplate <- config$tasklist
tasklisturl <- sprintf(tasklisttemplate)
tasklisturl <- URLencode(tasklisturl)
gettasklist <- fromJSON(tasklisturl, flatten = TRUE, simplifyDataFrame = TRUE)

# Pull Milestone data
milestonestemplate <- config$milestones
# Pull task Risks
taskrisktemplate <- config$taskrisks

taskdata <- getprojectlist$data[gettasklist$data$name ==  task.Reactive(), ]

this is where I am getting error

ERROR

Error in task.Reactive() : could not find function "task.reactive" Calls:
<Anonymous> ... withcallingHandlers -> withVisible -> eval -> eval -> [ ->[.data.frame..

#6

I wonder why it says "task.reactive" with a small case "R".

Have you tried making a minimal example? I'm looking at some code I wrote right here that uses several reactive functions without difficulty. You are running this from within Shiny, I haven't misunderstood?


#7

Sorry for the typo, I couldn't copy the message, so while typing it, small case appeared.

Yes, I tried to make minimal example.

And initially started using through shiny part for making webpages. Thats running successfully.

Later same code, I want to extend for making slides using R Markdown.
I am not using shiny in .Rmd, But would like to use those, server & ui code.

thanks,


#8

Oh, I see. I really don't think that will work at all. Not at all.

You could write a shiny app that writes the documents for you though, that would work? Just port the functions across and then have a button to generate the RMarkdown. I'm not sure you're saving a lot of time or effort, but you can certainly do that if you want.