【问题标题】:Multiples fileInput on navbarPage ShinynavbarPage Shiny 上的倍数文件输入
【发布时间】:2019-02-02 02:47:39
【问题描述】:

我有一个带有 navbarPage 的代码,用户将选择他想要的测试,上传他的文件,选择选项并下载结果。

我的问题:如何组织我的代码以了解用户选择了哪个测试?按照他的要求让我的代码继续运行?

当我只有一个 fileInput 时,我的代码可以正常运行,但是当我添加第二个时,我的数据不再显示为 mainPage 上的表格,并且我的 downloadButton 变得疯狂。

我的代码:

ui <- navbarPage(title = "DadosFacil", fluid = TRUE, theme = shinytheme("cosmo"),

           #---- TABA DE NORMALIDADE       
           tabPanel("Normalidade", id = "NormalTab",
                    sidebarLayout(
                      sidebarPanel( 
                        helper("",
                               colour = "black",
                               type = "inline",
                               title = "Upload do arquivo",
                               size = "s",
                               content = "Selecione apenas um arquivo em documento de texto (.txt), para mais informacoes sobre como organizar seu arquivo acesse nosso faq."),

                        fileInput("file1", "Escolha seus dados em .txt", #Ill only accept .txt files
                                  accept =".txt"),


                        helper("",
                               colour = "black",
                               type = "inline",
                               title = "Ajuda",
                               size = "s",
                               content = "Marque se seu arquivo possui os nomes das variaveis na primeira linha"),

                        checkboxInput("header", "Header",value = TRUE),

                        helper("",
                               colour = "black",
                               type = "inline",
                               title = "Separador Decimal",
                               size = "s",
                               content = "Selecione como voce separou seus numeros no seu arquivo, comumente no Brasil se utiliza a virgula, nos outros paises o ponto. Note que caso selecione errado, obtera um erro."),

                        radioButtons("decimal", "Ponto ou Virgula: ",
                                     choices = c(Virgula = ",", Ponto = "."),
                                     selected = ","),

                        tags$hr(), #Linha horizontal

                        downloadButton("downloadData", "Download")
                        ),
                      mainPanel(
                        dataTableOutput("Previa")
                      )
                    )
                   ),

           #---ABA DA ANOVA
           navbarMenu("ANOVA",

             #---DELINEAMENTO INTEIRAMEINTE CASUALIZADO      
             tabPanel("DIC", id = "DicTab",
                      sidebarLayout(
                        sidebarPanel(
                          helper("",
                                 colour = "black",
                                 type = "inline",
                                 title = "Upload do arquivo",
                                 size = "s",
                                 content = "Selecione apenas um arquivo em documento de texto (.txt), para mais informacoes sobre como organizar seu arquivo acesse nosso faq."),

                          fileInput("file2", "Escolha seus dados em .txt -> DIC", #Ill only accept .txt files
                                    accept =".txt"),


                          helper("",
                                 colour = "black",
                                 type = "inline",
                                 title = "Ajuda",
                                 size = "s",
                                 content = "Marque se seu arquivo possui os nomes das variaveis na primeira linha"),

                          checkboxInput("header", "Header",value = TRUE),

                          helper("",
                                 colour = "black",
                                 type = "inline",
                                 title = "Separador Decimal",
                                 size = "s",
                                 content = "Selecione como voce separou seus numeros no seu arquivo, comumente no Brasil se utiliza a virgula, nos outros paises o ponto. Note que caso selecione errado, obtera um erro."),

                          radioButtons("decimal", "Ponto ou Virgula: ",
                                       choices = c(Virgula = ",", Ponto = "."),
                                       selected = ","),

                          tags$hr(), #Linha horizontal

                          downloadButton("downloadData", "Download")
                        ),
                        mainPanel(
                          dataTableOutput("Previa")
                      )
                      )
             ),

             tabPanel("DBC",
                      sidebarLayout(
                        sidebarPanel(

                        ),
                        mainPanel(

                        )
                      )
             ),

             tabPanel("DQL",
                      sidebarLayout(
                        sidebarPanel(

                        ),
                        mainPanel(

                        )
                      )

             )

           )
)

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

  observe_helpers()

  datasetInputNormal <-reactive({
    req(input$file1)

    tryCatch({
      df <- read.table(input$file1$datapath,
                       header = input$header,
                       sep = "\t",
                       dec = input$decimal)
    },
    error = function(e){
      #Retorna error se der algum problema
      stop(safeError(e))
    })

    return(df)

  })

  datasetInputDic <-reactive({
    req(input$file2)

    tryCatch({
      df <- read.table(input$file2$datapath,
                       header = input$header,
                       sep = "\t",
                       dec = input$decimal)
    },
    error = function(e){
      #Retorna error se der algum problema
      stop(safeError(e))
    })

    return(df)

  })

  output$Previa <- DT::renderDataTable(DT::datatable(datasetInput()))

  output$NormalidadeDownload <- downloadHandler(
    filename = function(){
      paste("Resul_",input$file1$name, sep = "")
    },
    content = function(file){
      if(input$id == "NormalTab"){
        writeLines(VariosNormal(datasetInputNormal()), con = file, sep = "" ) #Call my function VariosNormal()
      } else if(input$id == "DicTab"){
        writeLines(Teste_DIC(datasetInputDic()), con = file, sep = "" ) #Call my function Teste_DIC()
      }

    }
  )
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我终于想出了如何解决这个问题。阅读这个问题:R Shiny fileInput multiple filetypes。我注意到不能有两个具有相同 ID 的输出,所以我为具有不同 ID 的所有内容创建了另一个输出并工作! 但我的问题是:有没有办法解决这个问题?因为在我的应用程序上,我将有 20 多个选项卡,每个选项卡都有自己设置的 os 按钮,唯一的方法是拥有这样的大量代码?

    我的代码已修复:

    ui <- navbarPage(title = "DadosFacil", fluid = TRUE, theme = shinytheme("cosmo"),
    
               #---- TABA DE NORMALIDADE       
               tabPanel("Normalidade", id = "NormalTab",
                        sidebarLayout(
                          sidebarPanel( 
                            helper("",
                                   colour = "black",
                                   type = "inline",
                                   title = "Upload do arquivo",
                                   size = "s",
                                   content = "Selecione apenas um arquivo em documento de texto (.txt), para mais informacoes sobre como organizar seu arquivo acesse nosso faq."),
    
                            fileInput("fileNormal", "Escolha seus dados em .txt", #Ill only accept .txt files
                                      accept =".txt"),
    
    
                            helper("",
                                   colour = "black",
                                   type = "inline",
                                   title = "Ajuda",
                                   size = "s",
                                   content = "Marque se seu arquivo possui os nomes das variaveis na primeira linha"),
    
                            checkboxInput("headerNormal", "Header",value = TRUE),
    
                            helper("",
                                   colour = "black",
                                   type = "inline",
                                   title = "Separador Decimal",
                                   size = "s",
                                   content = "Selecione como voce separou seus numeros no seu arquivo, comumente no Brasil se utiliza a virgula, nos outros paises o ponto. Note que caso selecione errado, obtera um erro."),
    
                            radioButtons("decimalNormal", "Ponto ou Virgula: ",
                                         choices = c(Virgula = ",", Ponto = "."),
                                         selected = ","),
    
                            tags$hr(), #Linha horizontal
    
                            downloadButton("downloadNormal", "Download")
                            ),
                          mainPanel(
                            dataTableOutput("PreviaNormal")
                          )
                        )
                       ),
    
               #---ABA DA ANOVA
               navbarMenu("ANOVA",
    
                 #---DELINEAMENTO INTEIRAMEINTE CASUALIZADO      
                 tabPanel("DIC", id = "DicTab",
                          sidebarLayout(
                            sidebarPanel(
                              helper("",
                                     colour = "black",
                                     type = "inline",
                                     title = "Upload do arquivo",
                                     size = "s",
                                     content = "Selecione apenas um arquivo em documento de texto (.txt), para mais informacoes sobre como organizar seu arquivo acesse nosso faq."),
    
                              fileInput("fileDic", "Escolha seus dados em .txt -> DIC", #Ill only accept .txt files
                                        accept =".txt"),
    
    
                              helper("",
                                     colour = "black",
                                     type = "inline",
                                     title = "Ajuda",
                                     size = "s",
                                     content = "Marque se seu arquivo possui os nomes das variaveis na primeira linha"),
    
                              checkboxInput("headerDic", "Header",value = TRUE),
    
                              helper("",
                                     colour = "black",
                                     type = "inline",
                                     title = "Separador Decimal",
                                     size = "s",
                                     content = "Selecione como voce separou seus numeros no seu arquivo, comumente no Brasil se utiliza a virgula, nos outros paises o ponto. Note que caso selecione errado, obtera um erro."),
    
                              radioButtons("decimalDic", "Ponto ou Virgula: ",
                                           choices = c(Virgula = ",", Ponto = "."),
                                           selected = ","),
    
                              tags$hr(), #Linha horizontal
    
                              downloadButton("downloadDic", "Download")
                            ),
                            mainPanel(
                              dataTableOutput("PreviaDic")
                          )
                          )
                 ),
    
                 tabPanel("DBC",
                          sidebarLayout(
                            sidebarPanel(
    
                            ),
                            mainPanel(
    
                            )
                          )
                 ),
    
                 tabPanel("DQL",
                          sidebarLayout(
                            sidebarPanel(
    
                            ),
                            mainPanel(
    
                            )
                          )
    
                 )
    
               )
    )
    
    server <- function(input, output, session) {
    
      observe_helpers()
    
      #--- NORMAL TAB
      datasetInputNormal <-reactive({
        req(input$fileNormal)
    
        tryCatch({
          df <- read.table(input$fileNormal$datapath,
                           header = input$headerNormal,
                           sep = "\t",
                           dec = input$decimalNormal)
        },
        error = function(e){
          #Retorna error se der algum problema
          stop(safeError(e))
        })
    
        return(df)
    
      })
    
    
      output$PreviaNormal <- DT::renderDataTable(DT::datatable(datasetInputNormal()))
    
      output$downloadNormal <- downloadHandler(
        filename = function(){
          paste("Resul_Normal",input$fileNormal$name, sep = "")
        },
        content = function(file){
          writeLines(VariosNormal(datasetInputNormal()), con = file, sep = "" ) #Call my function VariosNormal()
    
        }
      )
    
      #---DIC TAB
    
      datasetInputDic <-reactive({
        req(input$fileDic)
    
        tryCatch({
          df <- read.table(input$fileDic$datapath,
                           header = input$headerDic,
                           sep = "\t",
                           dec = input$decimalDic)
        },
        error = function(e){
          #Retorna error se der algum problema
          stop(safeError(e))
        })
    
        return(df)
    
      })
    
      output$PreviaDic <- DT::renderDataTable(DT::datatable(datasetInputDic()))
    
      output$downloadDic <- downloadHandler(
        filename = function(){
          paste("Resul_Dic",input$fileDic$name, sep = "")
        },
        content = function(file){
          writeLines(Teste_DIC(datasetInputDic()), con = file, sep = "" ) #Call my function Teste_Dic
    
        }
      )
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多