当前位置: 首页 > news >正文

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"),# ButtondownloadButton("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 headerrunApp("script/app3/")
}# Create Shiny app ----
shinyApp(ui, server)

Ref

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

End

http://www.lryc.cn/news/516005.html

相关文章:

  • 三天速成微服务
  • 【踩坑记录】uni-app 微信小程序调试不更新问题解决指南
  • 【Adobe Acrobat PDF】Acrobat failed to connect to a DDE server.是怎么回事?
  • PyTorch 中 coalesce() 函数详解与应用示例
  • ubuntu进行C++的调试
  • 【U8+】用友U8软件中,出入库流水输出excel的时候提示报表输出引擎错误。
  • NoSQL简介
  • XIAO Esp32 S3 网络摄像头——3音视频监控
  • 题目解析与代码实现:You‘re Given a String
  • Understanding the Lomb–Scargle Periodogram
  • 解决Linux切换用户后的命令提示符为-bashxx$的问题
  • AMP 混合精度训练中的动态缩放机制: grad_scaler.py函数解析( torch._amp_update_scale_)
  • Oracle数据库如何找到 Top Hard Parsing SQL 语句?
  • Mono里运行C#脚本25—mono_codegen
  • flink cdc oceanbase(binlog模式)
  • 【WPF】 数据绑定机制之INotifyPropertyChanged
  • 机器学习算法深度解析:以支持向量机(SVM)为例及实战应用
  • 网络编程基础:连接Java的秘密网络
  • 无监督学习:自编码器(AutoEncoder)
  • 在不到 5 分钟的时间内将威胁情报 PDF 添加为 AI 助手的自定义知识
  • Memcached prepend 命令
  • Win10 VScode配置远程Linux开发环境
  • 微信小程序校园自助点餐系统实战:从设计到实现
  • 解决sublime编译无法输入问题
  • const修饰指针总结
  • uniapp实现后端数据i18n国际化
  • 什么是国密设计
  • Android IO 问题:java.io.IOException Operation not permitted
  • 安装bert_embedding遇到问题
  • cka考试-03-k8s版本升级