# 导入所需的包
library(shiny)
library(shinydashboard)
library(capture)
# 创建Shiny应用
<- dashboardPage(
ui dashboardHeader(title = "Shiny Input示例"),
dashboardSidebar(disable = T),
dashboardBody(
fluidRow(
column(3, textInput("char_input", "字符输入:", value = "")),
column(3, numericInput("num_input", "数值型输入:", value = 0)),
column(3, sliderInput("slider_input", "滑块:", min = 0, max = 100, value = 50)),
column(3, sliderInput("slider_range_input", "滑块选取范围:", min = 0, max = 100, value = c(25, 75))
)
),fluidRow(
column(3, dateInput("date_input", "日期选择:", value = Sys.Date())),
column(3, dateRangeInput("date_range_input", "日期范围选择:", start = Sys.Date() - 7, end = Sys.Date())),
column(3, checkboxInput("checkbox_input", "复选框", value = FALSE)),
column(3, checkboxGroupInput("checkbox_group_input", "复选框组", choices = c("选项1", "选项2", "选项3"), selected = "选项1"))
),fluidRow(
column(3, fileInput("file_input", "文件上传")),
column(3, radioButtons("radio_input", "单选按钮", choices = c("选项1", "选项2", "选项3"), selected = "选项1")),
column(3, selectInput("select_input", "下拉菜单", choices = c("选项A", "选项B", "选项C"), selected = "选项A"))
),fluidRow(
column(12,textOutput("result"),
capture(
selector = "body",
filename = "plot",
icon("camera"), "Take screenshot of all page",
format = "png"
))
)
)
)
<- function(input, output) {
server $result <- renderText({
output# 将输入的结果以文本形式打印
<- paste(
input_text "字符输入:", input$char_input,
"数值型输入:", input$num_input,
"滑块:", input$slider_input,
"滑块选取范围:", input$slider_range_input,
"日期选择:", input$date_input,
"日期范围选择:", paste(input$date_range_input[1], "至", input$date_range_input[2]),
"复选框:", ifelse(input$checkbox_input, "已选中", "未选中"),
"复选框组:", paste(input$checkbox_group_input, collapse = ", "),
"上传文件:", input$file_input$name,
"单选按钮:", input$radio_input,
"下拉菜单:", input$select_input
)
input_text
})
}
shinyApp(ui, server)
Shiny的Input元素有哪些
R语言
Shiny开发
Shiny Input元素一览表
案例演示
参考资料
如果您想进一步探索Shiny的官方文档和资源,以下是一些参考链接:
Shiny官方网站:Shiny的官方网站是一个宝库,提供了丰富的文档、示例和教程,以帮助您入门和深入学习Shiny。
Shiny应用示例库:这个示例库包含了各种Shiny应用的示例,涵盖了不同领域的数据分析和可视化。
RStudio社区:RStudio社区是一个活跃的论坛,您可以在这里提问问题、分享经验和获取帮助。
Shiny教程和文档:Shiny官方网站上的教程和文档提供了深入的指导,帮助您构建更复杂的Shiny应用。