I have a code with navbarPage, that the user will choose which test he wants, upload his file, select the options and download the results.
My problem: how do I organize my code to know which test the user selected? To keep my code going as he asked?
When I had only one fileInput, my code runned properly, but when I added the second one, stopped showing my data as a table on the mainPage and my downloadButton gone crazy.
My code:
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)