1

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

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

有人可以解释如何在 R Shiny 中实现这一点吗?

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)
4

1 回答 1

0

我添加了一个数据集:

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")

带有添加数据集的 UI:

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")
                                  
                        )
                        
              )
    )

您的服务器添加了 dataset1

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)
于 2021-05-19T10:50:00.567 回答