R Shiny: флажок logi c (if / else) - Почему только результат последнего условия может быть показан один? - PullRequest
0 голосов
/ 03 мая 2020

Это упрощенная версия моего проекта. Он имеет два флажка: «простой» и «logisti c». Я надеюсь, что будет показана линия регрессии, когда установлен соответствующий флажок.

Вот что происходит, когда я запускаю свой код:

  • Когда установлены оба флажка: две линии регрессии показывают
  • Когда установлен только флажок «Простой» : простая линия регрессии показывает
  • НО, когда установлен только флажок 'logisti c': НИЧЕГО НЕ ПОКАЗЫВАЕТ

Я не понимаю, почему это происходит или как решить проблема. У кого-нибудь есть идеи?

Спасибо

server <- function(input, output) {

    output$linePlot <- renderPlotly({

        x<- seq(-10,10,0.1)
        y1<- x*input$b1+input$a1
        y2<- 1/(1+exp(-input$a2-input$b2*x))

        #plot
        p<-plot_ly()%>%
            layout(xaxis = list(range=c(-10,10)), yaxis = list(range=c(-32,32)))

        if (input$logistic){
            p<-p%>%
                add_trace(x=x,y=y2,type = 'scatter', mode = 'lines', name = "y2=1/(exp(-a2-b2*x))", line = list(color = 'rgb(22, 96, 167)'))
        }

        if (input$simple){
            p<-p%>%
                add_trace(x=x,y=y1,type = 'scatter', mode = 'lines', name = "y1=a1+b1*x",line = list(color = 'rgb(255, 129, 10)'))
        }
    })
}
ui <- fluidPage(

    # Application title
    titlePanel("Regression Line Simulator"),

    # Sidebar with four slider inputs for a1, a2, b1, b2
    sidebarLayout(
        sidebarPanel(
            # checkboxGroupInput("regression", label = "Type of regression", 
            #                    choices = list("Simple Linear" = 1, "Logistic" = 2),
            #                    selected = c(1,2)),
            # 
            checkboxInput("simple","Simple Regression", value = TRUE),
            checkboxInput("logistic", "Logistic Regression", value = TRUE),
            helpText("Select values on the sliders"),

            conditionalPanel(
                condition = "input.simple==true",
                sliderInput("a1","Select value for a1",min = -0.8,max = 0.8,value = 0,step = 0.1),
                sliderInput("b1","Select value for b1",min = -3,max = 3,value = 0,step = 0.1),
                sliderInput("sd1","Select the standard deviation for y1_data",min = 0.1,max = 2.0,value = 0.1,step = 0.1),
            ),

            conditionalPanel(
                condition = "input.logistic==true",
                sliderInput("a2", "Select value for a2",min = -0.8,max = 0.8,value = 0,step = 0.1),
                sliderInput("b2","Select value for b2",min = -3,max = 3,value = 0,step = 0.1),
                sliderInput("sd2","Select the standard deviation for y2_data",min = 0.1, max = 2.0,value = 0.1,step = 0.1)

            ),

            conditionalPanel(
                condition = "input.simple==true||input.logistic==true",
                sliderInput("size", "Select the sample size",min = 5,max = 20,value = 5,step = 1),
                actionButton("simulate", "Simulate!"),
                actionButton("clear", "Clear"))

        ),
            # Show a plot of the generated distribution
            mainPanel(
                plotlyOutput("linePlot")
            )
        )
    )

1 Ответ

0 голосов
/ 03 мая 2020

Я думаю, вы должны вернуть p в конце вашего renderPlotly:

output$linePlot <- renderPlotly({

    x<- seq(-10,10,0.1)
    y1<- x*input$b1+input$a1
    y2<- 1/(1+exp(-input$a2-input$b2*x))

    #plot
    p<-plot_ly()%>%
        layout(xaxis = list(range=c(-10,10)), yaxis = list(range=c(-32,32)))

    if (input$logistic){
        p<-p%>%
            add_trace(x=x,y=y2,type = 'scatter', mode = 'lines', name = "y2=1/(exp(-a2-b2*x))", line = list(color = 'rgb(22, 96, 167)'))
    }

    if (input$simple){
        p<-p%>%
            add_trace(x=x,y=y1,type = 'scatter', mode = 'lines', name = "y1=a1+b1*x",line = list(color = 'rgb(255, 129, 10)'))
    }

    p
})
...