DT和downloadButton应用
library(shiny)
library(DT)
ui <- shinyUI(
fluidPage(
titlePanel("DT test"),
downloadButton('downloadData', 'Download'),
fluidRow(
DT::dataTableOutput("table")
)
)
)
server <- shinyServer(function(input, output) {
output$table <- DT::renderDataTable(DT::datatable({
mtcars
}, rownames = FALSE))
output$downloadData <- downloadHandler(
filename = 'test.csv',
content = function(file) {
write.csv(mtcars, file)
}
)
})
shinyApp(ui,server)
downloadButton 中验证结果输出
一种情况是通过shinyjs,只有输入时,才显示下载按钮:
library(shiny)
ui <- fluidPage(
shinyjs::useShinyjs(),
textInput("text", "content", "", placeholder = "please insert something"),
shinyjs::hidden(downloadButton("download"))
)
server <- function(input, output, session) {
output$download <- downloadHandler(
filename = "file.csv",
content = function(file) {
write.csv(data.frame(content = input$text), file)
}
)
observeEvent(input$text, {
if (input$text == "")
shinyjs::hide("download")
else
shinyjs::show("download")
})
}
shinyApp(ui, server)
另一种情况是downloadButton搭配uiOutput,即只有输出结果出现时,下载按钮才出现。
#ui端
uiOutput("download")
#server端
output$download <- renderUI({
if(!is.null(input$file1) & !is.null(input$file2)) {
downloadButton('OutputFile', 'Download Output File')
}
})
添加进度条
但不是真正的程序进度。
# Only run examples in interactive R sessions
if (interactive()) {
options(device.ask.default = FALSE)
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlot({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0, {
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.25)
}
})
plot(cars)
})
}
shinyApp(ui, server)
}
如何确保仅在使用Shiny按下操作按钮时才触发操作
search_tweets <- function(search) return(search)
library(shiny)
ui <- fluidPage(
# Application title
#titlePanel("Twitter Analytics"),
fluidRow(
column( 4, titlePanel("Twitter Analytics")),
column( 3, textOutput("mysearch") ),
column( 4, textInput("searchstring",
label = "",
value = "")),
column(1,
br(),
actionButton("action", "go"))
)
)
server <- function(input, output) {
twitter <- eventReactive(input$action,{
search_tweets(input$searchstring)
})
output$mysearch <- renderText({
twitter()
})
}
shinyApp(ui, server)
其他
需要系统性学习了,不然不能随心所欲,寸步难行。多看看别人的项目:
https://github.com/rli012/CancerMIRNome
https://github.com/wangshisheng/NAguideR
网友评论