[英]Run system command in R Shiny
我具有以下服务器功能,该功能从ui的inputFile选项卡中获取两个输入文件。
library(shiny)
ui <- fluidPage(
fileInput("CF", label = "CF"),
fileInput("ED", label = "ED"),
actionButton("Run", "Run")
)
server <- function(input, output, session) {
cf_file <- reactive({
cfFile <- input$CF
return(cfFile$datapath)
})
ed_file <- reactive({
edFile <- input$ED
return(edFile$datapath)
})
table_content <- eventReactive(input$Run, {
req(input$ED$datapath)
req(input$CF$datapath)
file_ed <- ed_file()
file_cf <- cf_file()
##the system command uses external program which takes input files (file_cf and file_ed) from fileInput. The command will look like:
#/bin/qt con ed -i file_cf -p file_ed > file_ed.db#
system(paste("/bin/qt con ed -i", file_cf, "-p", file_ed, ">", file_ed,".db" ))
})
}
shinyApp(ui, server)
我这里有两个问题:
1)系统命令在这里不起作用,这会导致错误:
qt: Error reading file '/var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpISFd3V/aac5eff9961beb644d8ec5e0/0.phe': End of file
系统命令获取两个输入文件,并通过在输入文件之一中添加“ .db”来写入输出文件。 谁能指出上面的系统命令中有什么问题?
我已经在https://community.rstudio.com/t/system-call-within-r-shiny/11405/3
进行了类似的查询
2)如果系统命令有效,我如何将输出文件中的输出呈现为table_content?
尝试与错误:我尝试使用paste0()
但没有运气,这次出现了另一个错误,即它不读取输入文件file_cf和file_ed。
system(paste0("/bin/qt con ed -i ", file_cf, " -p ", file_ed, " > ", file_ed,".db"))
`Error accessing file '/var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpjqfSir/6a3263bc18297e4b6567979e/0.cf -p/var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpjqfSir/02b964d65d0f0b1a9476a8be/0.ed': No such file or directory`
然后使用system2:
system2(paste0("/bin/qt con ed -i ",file_cf, " -p ", file_ed, " > ", file_ed,".db" ))
sh: /bin/qt con ed -i /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpjqfSir/ebc57ae122e171f074281112/0.cf -p /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpjqfSir/15a75f8fbe5992bd82ab8a22/0.ed > /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmpjqfSir/15a75f8fbe5992bd82ab8a22/0.ed.db: No such file or directory
Warning in system2(paste0("/bin/qt con ed -i ", :
error in running command
由R生成的上述sh:命令在R外部的shell中运行时,会使用它运行的tmp路径,并且可以在tmp路径中看到输出文件。 但是,当使用system
和paste0
在R中运行它时,会出现上述错误。
我不知道您使用哪个文件作为输入,因此我只是使用图像并查看即将出现的系统命令,如下所示:
/bin/qt con ed -i C:\Users\**\AppData\Local\Temp\RtmpohWHcF/08b770ef9620ce852841b792/0.png -p C:\Users\**\AppData\Local\Temp\RtmpohWHcF/b2fdf736bf904194d709b66c/0.png > C:\Users\**\AppData\Local\Temp\RtmpohWHcF/b2fdf736bf904194d709b66c/0.png .db
问题在该行的结尾,因为.db
之前有一个空格。 我不知道这是否可以解决您的问题,但这可能是第一步;)
因此,只需尝试以下命令:
paste0("/bin/qt con ed -i ", file_cf, " -p ", file_ed, " > ", file_ed,".db")
它使用paste0
,它不包含空格,因此您将它们包含在字符串部分中。 但是,system命令的最后一个输入将具有双扩展名(例如.png.db
)。 要摆脱这种情况,您可以使用:
file_ed2 <- tools::file_path_sans_ext(file_ed)
并将其分配给最后一个系统cmd输入。
如果仍然一点儿也不炒锅,你也可以尝试一下system2
的命令insteand system
。 有时候对我来说效果更好。
要回答问题2 ,我需要知道它是什么样的输出以及如何加载和呈现它? 但无论如何,也许reactivePoll
函数可能在这里起作用。
-应用程式
library(shiny)
ui <- fluidPage(
fileInput("CF", label = "CF"),
fileInput("ED", label = "ED"),
actionButton("Run", "Run"),
verbatimTextOutput("code")
)
server <- function(input, output, session) {
cf_file <- reactive({
cfFile <- input$CF
return(cfFile$datapath)
})
ed_file <- reactive({
edFile <- input$ED
return(edFile$datapath)
})
table_content <- eventReactive(input$Run, {
req(input$ED$datapath)
req(input$CF$datapath)
file_ed <- ed_file()
file_cf <- cf_file()
file_ed2 <- tools::file_path_sans_ext(file_ed)
paste0("/bin/qt con ed -i ", file_cf, " -p ", file_ed, " > ", file_ed2,".db")
})
output$code <- renderText({
req(table_content())
table_content()
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.