How to use reactiveFileReader with a reactive filePath?

1,023 views
Skip to first unread message

Ted Mendum

unread,
Nov 7, 2015, 3:25:09 PM11/7/15
to Shiny - Web Framework for R
Hi All,

I would like to read (and periodically poll) a log file that the user has selected through the gui (using, e.g., choose.files()).  Unfortunately, the filePath argument to reactiveFileReader does not allow the use of reactives.  So something like

file_contents <- reactiveFileReader(1000, session, filePath = myfile(), readFunc = scan, what = "character", sep = "\n")

where myfile() is some reactive that returns the file name as a  string will fail with the message:

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

How can I get a value for the filePath which is chosen by the user but is not a reactive? 

Some constraints on acceptable solutions: 
1) The name of the log file will change every time the app is used. 
2) The user can't type the log file name in using textInput or the like because I am generating the file dynamically, nor do I want to deal with potential typos. 

A reproducible example is below. 

library(shiny)

ui <- fluidPage(
    headerPanel("Reactive File Reader"),
    sidebarPanel(
        actionButton("pick_file", "Pick File"),
        verbatimTextOutput("pick_file")
    ),
    mainPanel(
        textOutput("file_contents")
    )
)

server <- function(input, output, session){
    file_name <- eventReactive(
        input$pick_file,
        file.choose()
    )

   
    output$pick_file <- renderText(
        file_name()
    )


    file_contents <- reactiveFileReader(1000, session, filePath = file_name(),
                                         readFunc = scan, what = "character",sep = "\n")
   
    output$file_contents <- renderText(file_contents())
   
}

shinyApp(ui = ui, server = server)

Thanks,

Ted

Peter Andrews

unread,
Apr 5, 2017, 7:32:48 PM4/5/17
to Shiny - Web Framework for R
I know this is very old but for those interested... the myfile does not need to a reactive but simply a function that returns the current filePath. Because the reactiveFileReader runs at an interval it will call the function every time so there is no need for myfile to be a reactive. Key idea is that rather than passing a value you pass a function to get that value. That level of indirectness makes it work. 

Joe Cheng

unread,
Apr 6, 2017, 1:47:39 PM4/6/17
to Peter Andrews, Shiny - Web Framework for R
True! And in fact, the interval-based checking is itself done in an observer, so I believe that if your file-path-returning function does access reactive inputs/values/expressions, the checking will happen as soon as those reactives change, not waiting for the interval to elapse.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/f3fd00af-b6e8-4d08-a727-7a5e1ad1c535%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

andrew.r...@accumula.com

unread,
Sep 21, 2017, 4:17:57 PM9/21/17
to Shiny - Web Framework for R
Is there anyway to stop the reactive file reader after its use is finished? I am having an issue stopping it as it triggers my conditional shiny-busy panel.

Sanjay Mehrotra

unread,
Aug 17, 2022, 8:11:21 AM8/17/22
to Shiny - Web Framework for R
Hi Joe,
Just stumbled across this old response of yours that pleasantly surprised me. Whenever I tried a reactive file path with filereader in shiny it never worked. But as you explain it must work. 
Are you confirming the below statement or are there any qualifiers?
thanks a lot.
Rgds
Sanjay
(Shiny R developer)

Joe Cheng replied: 
True! And in fact, the interval-based checking is itself done in an observer, so I believe that if your file-path-returning function does access reactive inputs/values/expressions, the checking will happen as soon as those reactives change, not waiting for the interval to elapse.

Joe Cheng

unread,
Aug 17, 2022, 8:17:20 AM8/17/22
to Sanjay Mehrotra, Shiny - Web Framework for R
Yes, should still be accurate!

Reply all
Reply to author
Forward
0 new messages