问题描述
我是 Shiny Apps 的新手,所以非常感谢您的帮助!我的代码目前已损坏,我不确定原因。
这是我的问题:
- 如何使此代码起作用?我希望用户上传一个 csv 文件,在数据表中查看它,然后下载数据表
- 如何使用列中的正则表达式过滤行?我希望应用此正则表达式
df[with(df,grepl("\\bDATE\\b|\\b[0-9]{4}-[0-9]{2}-[0-9]{2}\\b|\\b[0-9]{2}-[0-9]{2}\\b|[0-9]{4}\\b",close_notes)),]
。您可以假设每个 csv 文件都将该列作为数据的一部分(在示例中为 close_notes)。
谢谢!我的代码如下。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1","Choose CSV File",accept = c(
"text/csv","text/comma-separated-values,text/plain",".csv")
),tags$hr(),checkBoxInput("header","Header",TRUE),# Button
downloadButton("downloadData","Download")
),mainPanel(
dataTableOutput("contents")
)
)
)
server <- function(input,output) {
output$contents <- renderDataTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file,it will be a data frame with 'name',# 'size','type',and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath,header = input$header)
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset,".csv",sep = "")
},content = function(file) {
write.csv(datasetinput(),file,row.names = FALSE)
}
)
}
shinyApp(ui,server)
解决方法
试试这个
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1","Choose CSV File",accept = c(
"text/csv","text/comma-separated-values,text/plain",".csv")
),tags$hr(),checkboxInput("header","Header",TRUE),# Button
downloadButton("downloadData","Download")
),mainPanel(
dataTableOutput("contents")
)
)
)
server <- function(input,output) {
datasetInput <- reactive({
req(input$file1)
# input$file1 will be NULL initially. After the user selects
# and uploads a file,it will be a data frame with 'name',# 'size','type',and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath,header = input$header)
})
output$contents <- renderDataTable({
datasetInput()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("myfile",Sys.Date(),".csv",sep = "")
},content = function(file) {
write.csv(datasetInput(),file,row.names = FALSE)
}
)
}
shinyApp(ui,server)