Interactive file input and reactive reading in shinyapp

拈花ヽ惹草 提交于 2019-12-11 07:59:27

问题


I would like to make a shinyapp that integrates the functionality of choosing a file (in a selected folder), (like in Interactive directory input in Shiny app (R)), and then reading it reactively by detecting changes in it, as in https://gist.github.com/wch/9652222.

However, I could not make the reactiveFileReader function work with a reactive filename (folder). It seems to work only with one pre-determined filename.

In this app, the file is expected to be chosen automatically after chosing any folder (button), provided that it is inside and has the same name of the subfolder and the extension .csv
For example, if you choose /home/name/folder, the selected file should be /home/name/folder/folder.csv
Below is a code without the expected functionality. It is showing an example file.

server<- function(input, output, session) { 
  shinyDirChoose(input, 'dir', roots = c(home = path1) )
  reacdir <- reactive(input$dir)
  output$dirtext <- renderPrint(c(path(),current() ) )
  path1<-"~"
  path <- reactive({
    home <- normalizePath(path1)
    file.path(home, paste(unlist(reacdir()$path[-1]), collapse = .Platform$file.sep))
  })

  current<-reactive({
    a<-sub('.*\\/', '', path() )  
    b<-paste("current subdir:",a)
  })

#  logfilename<- reactive({filename<-paste0(path(),"/",sub('.*\\/', '', path() ),".csv")})
   logfilename <- paste0('logfile',
                         floor(runif(1, 1e+05, 1e+06 - 1)),".txt")
  #    

  logwriter <- observe({
    invalidateLater(1000, session)
    cat(as.character(Sys.time()), '\n', file = logfilename,
        append = TRUE)
  })

  fileReaderData <- reactiveFileReader(500, session,
                                       logfilename, readLines)
 # when using logfilename(): You tried to do something that can only be done from inside a reactive expression or observer.

  # Also not working: fileReaderData <- reactive({file<-reactiveFileReader(500, session,
  #                                       logfilename(), readLines) })

  output$fileReaderText <- renderText({
    text <- fileReaderData()
    length(text) <- 14
    text[is.na(text)] <- ""
    paste(text, collapse = '\n')
  })
}
ui<-fluidPage(
  titlePanel("interactive selection of file and reactive reading"),
  fluidRow(
    column(12,
           shinyDirButton("dir", "1. Choose directory", "Upload")
           ,br(),br(),
            p("This app has a log file which is appended to",
             "every second.")
    )
  ),
  fluidRow(
    column(6, wellPanel(
      verbatimTextOutput("fileReaderText")
    ))
  )
)

shinyApp(ui, server)   


回答1:


I came to this solution. Tested modifying the /folder/folder.csv file externally.

library(shiny)
library(shinyFiles)
path1<-"~"
server<- function(input, output, session) { 
  shinyDirChoose(input, 'dir', roots = c(home = path1) )
  reacdir <- reactive(input$dir)
  output$dirtext <- renderPrint(c(path(),current() ) )

  path <- reactive({
    home <- normalizePath(path1)
    file.path(home, paste(unlist(reacdir()$path[-1]), collapse = .Platform$file.sep))
  })

  current<-reactive({
    a<-sub('.*\\/', '', path() )  
    b<-paste("current subdir:",a)
  })
  reac<-reactiveValues()
    observe({
        if(file.exists(paste0(path(),"/",sub('.*\\/', '', path() ),".csv")) ){
      fileReaderData<-reactiveFileReader(1000, session, paste0(path(),"/",sub('.*\\/', '', path() ),".csv"), read.csv, stringsAsFactors=FALSE)
      reac$df<-fileReaderData()
      output$fileReaderText <- renderText({
        text <- reac$df
        length(text) <- 14
        text[is.na(text)] <- ""
        paste(text, collapse = '\n')
    })
    }
    else{"index file does not exist, create with button 2."}
    }
  ) 
}
ui<-fluidPage(
  titlePanel("interactive selection of file and reactive reading"),
  fluidRow(
    column(12,
           shinyDirButton("dir", "1. Choose directory", "Upload")
           ,br(),br(),
           p("shinyapp")
    )
  ),
  fluidRow(
    column(6, wellPanel(
      verbatimTextOutput("fileReaderText")
    ))
  )
)

shinyApp(ui, server)  


来源:https://stackoverflow.com/questions/43743596/interactive-file-input-and-reactive-reading-in-shinyapp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!