【发布时间】:2021-01-14 02:57:49
【问题描述】:
最近在尝试学习shinyapp,并尝试做一个shinyapp进行t-test:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("t test shinyapp"),
# Sidebar set
sidebarLayout(
sidebarPanel(
h4(fileInput("file","Upload the file"),
helpText("Default max. file size is 5MB"),
# Horizontal line ----
tags$hr(),
uiOutput("vx1"),
br(),
uiOutput("vx2"),
br(),
uiOutput("gg"),
br(),
selectInput("method", "Select t test type",
c("One-sample t test",
"Independent two-sample t test",
"Paired sample t test"),
selected = "Independent two-sample t test"),
br(),
selectInput("alt", "Select alternative",
c("two.sided",
"greater",
"less"),
selected = "two.sided"),
br(),
textInput("mu", "Enter population mean for one-sample t test", ""),
br(),
radioButtons("ptype",
"Select the file type",
choices = list("png", "pdf")))),
mainPanel(
uiOutput("tb")
)
)))
shinyServer(function(input, output) {
dff <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.csv(file=file1$datapath, header = TRUE)
})
output$vx1 <- renderUI({
selectInput("var1", "Select variable names", choices = names(dff()))
})
output$vx2 <- renderUI({
selectInput("var2", "Select variable names", choices = names(dff()))
})
output$gg <- renderUI({
selectInput("gro", "Select group name", choices = names(dff()))
})
output$sum <- renderTable({
if(is.null(dff())){return ()}
print(c(paste0("You choice to do: ",input$method),
paste0("The way you use to test is: ", input$alt),
paste0("The variable you choice is: ", input$var1),
paste0("The group variable you selected is: ", input$gro),
paste0("the levels of group variable is:", levels(as.factor(dff()[input$gro])))),
sep = "\n")
print(dff()[input$gro])
})
output$tr <- renderTable({
if(is.null(dff())){return ()}
if(input$method == "One-sample t test"){
t.test(dff1[input$var1], mu = as.numeric(input$mu), alternative = input$alt)
} else if(input$method == "Independent two-sample t test"){
t.test(input$var1 ~ input$gro, data = dff(), alternative = input$alt)
}else{
t.test(dff()[input$var1], dff()[input$var2], paired=TRUE, alternative = input$alt)
}
})
output$tplot <- renderPlot({
})
downloadHandler(
filename = function(){
paste(input$method, input$ptype, sep = ".")
},
content = function(){
if(input$ptype == "png")
png(file)
else
pdf(file)
dev.off()
}
)
output$tb <- renderUI({
if(is.null(data()))
h5("Powered by", tags$img(src='qrcode.jpg'))
else
tabsetPanel(tabPanel("Summary", tableOutput("sum")),
tabPanel("t test result", tableOutput("tr")),
tabPanel("Plot",
downloadButton("download", "download the plot"),
))
})
})
如你所见,我可以在 output$sum 中打印 dff()[input$gro],但我无法在 output$tr 中运行。
当我运行代码时,我得到错误:t.test.formula 中的错误:分组因子必须恰好有 2 个级别。
有什么问题。非常感谢。
【问题讨论】:
-
你有
dff1[input$var1]。dff1对象定义了吗?