php网站设计流程,怎样登录建设银行官方网站,互联网服务平台官网,中国建设银行网站诚聘英才频道shiny 能快速把R程序以web app的形式提供出来#xff0c;方便使用#xff0c;降低技术使用门槛。
本文提供的示例#xff1a;把空格分隔的txt文件转为逗号分隔的csv文件。
前置依赖#xff1a;需要有R环境(v4.2.0)#xff0c;安装shiny包(v1.9.1)。括号内是我使用的版本…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/”)关闭关闭浏览器。
左上角上传文件右侧预览效果右下角下载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, ninput$num))} else {return(df, ninput$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, quoteF)})
}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