0

我正在制作一个闪亮的应用程序,允许用户上传 CSV,然后选择自变量和因变量。现在我可以上传文件、选择变量并运行回归分析。但是,我被困在将lm对象传递给然后通过新选项卡autoplot使其交互的步骤。autoplotly如何通过在闪亮的应用程序中使用用户选择的变量来创建交互式回归图?

用户界面

ui = navbarPage(tabPanel("Regression Analysis",
                         dataTableOutput('mytable'),
                         sidebarLayout(
                           sidebarPanel(width=3, fileInput("file1", "Please choose a CSV file",
                                                              multiple = T,
                                                              accept = c("text/csv",
                                                                         "text/comma-separated-values,text/plain",
                                                                         ".csv")),
                                                    tags$hr(),
                                                    checkboxInput("header", "Header", TRUE),
                                                    radioButtons("sep", "Separator",
                                                                 choices = c(Comma = ",",
                                                                             Semicolon = ";",
                                                                             Tab = "\t"),
                                                                 selected = ","),
                                                    radioButtons("quote", "Quote",
                                                                 choices = c(None = "",
                                                                             "Double Quote" = '"',
                                                                             "Single Quote" = "'"),
                                                                 selected = '"'),
                                                    tags$hr(),
                                                    radioButtons("disp", "Display",
                                                                 choices = c(Head = "head",
                                                                             All = "all"),
                                                                 selected = "head")

                           ),

                           mainPanel(
                             tableOutput("contents"),
                             actionButton("choice", "Define Regression Variables"),
                             selectInput("independent", "Independent Variables:", choices = NULL, multiple = T),
                             uiOutput("dependent1"),
                             #tableOutput("Table_selected.col"),
                             verbatimTextOutput("regTab")
                           )
                         ),
tabPanel("Plots",
    icon = icon("chart-area"),
    plotlyOutput(outputId = "RegPlots"))

)

服务器

server = function(input, output, session) {

  mydf <- reactive({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    df = read.csv(input$file1$datapath,
                  header = input$header,
                  sep = input$sep,
                  quote = input$quote)

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }

  })

  output$contents = renderTable({
    req(mydf())
    mydf()
  })

  # Code for allowing the user to select the variables/columns of interest
  info <- eventReactive(input$choice, {
    req(mydf())
    f <- mydf()
    f
  })

  observeEvent(input$choice, {  ## to update only when you click on the actionButton
    req(info())
    updateSelectInput(session,"independent", "Please Select independent Variable(s):", choices = names(info()) )
  })


  # output$Table_selected.col <- renderTable({
  #   input$choice
  #   req(info(),input$columns)
  #   f = info()
  #   f = subset(f, select = input$columns) #subsetting takes place here
  #   head(f)
  # })

  output$dependent1 = renderUI({
    req(mydf(),input$independent)
    radioButtons("dependent1", "Select a dependent Variable:",choices=names(mydf())[!names(mydf()) %in% input$independent])
  })

  ###  need to build your formuila correctly; It will work with multiple independent variables
  ###  model <- reactive({lm(reformulate(input$IndVar, input$DepVar), data = RegData)})

  runRegression <- reactive({
    req(mydf(),input$independent,input$dependent1)
    a = lm(reformulate(input$independent, input$dependent1), data=mydf())
    a
    # multinom(reformulate(input$independent, input$dependent1), data=mydf())  ### mulitnomial from nnet package
  })

  output$regTab = renderPrint({
    req(runRegression())
    if(!is.null(input$independent)){
      summary(runRegression())
    } else {
      print(data.frame(Warning="Please select Model Parameters."))
    }
  })

}
output$RegPlots = renderPlotly({
      req(runRegression())
      # Plot the residuals
      lm.plot.rsd = autoplot(a, label.size = 3, which = 1) +
        theme_bw()
      autoplotly(lm.plot.rsd +
                   ggplot2::ggtitle("Residuals vs Fitted")) 
    })
shinyApp(ui, server)

错误

 Error in : Objects of type function not supported by autoplot.
4

1 回答 1

1

尝试这个

  output$RegPlots = renderPlot({
    req(runRegression())
    # Plot the residuals
    a =  runRegression()
    ggplot(a, aes(x = .fitted, y = .resid)) +
      geom_point() +
      geom_smooth(method = loess, formula = y ~ x) +
      labs(title="Residuals vs Fitted")
     
  })

输出

如果你愿意,你可以尝试其他的情节。

于 2021-04-27T00:39:08.420 回答