代码之家  ›  专栏  ›  技术社区  ›  Victor Johnzon

闪亮的反应if语句在输出中给出一个“[1]”。如何删除此项?

  •  1
  • Victor Johnzon  · 技术社区  · 8 年前

    我有一个闪亮的服务器代码,其中有一个for循环来检查用户是否输入了9。但每当用户输入9或什么都不输入时,我将得到一个包含[1]以及“”的输出

    如果输入nothing,则输出为

    [1]""
    

    [1]"You are not working good"
    

    如何避免这[1]以及双引号?

    下面是我的服务器。R代码

    library(shiny)
    
    shinyServer(function(input, output) {
    
    output$name <- renderText({input$name})
    
    
    output$whrs<-renderPrint({
    if (input$whrs == "") {
      ""
    } else  
    if(input$whrs == 9)  {
      "You are not working good"
     }
      })
    
    })
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Mal_a    8 年前

    这会让你继续前进:

    library(shiny)
    
    server <- function(input, output) {
    
      output$whrs<-renderText({
        if (input$text == "") {
          ""
        } else  
          if(input$text == 9)  {
            "You are not working good"
          }
      })
    
    }
    
    ui <- shinyUI(fluidPage(
      sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(selectInput("text","Enter text",choices=c("","1","9")),
                  textOutput("whrs"))
      )
    ))
    
    shinyApp(ui = ui, server = server)
    

    您还没有提供完整的代码,并且您已经提供的代码中存在一些错误,这就是为什么我刚刚创建了一个小示例,可以进一步帮助您。

    首先,你应该使用 renderText renderPrint --&燃气轮机;这就是为什么你会得到双引号和 [1]

    enter image description here

        2
  •  1
  •   akrun    8 年前

    另一种选择是使用 switch

    ui <- shinyUI(fluidPage(
      sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(selectInput("text","Enter text",choices=c("","1","9")),
                  textOutput("whrs"))
      )
    ))
    
    
    server <- function(input, output) {
    
      res <- reactive({
        switch(input$text, 
          `""` = "",     
          `9` =  "You are not working good",
          `1`  = NA
        )
    
    })
    
      output$whrs<-renderText({
             res()
        })
    }
    
    
    
    shinyApp(ui = ui, server = server)
    

    enter image description here

    enter image description here