我在 R Shiny 中遇到问题。
if (length(Fnum)==0) {
print("File does not exists!")
output$errormsg=renderText({"File does not exists! Pause 3min"})
Sys.sleep(180)
}
错误消息显示在控制台中,但未显示在 Shiny 中。但是,当文件存在时,消息会正确显示。很奇怪,我不明白为什么只有在文件不存在时才显示消息。
发生了什么?
谢谢您的帮助!
以下代码是全部;
服务器
library(shiny)
library(shinyFiles)
library(stringi)
function(input, output, session) {
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
shinyDirChoose(input, "directory", roots = volumes, session = session, restrictions = system.file(package = "base"))
output$directorypath <- renderPrint({
if (is.integer(input$directory)) {
cat("No directory has been selected (shinyDirChoose)")
} else {
parseDirPath(volumes, input$directory)
}
})
path1 <- reactive({
return(print(parseDirPath(volumes, input$directory)))
})
mytime<-reactiveValues(inc=0, timer=reactiveTimer(2000), started=FALSE)
observeEvent(input$ab1, {
mytime$started<-TRUE
})
observe({
mytime$timer()
if(isolate(mytime$started)){
mytime$inc<-isolate(mytime$inc)+1
res=0
while (res==0) {
Fnum=grep(paste0("1",stri_trans_nfkc(input$filename)," test.xls"), dir())
if (length(Fnum)==0) {
print("File does not exists!")
output$errormsg=renderText({"File does not exists! Pause 3min"})
Sys.sleep(180)
}else{
dlist=dir()
Fname=dlist[Fnum]
print(Fname)
fn=paste0(path1(),"/",Fname)
output$errormsg=renderText({paste0("File ", Fname," exists!")})
res=1
}
}
print(paste0("Filepath=",fn))
}
})
}
用户界面
library(shiny)
library(shinyFiles)
fluidPage(
titlePanel("Program "),
sidebarLayout(
sidebarPanel(
# Input: Select a directory ----
tags$hr(),
strong("Select directory:"),
br(),
shinyDirButton("directory", "Folder select", "Please select a folder"),
textInput("filename","Input filename. ", ""),
actionButton("ab1","START",class = "btn-warning")
),
mainPanel(
strong("Directory Path:"),
br(),
verbatimTextOutput("directorypath"),
h4(textOutput("errormsg")),
)
)
)
@YBS 对不起,我的解释很糟糕。该程序与另一个程序相关。这是为了分析另一个程序产生的数据。所以,这个程序需要等到另一个程序的文件产生。因此,通过使用 mytime ,该程序将重复直到找到该文件。但是,通过如下修改,我得到了预期的结果。再次感谢您的建议。
server <- function(input, output, session) {
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
shinyDirChoose(input, "directory", roots = volumes, session = session, restrictions = system.file(package = "base"))
output$directorypath <- renderPrint({
if (is.integer(input$directory)) {
cat("No directory has been selected (shinyDirChoose)")
} else {
parseDirPath(volumes, input$directory)
}
})
path1 <- reactive({
return(print(parseDirPath(volumes, input$directory)))
})
mytime<-reactiveValues(inc=0, timer=reactiveTimer(200), started=FALSE)
#observeEvent(input$ab1, {
observe({
if (input$ab1==0){
return(NULL)
}else{
mytime$started<-TRUE
# }
# })
#
# observe({
mytime$timer()
if(mytime$started){
mytime$inc<-isolate(mytime$inc)+1
#res=0
#while (res==0) {
req(input$filename)
if (is.null(input$filename)) {
return(NULL)
}else{
userfile <- paste0(path1(),"/",input$filename)
if (file.exists(userfile)) {
fn=paste0("Filepath=",path1(),"/",input$filename)
output$errormsg <- renderText({
if (file.exists(userfile)) {
paste0("File ", input$filename," exists!")
}else return(NULL)
})
#res=1
}else {
print("File does not exist!")
output$errormsg=renderText({
if (file.exists(userfile)) {
return(NULL)
}else{
paste0("File ", input$filename," does not exist!")
}
})
fn=paste0("No file named ", input$filename, " in ", path1()," folder")
}
}
#}
print(paste0(fn))
}
}
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句