【问题标题】:Shiny Package in RR中的闪亮包
【发布时间】:2023-03-13 06:54:01
【问题描述】:

我正在 R 中练习闪亮的包。我正在创建一个应用程序,用户在其中选择两个变量并确定要适合相应散点图的曲线的程度。为此,我正在使用 mtcars 数据。我使用 selectinput 命令来获取变量。我希望滑块输入命令来决定拟合曲线的程度。除了滑块输入命令之外,代码似乎都在工作。

library(shiny)

ui <- fluidPage(
  headerPanel('Fitting a curve'),
  sidebarPanel(
    selectInput(inputId = "xcol",label = "X-Axis",choices = names(mtcars)),
    selectInput(inputId = "ycol",label = "Y-Axis",choices = names(mtcars),selected = names(mtcars)[[3]]),
    sliderInput(inputId = "degree",label = "Degree of fit",min = 0,max = 2,value = 0)
  ),
  mainPanel(plotOutput("plot1"))

)

server <- function(input,output){
  x <- reactive({mtcars[,input$xcol]})
  y <- reactive({mtcars[,input$ycol]})
  z <- renderPrint({ifelse(input$degree==0,lm(y()~),ifelse(input$degree==1,lm(y()~x()),lm(y()~x()+x()^2)))})
  output$plot1 <- renderPlot({
    plot(x(),y(),col = "red")
    abline(z())
    })

}

shinyApp(ui = ui,server = server)

很确定服务器部分的“z”行有错误。请帮助我是闪亮包的新手。

【问题讨论】:

  • 您可以通过 lm(y()~1) 仅使用拦截来适应

标签: r shiny


【解决方案1】:
  1. 不要在这里使用ifelseif (...) {...} else {...} 更好(而且它不会破坏东西)。为什么?比较这两个:

    mdl1 <- ifelse(1 == 1, lm(mpg~disp, data=mtcars), lm(mpg~disp+cyl, data=mtcars))
    class(mdl1)
    # [1] "list"
    mdl1
    # [[1]]
    # (Intercept)        disp 
    # 29.59985476 -0.04121512 
    
    mdl2 <- if (1 == 1) lm(mpg~disp, data=mtcars) else lm(mpg~disp+cyl, data=mtcars)
    class(mdl2)
    # [1] "lm"
    mdl2
    # Call:
    # lm(formula = mpg ~ disp, data = mtcars)
    # Coefficients:
    # (Intercept)         disp  
    #    29.59985     -0.04122  
    
  2. 您应该得到一个错误,您应该在您的问题中逐字包含该问题。在这种情况下,我看到unexpected ')' in ...。我找到了lm(y()~)。您需要因变量或至少需要 1,将其更改为 lm(y()~1) 可修复该错字。

  3. 在这里(还)不会压垮你,但是在使用它们之前require 你的反应变量是稳定的而不是NULL 是一个很好的做法。至少阅读?req;如需更多控制和用户友好性,请阅读?validate

看看这是否效果更好:

library(shiny)

ui <- fluidPage(
  headerPanel('Fitting a curve'),
  sidebarPanel(
    selectInput(inputId = "xcol",label = "X-Axis",choices = names(mtcars)),
    selectInput(inputId = "ycol",label = "Y-Axis",choices = names(mtcars),selected = names(mtcars)[[3]]),
    sliderInput(inputId = "degree",label = "Degree of fit",min = 0,max = 2,value = 0)
  ),
  mainPanel(plotOutput("plot1"))

)

server <- function(input,output){
  x <- reactive({mtcars[,input$xcol]})
  y <- reactive({mtcars[,input$ycol]})
  z <- reactive({
    req(input$degree, x(), y())
    if (input$degree == 0) {
      lm(y() ~ 1)
    } else if (input$degree == 1) {
      lm(y() ~ x())
    } else lm(y() ~ x() + x()^2)
  })         
  output$plot1 <- renderPlot({
    plot(x(),y(),col = "red")
    abline(z())
  })

}

shinyApp(ui = ui,server = server)

【讨论】:

  • 常量模型的事情是我的拼写错误。道歉。给出的代码也可以正常工作。谢谢。但是,我似乎不了解帮助菜单中的请求和验证功能详细信息。有没有更好的解释?也许通过视频链接?
  • req 如果其参数不是“真实的”(简而言之:不为空,具有某种价值),将优雅地退出反应块。这通常在更复杂的闪亮应用程序中需要,其中一个块可能会在输入字段被实例化之前触发(即使输入被定义为始终具有某个值,这也会导致其值为NULL)。此外,如果其他条件不成立,您可以跳过块的执行。考虑req(cond) 与用if (!is.null(cond)) { ... } 包装整个反应块内部相同,其中is.null 实际上只是req 进行的一个测试。
  • ... 和validate 可以产生相同的效果,但让您能够以一种很好的方式通知用户某事。 (req 保持沉默。)
【解决方案2】:

这是你想要的吗?

library(shiny)

ui <- fluidPage(
    headerPanel('Fitting a curve'),
    sidebarPanel(
        selectInput(inputId = "xcol",label = "X-Axis",choices = names(mtcars)),
        selectInput(inputId = "ycol",label = "Y-Axis",choices = names(mtcars),selected = names(mtcars)[[3]]),
        sliderInput(inputId = "degree",label = "Degree of fit",min = 0,max = 2,value = 0)
    ),
    mainPanel(
        plotOutput("plot1")
        )

)

server <- function(input,output){

    x <- reactive({
        mtcars[,input$xcol]
    })

    y <- reactive({
        mtcars[,input$ycol]
    })

    z <- reactive({
        if(input$degree==0){
            return(lm(y()~1))
        }else if(input$degree == 1){
            return(lm(y()~x()))
        }else{
            return(lm(y()~x()+x()^2))
        }
    })

    output$plot1 <- renderPlot({
        plot(x(),y(),col = "red")
        abline(z())
    })

}

shinyApp(ui = ui,server = server)

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 2018-12-27
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多