在带有用户选择的列的闪亮应用程序中使用自动绘图

如何解决在带有用户选择的列的闪亮应用程序中使用自动绘图

我正在制作一个闪亮的应用程序,允许用户上传 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 = '"'),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,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.

解决方法

试试这个

  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")
     
  })

output

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?