shinyapp 中的交互式文件输入和反应式阅读
Interactive file input and reactive reading in shinyapp
我想制作一个 shinyapp,它集成了选择文件(在选定文件夹中)的功能,(如 ), and then reading it reactively by detecting changes in it, as in https://gist.github.com/wch/9652222。
但是,我可以不让 reactiveFileReader 函数与反应文件名(文件夹)一起工作。它似乎只与一个预先确定的文件名一起工作。
在这个应用程序中,文件预计会在选择后自动选择任何文件夹(按钮),前提是它在里面并且与子文件夹同名且扩展名.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)
我想到了这个解决方案。已测试从外部修改 /folder/folder.csv 文件。
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)
我想制作一个 shinyapp,它集成了选择文件(在选定文件夹中)的功能,(如
但是,我可以不让 reactiveFileReader 函数与反应文件名(文件夹)一起工作。它似乎只与一个预先确定的文件名一起工作。
在这个应用程序中,文件预计会在选择后自动选择任何文件夹(按钮),前提是它在里面并且与子文件夹同名且扩展名.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)
我想到了这个解决方案。已测试从外部修改 /folder/folder.csv 文件。
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)