本节概述了闪亮的内容以及开发人员可能想要使用它的原因。
它还应该提到闪亮的任何大型主题,并链接到相关主题。由于文档闪亮是新的,您可能需要创建这些相关主题的初始版本。
包括在你的shinyApp地块的最简单的方法是使用plotOutput
在UI和renderPlot
在服务器中。这将适用于基本图形以及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)
DT包中最容易包含表, DT包是JavaScript库DataTables的R接口。
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存储库托管Shiny的开发版本,与CRAN版本相比可能具有更多功能,但它也可能不稳定。 每个shiny
应用程序包含两个部分:用户界面定义( UI
)和服务器脚本( server
)。此示例显示如何从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')
shinyApp(ui,server)
来运行您的应用程序结果
在此示例中,您将看到一些文本和一个按钮:
按下按钮后服务器响应: