如何正确创建 r 代码的 exe

How to create an exe for r code correctly

我想让我的用户使用存储在 gist.github.com 上的闪亮应用 library(shiny);runGist("xxx")。 为此,我创建了一个文件夹,其中包含可移植 Chrome、可移植 R 和 2 个文件:

1 - run.R - 包含以下代码:

library(shiny);runGist("xxx")

2 - 应调用 run.r 以便调用 runGist 的 VBScript。

Randomize
CreateObject("Wscript.Shell").Run "R-Portable\App\R-Portable\bin\R.exe CMD BATCH --vanilla --slave run.R" & " " & RND & " ", 0, False

当我点击 VBScript 时没有任何反应,所以我想我遗漏了什么,如何解决这个问题?

更新:点击 run.vbs 后,我得到了一个编号文件,当我在 Notepad++ 上打开它时,我得到以下文本:

Downloading https://gist.github.com/#############/download
NULL
[1] TRUE

Listening on http://127.0.0.1:1337

当我将 http://127.0.0.1:1337 复制到浏览器时,它提供了我想要的内容。 所以问题是如何调用消息中提供的地址的浏览器? - 我注意到每次点击都会给出另一个地址。

从 VBScript 的角度来看,由于您使用的都是相对路径,如果当前活动目录(从中计算相对路径)不是您认为的那样,您可能会遇到问题。

您可以尝试类似(详细版本)

Option Explicit

    'Initialize PRNG
    Randomize 

Dim fso 
    ' As we are dealing with files/folders, it is better to use adecuate functions
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim baseFolder
    ' Retrieve the folder where the current script is stored
    baseFolder = fso.GetFile(WScript.ScriptFullName).ParentFolder.Path

Dim RScript
    ' Combine the current folder and file to execute 
    RScript = fso.BuildPath( baseFolder, "run.R" )

Dim R
    ' Combine the current folder and path to the R executable
    R = fso.BuildPath( baseFolder, "R-Portable\App\R-Portable\bin\R.exe" )

Dim command 
    ' compose the command to execute
    command = q(R) & " CMD BATCH --vanilla --slave " & q(RScript) & " " & Rnd

    ' Execute the composed command
    Call WScript.CreateObject("WScript.Shell").Run( command, 0, False )

' Just a helper function to quote the file/folder paths in the command    
Function q( text )
    q = """" & text & """"
End Function

关于您的更新: 请注意,您可以将端口号设置为 runApp() 中的参数 例如:runApp(..., port = 1337)

示例:

n <- 200


# Define the UI
ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  plotOutput('plot')
)


# Define the server code
server <- function(input, output) {
  output$plot <- renderPlot({
    hist(runif(input$n))
  })
}

# Return a Shiny app object
runApp(shinyApp(ui = ui, server = server), port = 1337)

编辑:对 follow-up 的回答:

如果这没有帮助:

library(shiny);runGist(..., launch.browser = TRUE, port = 1337)

试试这个:

library(shiny);runGist(..., port = 1337);browseURL("http://127.0.0.1:1337")

您可以覆盖默认浏览器选项,然后 runGistlaunch.browser 将正常工作。

run.R

#Assuming your portable Chrome's relative path is "Chrome\Application\chrome.exe"
options(browser = normalizePath("Chrome\Application\chrome.exe"));

library(shiny);
runGist("xxx", launch.browser = TRUE);

run.vbs

Set Fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
    'specify CD to start commands from script's parent folder
    WshShell.CurrentDirectory = Fso.GetParentFolderName(WScript.ScriptFullName)

'Start the command as hidden and don't wait
WshShell.Run "R-Portable\App\R-Portable\bin\R.exe CMD BATCH --vanilla --slave run.R NUL", 0, False