Shiny app的输入与输出

输入

在ui.R中运用控件输入value,在server.R中实时调用value进行计算。

ui.R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
动作按钮:
actionButton("action", label = "Action")

提交按钮:
submitButton(text="开始查询", icon("refresh"))

帮助文本框:
helpText("帮助文本。。。。。。。")

数字输入框:
numericInput("num",label = h3("Numeric input"),value = 1)

数字滑动条:
sliderInput("slider", label = h3("Sliders"),
min = 0, max = 100, value = c(25, 75))

文本输入框:
textInput("article_ID", "文章ID", value = "")

单选按钮:
radioButtons("hope_year", "年份选择:",
c("2015年" = 2015,"2016年" = 2016),
selected="2016",
inline=TRUE)

下拉框选择(单选或多选):
selectInput("year", "年份选择:",
choices=c("全部" = 999,"2016" = 2016,"2017" = 2017),selected=999,
multiple=TRUE, selectize=TRUE
)

复选框:
checkboxGroupInput("device", "设备",
choices=c("pc" = 0,"wap" = 1,"app" = 2),
selected=c(1,2),
inline=TRUE)

单选框(是/否):默认值设置为FALSE
checkboxInput("type", label = "是否包含", value = FALSE)

日期选择:
dateInput("date", label = h3("Date input"), value = "2014-01-01")

日期区间选择:
dateRangeInput("dateRange", "日期:",language="zh-CN",
start=Sys.Date()-7,end=Sys.Date()-1,
min = NULL,max = NULL,
format="yy-mm-dd")

上传文件:
fileInput('file1', '上传UID(txt文档)',accept = c('.csv','.tsv'))

# server.R端使用文件
inFile <- input$file1
if (is.null(inFile))
return(NULL)
users <- read.csv(inFile$datapath, header = F ,sep = ',', quote = '',stringsAsFactors=FALSE)

server.R

1
input_value <- reactive({input$inputId})

输出

在server.R中使用render*函数将计算结果传输至ui.R,再在ui.R中使用*Output函数展现

text

1
2
3
4
5
# server.R
output$TextName <- renderText ({'文本输出'})

# ui.R
textOutput("TextName")

table

1
2
3
4
5
# server.R
output$TableName <- renderDataTable({table_data})

# ui.R
dataTableOutput("TableName")

自定义ui

1
2
3
4
5
# server.R
output$CustomUiName <- renderUI({ui_content})

# ui.R
uiOutput("CustomUiName")

下载数据

1
2
3
4
5
6
7
8
9
10
# server.R
output$DownloadData <- downloadHandler(
filename = function() {"title"},
content = function(con) {
write.csv(data, con, row.names = FALSE,fileEncoding = "GBK")
}
)

# ui.R
downloadButton("DownloadData", "导出数据")

图形

1
2
3
4
5
# server.R
output$Plot <- renderPlot({ggplot(...)})

# ui.R
plotOutput("Plot",width = "50%")

That’s all.
Happy writing!