如何在R中渲染本地数据集



我有以下显示CSV结果的R Shiny代码。目前,它只适用于R的默认数据集。我希望以与本地机器上的数据集相同的方式显示CSV结果。

示例csv:Study1.csv、Study2.csv、Study3.csv

有人能解释一下如何在R中实现这一点吗?

ui <- fluidPage(

# App title ----
titlePanel("Downloading Data"),

# Sidebar layout with input and output definitions ----
sidebarLayout(

# Sidebar panel for inputs ----
sidebarPanel(

# Input: Choose dataset ----
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),

# Button
downloadButton("downloadData", "Download")

),

# Main panel for displaying outputs ----
mainPanel(

tableOutput("table")

)

)
)
server <- function(input, output) {

# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})

# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})

# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE)
}
)

}
shinyApp(ui, server)

我添加了一个数据集:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))

您也可以通过读取csv并将其用作数据集1

dataset1 <- read.csv("your path",sep="your seperator")

添加了数据集的用户界面:

dataset1 <- structure(list(This = c(1L, 7L, 3L), is_the = c(5L, 8L, 2L), 
header = c(9L, 5L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))
ui <- fluidPage(

# App title ----
titlePanel("Downloading Data"),

# Sidebar layout with input and output definitions ----
sidebarLayout(

# Sidebar panel for inputs ----
sidebarPanel(

# Input: Choose dataset ----
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars", "dataset1")),

# Button
downloadButton("downloadData", "Download")

),

# Main panel for displaying outputs ----
mainPanel(

tableOutput("table")

)

)
)

添加了数据集1 的服务器

server <- function(input, output) {

# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars,
"dataset1" = dataset1)
})

# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})

# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(datasetInput(), file, row.names = FALSE)
}
)

}
shinyApp(ui, server)

最新更新