shinyapp中的交互式文件输入和React式阅读

pkwftd7m  于 2023-06-19  发布在  React
关注(0)|答案(1)|浏览(121)

我想做一个shinyapp,它集成了选择文件的功能(在选定的文件夹中),(如在Interactive directory input in Shiny app (R)中),然后通过检测其中的变化来阅读它,如在https://gist.github.com/wch/9652222中。
但是,我无法使reactiveFileReader函数与React式文件名(文件夹)一起工作。它似乎只适用于一个预先确定的文件名。
在这个应用程序中,该文件预计将被自动选择后,选择任何文件夹(按钮),只要它是内,并具有相同的名称的子文件夹和扩展名.csv
例如,如果选择/home/name/folder,则所选文件应为/home/name/folder/folder.csv
下面是一个没有预期功能的代码。它显示了一个示例文件。

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)

z4iuyo4d

z4iuyo4d1#

我想到了这个解决方案。已测试从外部修改强制文件/folder/folder.csv

library(shiny)
library(shinyFiles)
path1 <- "~"

server <- function(input, output, session) { 
  home <- normalizePath(path1)
  home <- c('home' = home) 

  shinyDirChoose(input, 'dir', roots = home)

  output$dirtext <- renderPrint(current())

  path <- reactive({
    parseDirPath(roots = home, input$dir)
  })

  current <- reactive({
    paste("current subdir:", path())  
  })

  reac <- reactiveValues()

  observeEvent(path(), {
    csv_filename <- paste0(basename(path()), ".csv")

    req(length(file.exists(file.path(path(), csv_filename))) > 0)
    if(file.exists(file.path(path(), csv_filename))) {

      fileReaderData <- reactiveFileReader(1000, session, 
        file.path(path(), csv_filename), 
        read.csv, stringsAsFactors = FALSE)

      reac$df <- fileReaderData()

      output$fileReaderText <- renderText({
        text <- reac$df
        text[is.na(text)] <- ""
        paste(text, collapse = '\n')
      })

    } else {
        "folder_name should have folder_name.csv valid file in it, 2 lines at minimum"
    }
  }) 
}

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"),
      verbatimTextOutput("dirtext")
    ))
  )
)

shinyApp(ui, server)

相关问题