このセクションでは、光沢のあるものの概要と、開発者がそれを使用したい理由について概説します。
また、光沢のある広い対象を言及し、関連するトピックにリンクする必要があります。光沢のあるドキュメントは新しいものなので、それらの関連トピックの初期バージョンを作成する必要があります。
shinyAppにプロットを含める最も簡単な方法は、サーバーのuiとrenderPlot
にplotOutput
を使用することplotOutput
。これはggPlot
と同様にベースグラフィックスでも動作しggPlot
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput('myPlot'),
plotOutput('myGgPlot')
)
server <- function(input, output, session){
output$myPlot = renderPlot({
hist(rnorm(1000))
})
output$myGgPlot <- renderPlot({
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()
})
}
shinyApp(ui, server)
テーブルは、JavaScript パッケージの DataTablesへのRインタフェースであるDTパッケージに最も簡単に含まれています。
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput('myTable')
)
server <- function(input, output, session){
output$myTable <- renderDataTable({
datatable(iris)
})
}
shinyApp(ui, server)
Shinyはローカルコンピュータ上のスタンドアロンアプリケーションとして、複数のユーザー(光沢のあるサーバーを使用)またはshinyapps.ioに光沢のあるアプリケーションを提供できるサーバー上で実行できます。
install.packages("shiny")
、RStudio Githubリポジトリからインストールする場合はdevtools::install_github("rstudio/shiny")
を実行します。 Githubリポジトリは、CRANバージョンと比較してより多くの機能を持つShinyの開発バージョンをホストしていますが、不安定になる可能性もあります。 各shiny
アプリには、ユーザーインターフェイス定義( UI
)とサーバースクリプト( server
)の2つの部分が含まれています。この例は、UIまたはサーバーから "Hello world"を印刷する方法を示しています。
UI.R
UIでは、いくつかのビューオブジェクト(div、入力、ボタンなど)を配置することができます。
library(shiny)
# Define UI for application print "Hello world"
shinyUI(
# Create bootstrap page
fluidPage(
# Paragraph "Hello world"
p("Hello world"),
# Create button to print "Hello world" from server
actionButton(inputId = "Print_Hello", label = "Print_Hello World"),
# Create position for server side text
textOutput("Server_Hello")
)
)
Server.R
サーバースクリプトでは、データを操作したり、アクションをリッスンするメソッドを定義できます。
# Define server logic required to print "Hello World" when button is clicked
shinyServer(function(input, output) {
# Create action when actionButton is clicked
observeEvent(input$Print_Hello,{
# Change text of Server_Hello
output$Server_Hello = renderText("Hello world from server side")
})
})
実行する方法?
アプリはいくつかの方法で実行できます:
runApp('your dir path')
使用しrunApp('your dir path')
shinyApp(ui,server)
を使用してアプリを実行することができます結果
この例では、テキストとボタンが表示されます。
ボタンをクリックすると、サーバーが応答します。