Rで解析:WindowsでShinyアプリをダブルクリックで開くコマンドの紹介

WindowsでShinyアプリをダブルクリックで開くコマンドの紹介です。実行コマンドをRで実行すると指定したフォルダ内に以下のファイルが作成されます。

作成された各ファイルはRまたはテキストエディタなどでも内容を確認できます。RStudioの利用をおすすめします。

利用はフォルダ内の「server.R」と「ui.R」にオリジナルのコマンドを追記して「RunShinyApp.vbs」をダブルクリックすることを想定しています。各ファイルのエンコードはUTF-8を想定しています。

参考までに出力される「server.R」と「ui.R」に何もコマンドを追記せず「RunShinyApp.vbs」をダブルクリックするとブラウザに下記内容が表示されます。なお、表示ボタンを押しても何も動作はしません。

何かの参考になればと考えます。

実行コマンドはwindows 11のR version 4.1.2で確認しています。


実行コマンド

下記コマンドを実行してください。

###ファイルの保存先を指定
library("tcltk")
#初期作業フォルダを取得
OriWd <- getwd()
#&#12501;&#12449;&#12452;&#12523;&#12398;&#20445;&#23384;&#20808;
setwd(paste(as.character(tkchooseDirectory(title = "&#20445;&#23384;&#20808;&#12501;&#12457;&#12523;&#12480;&#12434;&#36984;&#25246;"), sep = "", collapse ="")))

###RunShinyApp.vbs&#12434;&#20316;&#25104;#####
#R.exe&#12398;&#12497;&#12473;&#12434;&#21462;&#24471;
GetComm <- paste0('Set RdoObject = CreateObject("Wscript.Shell")\n',
                     'RdoObject.Run "', file.path(R.home(), "bin", "R.exe", " "),
                     'CMD BATCH --encoding=UTF-8 OpenShiny.R", False')
#&#20445;&#23384;
cat(x = GetComm,
    file = "RunShinyApp.vbs")
########

###OpenShiny.R&#12434;&#20316;&#25104;####
OpenSComm <- iconv(paste0('if (!require("shiny")) {install.packages("shiny")}\n',
                             'shiny::runApp(launch.browser = TRUE)'),
                      from = "", to = "utf-8")
cat(x = OpenSComm,
    file = "OpenShiny.R")
########

###ui.R&#12434;&#20316;&#25104;######
UICode <- iconv(paste0('shinyUI(fluidPage(\n',
                          '#&#20197;&#19979;&#12395;&#12467;&#12540;&#12489;&#12434;&#35352;&#36848;\n',
                          '#&#12486;&#12473;&#12488;&#12467;&#12540;&#12489;\n',
                          'actionButton("action", label = "&#12371;&#12428;&#12399;&#12486;&#12473;&#12488;&#12391;&#12377;")\n',
                          '))'), from = "", to = "utf-8")
#&#20445;&#23384;
cat(x = UICode,
    file = "ui.R")
########

###server.R&#12434;&#20316;&#25104;#####
ServerCode <- iconv(paste0('shinyServer(function(input, output, session){\n\n',
                              '  session$onSessionEnded(function() {\n',
                              '    stopApp()\n',
                              '    q("no")\n',
                              '    })\n\n',
                              '#&#20197;&#19979;&#12395;&#12467;&#12540;&#12489;&#12434;&#35352;&#36848;\n\n})'), from = "", to = "utf-8")
#&#20445;&#23384;
cat(x = ServerCode ,
    file = "server.R")
########

#&#21021;&#26399;&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#25147;&#12377;
setwd(OriWd)

少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました