【问题标题】:R Shiny App for Linear Regression, Issue with Render Functions用于线性回归的 R Shiny 应用程序,渲染函数问题
【发布时间】:2015-11-23 15:08:25
【问题描述】:

当我运行我的代码时,我没有收到任何错误并且我的应用程序保持运行状态(不会因为代码错误而变成灰屏)。但是,我的屏幕左侧没有任何输出。我假设我在某处有某种类型的语法错误,但我无法弄清楚。这是我的代码,应该很容易在您自己的 R Studio 上运行。

这是我的用户界面:

shinyUI(fluidPage(
  titlePanel("Linear Regression"),

  sidebarLayout(position = "right",
    mainPanel(h2("Your Model"), dataTableOutput("table")),
    sidebarPanel(h2("Your Data"),

      fileInput("mydata", label = h4("Please upload the .xls, .txt, or.csv file you would like included in the analysis. Please use column names in your dataset.")),

      textInput("response", label=h4 ("What is the column name of your response variable?")),

      textInput("explan1", label=h4 ("What is the column name of your explanitory variable?")),

      actionButton("analysis", "Analyze!"),
      verbatimTextOutput("modelSummary"), 
      plotOutput("plot")

      )
    )
  )
)

这是我的服务器:

shinyServer({
  function(input, output) ({
    observeEvent(input$analysis, {
     reactive({
        dat <- file.path(input$mydata)

        response=input$response
        explan1=input$explan1

        plot(input$response~input$explan1, data=dat)

        mean.response<-mean(dat$response,na.rm=T)

        abline(h=mean.Premium)

        model=lm(response~explan1, data = dat)

        output$plot <- renderPlot({
          plot(input$response~input$explan1, data= dat)
          abline(model,col="red")
        })

        output$modelSummary <- renderPrint({
          summary(model)
        })
      })   
    })
  })
})

对于 renderText,我试图让它输出整个摘要。 谢谢!

【问题讨论】:

  • 据我所知,您的应用程序运行正常。我可以在您的应用左侧看到所有菜单。如果您的意思是在您点击“分析”后出现问题,您可能应该制作一个最小的可重现示例(即不需要我们创建包含变量的文本文件以加载到您的应用中的示例)。

标签: r output shiny render


【解决方案1】:

您从未分配过您的renderText。您的ui 中也没有相应的textOutput 呼叫。尽管您以闪亮的方式渲染对象,但它必须作为输出提供。但是,renderText 不适用于从summary 返回的list。我会使用renderPrintverbatimTextOutput 来提供输出。

另外,我不确定submitButton。我发现actionButtonobserveEvent 块有更多的控制权。个人喜好,但我似乎无法让 submitButton 正常工作,所以这是我的解决方案。

这是一个可重复性极低的示例,演示如何使用您的布局以闪亮的方式打印 summary 输出。鉴于您没有提供可重现的数据集,我使用了 mtcars 数据。

library(shiny)
data(mtcars)

runApp(
  list(
    ui = fluidPage(
      titlePanel("Linear Regression"),

      sidebarLayout(position = "right",
                    mainPanel(h2("Your Model"), dataTableOutput("table")),
                    sidebarPanel(h2("Your Data"),

                                 fileInput("mydata", label = h4("Please upload the .xls, .txt, or.csv file you would like included in the analysis.")),

                                 radioButtons("filetype", label = h4("Please select the type of data uploaded:"), c(".csv", ".txt", ".xls"), selected = ".xls"),

                                 textInput("response", label=h4 ("What is the column name of your response variable?")),

                                 textInput("explan1", label=h4 ("What is the column name of your explanitory variable?")),
                                 actionButton("analysis","Analyze!"),
                                 verbatimTextOutput("modelSummary")

                    )

      )),

    server = function(input, output) {
      observeEvent( input$analysis, {

          model=lm(mpg ~ hp, data = mtcars)


          output$modelSummary <- renderPrint({
            summary(model)
          })

      })
    }

))

【讨论】:

  • 我已经编辑了上面的代码以显示我现在拥有的内容。尽管我的控制台中没有出现错误或警告消息,但我仍然没有在我的应用程序中获得输出。
  • @LindseyRegister 删除 reactive 语句。没必要。
  • 我删除它并在我尝试提交时收到此错误消息:警告:观察者中未处理的错误:'字符'类型的无效'环境'参数观察事件(输入$分析)
  • @LindseyRegister 你能正常运行我的代码吗?
  • @LindseyRegister 您使用的是什么闪亮版本?我的代码使用shiny_0.12.2 运行没有错误。如果您完全复制了我的代码,则不应收到该错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 2021-07-29
  • 2021-03-12
  • 2012-07-24
  • 1970-01-01
  • 2020-07-03
相关资源
最近更新 更多