为什么ShinyApps中的seq()函数不起作用?

问题描述 投票:0回答:1

我尝试在Apps中使用seq()函数创建ShinyApp。

header <- dashboardHeader(title = 'Testing' ,titleWidth = 300)
sidebar <- dashboardSidebar(uiOutput("sidebarpanel"), width = 300)
body <- dashboardBody(uiOutput("body"))
uix <- dashboardPage(header, sidebar, body)

serverx <- function(input, output, session){

  output$sidebarpanel <- renderUI({
    div(
      sidebarMenu(id="tabs",
                  menuItem("Tes 1", tabName = "tes1", icon = icon("dashboard"), selected = TRUE)
                  )
    )
  })

  output$body <- renderUI({
    tabItems(tabItem(tabName = "tes1",
                      fluidRow(column(2, textInput("s1", "From :", value = 1))
                                ,column(2, textInput("s2", "To", value = 7))
                      ),
                      textOutput("result")
             )
    )
    })

  segment_low <- reactiveValues(ba=NULL)
  segment_high <- reactiveValues(ba=NULL)
  results <- reactiveValues(ba=NULL)

  toListen <- reactive({
    list(input$s1, input$s2)
  })

   observeEvent(toListen(),{
    segment_low$ba <- input$s1 %>% as.numeric()
    segment_high$ba <- input$s2 %>% as.numeric()
  })

  observe({
    results$ba <- seq(segment_low$ba,segment_high$ba, 1)
  })

  output$result <- renderText({
    results$ba 
  })

  }

shinyApp(uix, serverx)

使用该语法,我将创建一个名为results$ba的变量,因为我想在下一次升级该值。但是结果是一个错误:

Warning: Error in seq.default: 'from' must be of length 1
  [No stack trace available]

有人可以帮我解决这个问题吗?由于如果我将reactValueValues放入seq()函数,则只会发生此错误,因此在输入静态输入(例如seq(2,5,1))时,它不会返回错误。而且我已经将每个输入的初始value都放在了textInput()函数中。

Kindle需要您的帮助,开发人员!非常感谢。

r shiny syntax-error sequence seq
1个回答
1
投票

问题是您正在服务器端渲染s1s2输入。因此,服务器在开始时将其呈现为NULL,并且在获取seq值时,NULL函数会出错。

最简单的操作是添加一个req函数,以防止您的代码进行求值,除非它得到一些非NULL值。

    observe({
        req(segment_low$ba, segment_high$ba)
        results$ba <- seq(segment_low$ba,segment_high$ba, 1)
    })

[基本上,因为您正在使用观察,所以非常急切,因此您正在告诉seq函数立即求值。通过使用req函数,您可以阻止评估链发生,除非segment_low $ ba和segment_high $ ba具有非NULL值。

© www.soinside.com 2019 - 2024. All rights reserved.