R语言与统计分析Cook RR shiny 开发web交互应用

R包shiny开发网页--8.文件上传和下载

2018-10-07  本文已影响33人  小洁忘了怎么分身

小洁写于2018.10.7
十一假期结束地毫无感觉,在家慵懒地呆了几天。在北京总觉得一切都排的满满当当,时间总是不够用。回家后却自然切换了懒猪模式,只知道吃睡了。。。感觉亲妈对我天天加班到十二点会表示怀疑TOT

1.上传csv,选择读取参数,在网页上显示为表格


这段代码是由官网教程例9改变而来,设置read.csv函数的sep、quote参数,还可选择显示全部还是显示全部。

library(shiny)
library(shinydashboard)
header <- dashboardHeader(title = "Uploading Files")
sidebar <- dashboardSidebar(
  fileInput("file1", "Choose CSV File",
            multiple = TRUE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv")),
  tags$hr(),
  checkboxInput("header", "Header", TRUE),
  radioButtons("sep", "Separator",
               choices = c(Comma = ",",
                           Semicolon = ";",
                           Tab = "\t"),
               selected = ","),
  radioButtons("quote", "Quote",
               choices = c(None = "",
                           "Double Quote" = '"',
                           "Single Quote" = "'"),
               selected = '"'),
  
  # Horizontal line ----
  tags$hr(),
  radioButtons("disp", "Display",
               choices = c(Head = "head",
                           All = "all"),
               selected = "head")
)
body <- dashboardBody(tableOutput("contents"))
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
  
  output$contents <- renderTable({
    
    req(input$file1)
    
    df <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)
    
    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
  })
}
shinyApp(ui,server)

上传文件的命令:fileInput
显示空行:tags$hr()
实现选择输出:if、else

2.下载

原理:把write.csv生成界面版


library(shiny)
library(shinydashboard)
header <- dashboardHeader(title = "Downloading Data")

sidebar <- dashboardSidebar(
    selectInput("dataset", "Choose a dataset:",
                choices = c("rock", "pressure", "cars")),
    downloadButton("downloadData", "Download")
)

body <- dashboardBody(tableOutput("table"))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  
  output$table <- renderTable({
    datasetInput()
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(input$dataset, ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )
  
}

shinyApp(ui, server)

3.显示系统时间

library(shiny)

# Define UI for displaying current time ----
ui <- fluidPage(
  
  h2(textOutput("currentTime"))
  
)

# Define server logic to show current time, update every second ----
server <- function(input, output, session) {
  
  output$currentTime <- renderText({
    invalidateLater(1000, session)
    paste("The current time is", Sys.time())
  })
  
}

# Create Shiny app ----
shinyApp(ui, server)

invalidateLater(1000, session)代表1000毫秒后更新界面,Sys.time()代表输出系统时间。

微信公众号生信星球同步更新我的文章

友情链接:
生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
B站链接:https://m.bilibili.com/space/338686099
YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists
生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA
学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw
资料大全:https://mp.weixin.qq.com/s/QcES9u1vYh-l6LMXPgJIlA

上一篇下一篇

猜你喜欢

热点阅读