[英]How to use R shiny to filter a specific column from a csv file and extract the data in csv and pdf format
As I am new to R shiny, please go easy on me: I have found this code useful: https://community.rstudio.com/t/download-dataset-filtered-in-shiny-input/75770 . 此代码根据“物种”列获取 Iris 数据和过滤器
为了在通过fileInput()
上传自己的数据后得到过滤结果,我对上面的代码进行了一些调整。 我正在尝试使用“类型”列过滤数据,但我收到了下面提到的错误。
错误:
object 'file1' not found
csv 数据:
ID Type Range
21 A1 100
22 C1 200
23 E1 300
代码:
library(tidyverse)
library(shiny)
library(DT)
library(shinyWidgets)
ui <- fluidPage(
#setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")),
h1("Data"),
sidebarLayout(
sidebarPanel(fileInput("file1", label = "Choose species"),
downloadButton("download1","Download entire Table as csv")),
mainPanel(h4("Table 1: Iris"),
dataTableOutput("csv_dto")
)
))
server <- function(input, output, session) {
output$csv_dto <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
})
thedata <- reactive({
file$datapath %>%
filter(Type == input$Type)
})
output$type_dto <- renderDataTable({
thedata() %>%
datatable(extensions = 'Buttons',
options = list(
#Each letter is a dif element of a datatable view, this makes buttons the last thing that's shown.
buttons = c("copy", "csv", "pdf")),
filter = list(
position = 'top'),
rownames = FALSE)
})
output$download1 <- downloadHandler(
filename = function() {
paste("type_", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(thedata(), file)
}
)
}
shinyApp(ui, server)
有人可以帮我解决这个问题吗?
我无法为您提供 PDF output 的帮助,但要让您入门:您必须对?fileInput
示例中的代码进行一些调整。
而不是renderTable
使用reactive
。 也不要分配给output
。 在 UI 中使用dataTableOutput("type_dto")
代替dataTableOutput("csv_dto")
")。
library(tidyverse)
library(shiny)
library(DT)
library(shinyWidgets)
ui <- fluidPage(
# setBackgroundColor(color = c("#66e0ff", "#00a3cc", "#003d4d")),
h1("Data"),
sidebarLayout(
sidebarPanel(
fileInput("file1", label = "Choose species"),
downloadButton("download1", "Download entire Table as csv")
),
mainPanel(
h4("Table 1: Iris"),
dataTableOutput("type_dto")
)
)
)
server <- function(input, output, session) {
csv_dto <- reactive({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath)
})
thedata <- reactive({
csv_dto() %>%
filter(Type == input$Type)
})
output$type_dto <- renderDataTable({
thedata() %>%
datatable(
extensions = "Buttons",
options = list(
# Each letter is a dif element of a datatable view, this makes buttons the last thing that's shown.
buttons = c("copy", "csv", "pdf")
),
filter = list(
position = "top"
),
rownames = FALSE
)
})
output$download1 <- downloadHandler(
filename = function() {
paste("type_", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(thedata(), file)
}
)
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.