第 1 组和第 2 组的 csv 结果未显示

时间:2021-05-19 16:12:50

标签: r shiny rscript rshiny

下面的代码从本地计算机获取一个 CSV 文件并将其显示在主面板上。不幸的是,该代码没有运行,这意味着它在选择“set1”、“set2”等选项时不会显示预期的 CSV 结果。我是 R 的新手.. 谁能帮我解决这个问题?

library(here)
library(shiny)


set1_path <- here("set1.csv")
set2_path <-here("set2.csv")
set3_path <-here("set3.csv")
set4_path <- here("set4.csv")


set1<- read.csv(set1_path)
set2 <- read.csv(set2_path)
set3 <- read.csv(set3_path)
set4 <- read.csv(set4_path)

options(shiny.maxRequestSize=30*1024^2)
shinyApp(
  ui = tagList(
    navbarPage(
      theme = "spacelab",
      
      tabPanel("report extracting",
               sidebarPanel(
                 fileInput("file1", "Select datasets:",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv"),
                           
                           
                 ),
                 # tags$hr(),
                 # checkboxInput("header", "Header", TRUE),
                 # textInput("txt", "Study info:", "Study name read"),
                 # sliderInput("slider", "Tables to read:", 1, 100, 30),
                 # tags$h5("Prepare extraction"),
                 selectInput("pdfExtract1", "Pick a Domain", choices = c("domain1", "domain2", "domain3", "domain4")),
                 tableOutput("preview"),
                 actionButton("pdfExtract", "Extract", class = "btn-primary"),
                 # actionButton("dataset", "Extract", class = "btn-primary")
                 
                 #Reading extracted datasets 
                 # Input: Choose dataset ----
                 selectInput("dataset", "Choose a dataset:",
                             choices = c("SelectDataSet ", "set1", "set2", "set3", "set4")),
                 
                 # Button
                 downloadButton("downloadData", "Download")
                 
               
               ),
               mainPanel(
                 tableOutput("table"),
                 # tableOutput("contents"),
                 tabsetPanel(
                   
                   tabPanel("PDF File select",
                            h4("Domains"),
                            tableOutput("table"),
                            h3("Extracting..."),
                            # selectInput("pdfExtract1", "Pick a Domain", choices = c("domain1", "domain2", "domain3", "domain4")),
                            # tableOutput("preview"),
                            # actionButton("pdfExtract", "Extract", class = "btn-primary")
                            # downloadButton("download", "Download .tsv")
                            
                   ),
                   tabPanel("Raw data", "TBD"),
                   tabPanel("Summary data", "TBD")
                 )
               )
      ), # end of first tabpanel
      tabPanel("calculation",
               sidebarPanel(
                 fileInput("file2", "Select datasets:",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")
                 ),
                 tags$hr(),
                 checkboxInput("header", "Header", TRUE),
                 textInput("txt2", "domain info:", "report"),
                 sliderInput("slider", "Tables to read:", 1, 100, 30),
                 tags$h5("calculation"),
                 actionButton("dataset2", "Extract", class = "btn-primary")
               ),
               mainPanel(
                 tableOutput("contents2"),
                 
                 tabsetPanel(
                   tabPanel("Datasets",
                            h4("Domains"),
                            tableOutput("table2"),
                            h3("Summarizing...")
                   )
                 )
               )
      ),
      tabPanel("study compare",
               sidebarPanel(
                 fileInput("file3", "compare:"),
                 textInput("txt3", "Study info:", "Study name read"),
                 tags$h5("calculation"),
                 actionButton("action2", "Compare", class = "btn-primary")
               ),
               mainPanel(
                 tabsetPanel(
                   tabPanel("PDF File select",
                            h4("Domains"),
                            tableOutput("table3"),
                            h3("Comparing..."),
                   ),
                   tabPanel("Summary data", "TBD")
                 )
               )
      )
    )
  ),
  server = function(input, output,session) {
    
    # output$contents <- renderTable({
    #   # 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$contents2 <- renderTable({
    #   # 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$file2
    #   
    #   if (is.null(inFile))
    #     return(NULL)
      
    #   read.csv(inFile$datapath, header = input$header)
    # })
    # output$txtout <- renderText({
    #   paste(input$txt, input$slider, format(input$date), sep = ", ")
    # })
    output$table <- renderTable({
      df <- c("datasetsx")
    })
    output$table2 <- renderTable({
      df <- c("datasetsy")
    })
    output$table3 <- renderTable({
      df <- c("datasetsz")
    })
    observeEvent(input$dataset, {
      source("domain.R", local = TRUE)
    })
    observeEvent(input$dataset2, {
      source("calculation.R", local = TRUE)
    })
    #Domain level Selection   
    observeEvent(input$pdfExtract, {    
      if(input$pdfExtract1 == "PP"){ 
        source("domain1.R", local = TRUE)
      }
      else if(input$pdfExtract1 == "MA"){
        source("domain2.R", local = TRUE)
      }
      else if (input$pdfExtract1 =="CL"){
        source("domain3.R", local =TRUE)
      }
      else{
        source("domain4.R", local =TRUE)
      }
    })
    # Reactive value for selected dataset ----
    datasetInput <- reactive({
      switch(input$dataset,
             "set1" = set2,
             "set2" = set2,
             "set3" = set3,
             "set4" = set4)
    })
    
    # 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)
      }
    )
    
  }
)

1 个答案:

答案 0 :(得分:0)

我从你的代码中做了一个最小的例子。你必须做一些事情。 首先你需要一个 eventReactive:

datasetInput <- eventReactive( input$pdfExtract, {
      switch(input$dataset,
             "set1" = set1
             )     })

如果用户选择了一个集合,什么都不会发生,直到用户点击“pdfExtract-button”。然后进行数据集输入。我们在这里调用它在我们的主面板中查看:

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

整个程序:

library(shiny)
    
    set1 <- 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))
    options(shiny.maxRequestSize=30*1024^2)
    
    ui <- fluidPage(
      theme = "spacelab",
                     selectInput(inputId = "dataset", "Choose a dataset:",
                                 choices = c("SelectDataSet ", "set1")),
                     
                     actionButton(inputId = "pdfExtract", "Extract", class = "btn-primary"),
                    
                   mainPanel(
                     tableOutput(outputId = "table"),
                   )
        )
    
    server = function(input, output,session) {
        
        # Reactive value for selected dataset ----
        datasetInput <- eventReactive( input$pdfExtract, {
          switch(input$dataset,
                 "set1" = set1
                 )     })
        
        # Table of selected dataset ----
        output$table <- renderTable({
            datasetInput()
          })
        
      }
    shinyApp(ui, server)
<块引用>

另外你有 output$table <- 你在服务器中调用两次的 renderTable,我想这就是为什么 R 没有在你的代码中加载任何表。

相关问题