Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny : non-reactive text output

Tags:

r

shiny

I am just trying to write output to a textOutput but I don't want it responding to any reactives - I want total control of when I update the text (for notifications: I want to basically write some log messages based on backend processing to the screen).

If I add: verbatimTextOutput("txt") in my ui.R and then try to do:

observeEvent(input$someButton, {
 ... # do some work
 output$txt <- "some text" #Error: see below
 ... # do some more work
})

I am getting Warning: Unhandled error in observer: Unexpected character output for txt

ADDED: reproducible example:

server <- function(input, output) {
  observeEvent(input$doBtn, {
    #...  do some work
    output$txt <- "some text" #crashes app.
    #... do some more work
  })

  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- shinyUI(fluidPage(
  mainPanel(
    verbatimTextOutput("txt"),
    actionButton("doBtn", "Do something")
  )
))

shinyApp(ui = ui, server = server)

Is it even possible to directly reference a text output element in this way, without wrapping it in its own render* function? I don't think I can take the error message too literally because the text being updated is very simple. And yes I am aware of withProgress() etc but that is not what I want in this case.

Thank you.

like image 858
rstruck Avatar asked Apr 15 '15 18:04

rstruck


1 Answers

If you really want the output to the screen, you can still use renderText within your observeEvent. I included an additional text message to demonstrate the use of htmlOutput in case you don't want that box around the text.

require(shiny)

runApp(
    list(
        ui = pageWithSidebar(
            headerPanel("text test"),
            sidebarPanel(
                p("Demo Page."),
                actionButton("doBtn", "Do something")
            ),
            mainPanel(
                verbatimTextOutput("txt"),
                htmlOutput("text2")
            )
        ),
        server = function(input, output){

            observeEvent(input$doBtn, {
                #... # do some work
                output$txt <- renderText("some text")
                #... # do some more work
            })

            output$text2 <- renderUI({
                HTML("my awesome text message in HTML!!!")
            })

        }
    )
)

On the other hand, if you just need something to print to the console you could just use cat as shown in the documentation of ?observerEvent

observeEvent(input$doBtn, {
        cat("some text")
})
like image 81
cdeterman Avatar answered Oct 03 '22 13:10

cdeterman