1
votes

I am developing an app in R Shiny where the user can enter or select text which is then used to populate a report, which in turn can be saved as a document. The text should look like:

% Title % Created by % Created 2016-09-16 #

My ui.R code:

library(shiny)
library(hash)
shinyUI(mainPanel(
    textInput("report_author", label="Author")))

server.R code:

library(shiny)
source("generate_report.R", local=TRUE)  
shinyServer(function(input, output) {
output$final_report <- reactive({
    input_report_author <- input$report_author
    input_report_year = ' ' 
    input_report_month = ' ' 
    input_report_panel = ' '  
    renderText({
      make_report(
        author=input_report_author,
        year=input_report_year,
        month=input_report_month,
        panel=input_report_panel)})})})

and generate_report.R:

make_report <- function(panel, author, year, month, date=defaultDate) {
  author = toString(author)
  report_text = paste(
    "% Title\n",
    "% Created by ", author, "\n",
    "% Created ", date, "\n\n",       
    "#", panel, "\n\n",
     sep="")
  return(report_text)}

The text that is returned is not what I was aiming for:

structure(function (...) ,{, if (length(outputArgs) != 0 && !hasExecuted$get()) {, warning("Unused argument: outputArgs. The argument outputArgs is only ", , "meant to be used when embedding snippets of Shiny code in an ", , "R Markdown code chunk (using runtime: shiny). When running a ", , "full Shiny app, please set the output arguments directly in ", , "the corresponding output function of your UI code."), hasExecuted$set(TRUE), }, if (is.null(formals(origRenderFunc))) , origRenderFunc(), else origRenderFunc(...),}, class = "function", outputFunc = function (outputId, container = if (inline) span else div, , inline = FALSE) ,{, container(id = outputId, class = "shiny-text-output"),}, outputArgs = list(), hasExecuted = )

If I replace <- input$report_author' with a string,, I get the same error, so the fault is likely in the output$final_report<-reactive({..}) part of the code.

If I try running the app without the reactive({}) wrapper, I get an error at the input_report_author=input$report_author code segment:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context.

code:

input_report_author = input$report_author
input_report_year = ' ' 
input_report_month = ' ' 
input_report_panel = ' ' 
output$final_report <- renderText({
    make_report(author=input_report_author, year=input_report_year,
        month=input_report_month, panel=input_report_panel)})

Can anyone advise me how to pass in text to a Shiny app and have it return a customized string?

1
You should put input$report_author inside renderText that is a reactive expression. In this case you don't need to put it inside a reactive function.Geovany

1 Answers

0
votes

As @Geovany noted, you shouldn't have to wrap renderText inside reactive. The following single-file app works for me:

library(shiny)

make_report <- function(panel, author, year, month, date=Sys.Date()) {
  author = toString(author)
  report_text = paste(
    "% Title\n",
    "% Created by ", author, "\n",
    "% Created ", date, "\n\n",       
    "#", panel, "\n\n",
     sep="")
  return(report_text)}

ui <- fluidPage(mainPanel(
  textInput("report_author", label="Author"),
  textOutput("final_report")
  )
)

server <- function(input, output) {

  output$final_report <- renderText({
    input_report_author <- input$report_author
    input_report_year = ' ' 
    input_report_month = ' ' 
    input_report_panel = ' '        
    make_report(
      author=input_report_author,
      year=input_report_year,
      month=input_report_month,
      panel=input_report_panel)}
    )
}

shinyApp(ui = ui, server = server)

Output:

enter image description here