我正在尝试使用Shiny为线性和多元回归编写应用程序,但多个条件面板存在问题,
那个
这不是一个单一的问题,它也无法读取文件,并保持所选第一个条件面板的状态。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# # Application title
# column(9,
# titlePanel("Multiple Regression")
#
# ),
sidebarLayout(
sidebarPanel(
selectInput("sel_type",
"Select Regression Type ",
c("Linear Regression"="LR","Multiple Regression"="MR"),
selected = "LR"),
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
uiOutput("select_predict"),
conditionalPanel(
condition = "input.sel_type=='LR'",
uiOutput("select_test")
)
),
mainPanel(
conditionalPanel(
condition = "input.sel_type=='LR'",
h1("Simple Linear Regression"),
br(),
h3(textOutput("scat_plot"))
,plotOutput("scatter_plot",width = 500, height = 500),
br(),
# verbatimTextOutput("dataSet"),
h3("Data Set"),
br(),
column(4,
h4("Test variable"),
tableOutput("tbl1"),
offset = 1
),
h4("Predicting variable"),
tableOutput("tbl"),
br(),
h3("correlation analysis"),
verbatimTextOutput("cor"),
br(),
h3("Linear model and Regression Analysis"),
br(),
verbatimTextOutput("lm_ra"),
br(),
uiOutput("predict"),
br(),
verbatimTextOutput("predicted_value")
),
conditionalPanel(
condition = "input.sel_type == 'MR' ",
h1("Multiple Regression"),
br(),
verbatimTextOutput("summary"),
h2("COrrelation Analysis"),
br(),
verbatimTextOutput("cor")
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# sample1 <- read.csv(input$file1$datapath)
output$select_predict = renderUI({
df <- read.csv(input$file1$datapath)
selectInput("sel_test_var","Select Test variable",choices = names(df))
})
output$select_test = renderUI({
df <- read.csv(input$file1$datapath)
selectInput("ip2","Select Predicting Variable",names(df)[!names(df)==input$sel_test_var])#,multiple = T)
})
output$scat_plot = renderText({
sprintf(
"Scatter plot between '%s' and '%s'", input$sel_test_var, input$ip2
)
})
output$scatter_plot = renderPlot({
df <- read.csv(input$file1$datapath)
x<- df[,input$sel_test_var]
y<- df[,input$ip2]
if (!is.null(y)) {
plot(x,y,xlab=input$sel_test_var, ylab=input$ip2)
abline(lm(y~x))
}
})
# output$dataSet = renderPrint({
# df <- read.csv(input$file1$datapath)
# sprintf("Data of %s",input$sel_test_var)
# df[,input$sel_test_var]
# # sprintf("Data of %s",input$ip2)
# # df[,input$ip2]
# })
output$tbl = renderTable({
df <- read.csv(input$file1$datapath)
z <- as.data.frame(df[,input$ip2])
names(z) <- input$ip2
z
})
output$tbl1 = renderTable({
df <- read.csv(input$file1$datapath)
z <- as.data.frame(df[,input$sel_test_var])
names(z) <- input$sel_test_var
z
})
output$cor=renderPrint({
df <- read.csv(input$file1$datapath)
result <- cor.test(df[,input$ip2],df[,input$sel_test_var],method = "pearson")
result
})
output$lm_ra = renderPrint({
df <- read.csv(input$file1$datapath)
model1 <- lm(df[,input$sel_test_var] ~ df[,input$ip2])
model1
#sprintf("Summury linear model")
cat(sprintf("Model summary"))
summary(model1)
})
output$predict = renderUI({
numericInput(inputId = "predict_value",label = "Enter Predicting value",value = 10)
})
output$predicted_value = renderPrint({
df <- read.csv(input$file1$datapath)
x <- df[,input$ip2]
model1 <- lm(df[,input$sel_test_var] ~ x)
y=predict(model1,data.frame(x = input$predict_value),interval = "confidence", level=0.95)
y
})
########################## Multiple Regression #################################
output$summary = renderPrint({
df <- read.csv(input$file1$datapath)
cat(paste("Structure of Data Set\n"))
str(df)
#cat(paste("\n\n"))
# cat(paste("Dimension of Data Set\n"))
# dim(df)
cat(paste("\n\n"))
cat(paste("Summary of Data Set\n"))
summary(df)
})
output$cor = renderPrint({
df <- read.csv(input$file1$datapath)
cor(df)
})
}
# Run the application
shinyApp(ui = ui, server = server)
预先感谢您!