為你的應用程式加入版面結構
Shiny 的版面配置可用來為你的應用程式建立結構,將各元素放在你想要的位置。
使用 sidebarLayout() 函式建立的「側邊欄版面」,提供基本的兩欄結構:左側較小的側邊欄,右側較大的主面板。
側邊欄版面函式需要兩個引數:sidebarPanel() 與 mainPanel()。這兩個面板都可以包含任意組合的文字或 HTML 元素,方式就像你在 fluidPage() 中混合這些元素一樣。
本練習屬於課程
案例研究:使用 R 的 Shiny 建置網頁應用程式
練習說明
你的任務是為現有的應用程式加入側邊欄版面,讓輸入位於左側,輸出放在主面板中。你需要:
- 定義 Shiny 應用程式的 UI。
- 在頁面中加入側邊欄版面配置。
- 在版面中加入側邊欄面板,並把輸入與文字放入其中。
- 在版面中加入主面板,並把圖表與表格放入其中。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load the shiny package
library(shiny)
# Define UI for the application
ui <- ___(
# Add a sidebar layout to the application
___(
# Add a sidebar panel around the text and inputs
___(
h4("Plot parameters"),
textInput("title", "Plot title", "Car speed vs distance to stop"),
numericInput("num", "Number of cars to show", 30, 1, nrow(cars)),
sliderInput("size", "Point size", 1, 5, 2, 0.5)
),
# Add a main panel around the plot and table
___(
plotOutput("plot"),
tableOutput("table")
)
)
)
# Define the server logic
server <- function(input, output) {
output$plot <- renderPlot({
plot(cars[1:input$num, ], main = input$title, cex = input$size)
})
output$table <- renderTable({
cars[1:input$num, ]
})
}
# Run the application
shinyApp(ui = ui, server = server)