【问题标题】:Run system command in R Shiny在 R Shiny 中运行系统命令
【发布时间】:2019-03-05 10:47:32
【问题描述】:

我有以下服务器函数,它从 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

上面的 sh: 命令在 R 外部的 shell 中运行时由 R 生成,使用它工作的 tmp 路径,并且可以在 tmp 路径中看到输出文件。但是当它在 R 中使用 systempaste0 运行时,会出现上述错误。

【问题讨论】:

  • 您是否尝试在 www 文件夹中创建文件或尝试使用shiny.rstudio.com/reference/shiny/latest/addResourcePath.html
  • 我尝试了上面显示的方式。它无法读取输入文件并写入输出。你能举一个使用 www 文件夹或 addResourcePath 的例子吗?
  • 输入文件存储在 Temp 文件夹中,这就是为什么你会得到奇怪的路径名。但是查看第二个错误,它将整个命令作为文件。而且我还在 -p 和 >.. 之前看到错误的空格。或者只是从输出中?
  • 只在输出中,命令本身没有空格
  • 我已经用空格更新了system2(paste0()),如上所示,它有同样的错误。它以某种方式正确读取输入文件的文件扩展名,但不正确读取文件名。

标签: r shiny system-calls


【解决方案1】:

我不知道您使用哪些文件作为输入,所以我只使用了图像并查看了输出的系统命令,如下所示:

/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,它不包含空格,因此您将它们包含在字符串部分中。系统命令的最后一个输入将具有双重扩展名(如.png.db)。要摆脱它,您可以使用:

file_ed2 <- tools::file_path_sans_ext(file_ed)

并将其分配给最后一个系统 cmd 输入。

如果还是不行,你也可以试试system2 命令来代替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)

【讨论】:

  • 我试过了,但没有运气,这次出现了一个不同的错误,它没有读取输入文件
  • OP 更新错误。我的情况不需要file_ed2,因为我需要双重扩展。
猜你喜欢
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2011-03-14
  • 2011-11-03
相关资源
最近更新 更多