【发布时间】: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)
【问题讨论】: