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