R shiny app | 网页应用 空格分隔的文本文件在线转csv

shiny 能快速把R程序以web app的形式提供出来,方便使用,降低技术使用门槛。

本文提供的示例:把空格分隔的txt文件转为逗号分隔的csv文件。

前置依赖:需要有R环境(v4.2.0),安装shiny包(v1.9.1)。括号内是我使用的版本。

install.packages(“shiny”)

1.运行与效果

  • 文件名:script/app3/app.R
  • 运行: > shiny::runApp(“script/app3/”)
  • 关闭:关闭浏览器。
  1. 左上角:上传文件,
  2. 右侧:预览效果
  3. 右下角:下载csv文件
    在这里插入图片描述

2.文件内容

script/app3/app.R 如下:

library(shiny)
library(bslib)

# Define UI for slider demo app ----
ui <- page_sidebar(
  
  # App title ----
  title = "Blank space txt to csv | v0.0.1",
  
  # Sidebar panel for inputs ----
  sidebar = sidebar(
    
    # Input: Select a file ----
    fileInput(
      "file1",
      "Choose TXT File",
      multiple = TRUE,
      accept = c(
        "text/csv",
        "text/comma-separated-values,text/plain",
        "text/blank-separated-values,text/plain",
        ".csv"
      )
    ),
    
    tags$hr(),
    
    sliderInput(
      "num",
      "show number:",
      min = 2,
      max = 1000,
      value = 10
    ),
    
    # Horizontal line ----
    tags$hr(),
    
    # Input: Checkbox if file has header ----
    checkboxInput("header", "Header", TRUE),
    
    # Input: Select separator ----
    radioButtons(
      "sep",
      "Separator",
      choices = c(
        Comma = ",",
        Semicolon = ";",
        Blank = " ",
        Tab = "\t"
      ),
      selected = " "
    ),
    
    # Input: Select quotes ----
    radioButtons(
      "quote",
      "Quote",
      choices = c(
        None = "",
        "Double Quote" = '"',
        "Single Quote" = "'"
      ),
      selected = ''
    ),
    
    # Horizontal line ----
    tags$hr(),
    
    # Input: Select number of rows to display ----
    radioButtons(
      "disp",
      "Display",
      choices = c(
        Head = "head",
        All = "all"
      ),
      selected = "head"
    ),
    # Button
    downloadButton("downloadData", "Download csv")
  ),
  
  # Output: Data file ----
  tableOutput("contents")
)

# Define server logic to read selected file ----
server <- function(input, output) {
  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.
    
    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, n=input$num))
    } else {
      return(df, n=input$num)
    }
  })
  
  
  # Downloadable csv of selected dataset ----
  output$downloadData <- downloadHandler(
    filename = function() {
      req(input$file1)
      paste(input$file1$datapath, ".csv", sep = "")
    },
    
    content = function(file) {
      req(input$file1)
      
      df <- read.csv(
        input$file1$datapath,
        header = input$header,
        sep = input$sep,
        quote = input$quote
      )
      write.csv(df, file, row.names = FALSE, quote=F)
    }
  )
}


if(0){
  # show blank separated txt file with header
  runApp("script/app3/")
}


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

Ref

  • https://shiny.posit.co/r/getstarted/shiny-basics/lesson1/

End