R Shiny:如何通过服务器函数中的uiOutput()引用renderUI中的反应元素?

时间:2017-09-28 23:21:30

标签: r shiny

我试图让一个UI元素与另一个元素做出反应,然后测试以查看第一个元素是否等于一个特定值。我似乎能够打印出反应元素的值,但是我不能在if语句中使用它来检查它的值。代码如下。我已经注释掉了第23行周围的麻烦部分。它在注释掉的行中运行,并验证反应元素输入$ paramRes1是否具有1到5之间的整数字符值,正如预期的那样,但当我取消注释时在行中检查该整数值是否为1,它告诉我输入$ paramRes1为null。确切的错误是:

Warning:
Error in if: argument is of length zero
Stack trace (innermost first):
59: observerFunc [#16]
 4: <Anonymous>
 3: do.call
 2: print.shiny.appobj
 1: <Promise>

为什么输入$ paramRes1为空时会打印出来作为char?或者我误读了这个错误?谢谢你的帮助!

suppressWarnings(library(shiny))
suppressWarnings(library(shinyFiles))
    ui <- function(request) {
    fluidPage(
        uiOutput("param1"),
        uiOutput("param2"),
        fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
    )
}

server <- function(input, output, session) {
    state = reactiveValues()

    observe({
          state$choice <- setNames(as.list(c(1, 2, 3, 4, 5)), c("First", "Second",
            "Third", "Fourth", "Fifth"))
    })
    output$param1 <- renderUI({
        selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
    })
    output$param2 <- renderUI({
        textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
    })

    observe({
      #  if (input$paramRes1 %in% 1) {
            state$val <- input$paramRes1
      #  } else {
      #      state$val <- 0
      #  }

    })

    output$value <- renderText({ str(state$val) })
}
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:1)

以下是您的应用的(略)简化版本以及我在评论中建议的修复

library(shiny)

ui <- fluidPage(
  uiOutput("param1"),
  uiOutput("param2"),
  fluidRow(column(12, verbatimTextOutput("value", placeholder = T)))
)

server <- function(input, output, session) {
  state = reactiveValues(choice = list(
    first = 1, second = 2, third = 3, fourth = 4, fifth = 5
  ))
  output$param1 <- renderUI({
    selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
  })
  output$param2 <- renderUI({
    textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
  })
  observeEvent(input$paramRes1,{
    if (input$paramRes1 %in% 1)
      state$val <- "It's 1"
    else
      state$val <- "It's not 1"
  })
  output$value <- renderText({ state$val })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

以下工作会怎样?我只是在这里更改服务器部分。差异在######之间突出显示。

server <- function(input, output, session) {
  state = reactiveValues()

  observe({
    state$choice <- setNames(as.list(c(1, 2, 3, 4, 5)), c("First", "Second",
                                                      "Third", "Fourth", "Fifth"))
   })
  output$param1 <- renderUI({
    selectInput("paramRes1", "Select Param 1", state$choice, selected = 1)
  })
  output$param2 <- renderUI({
    textInput("paramNm", "Param", value = input$paramRes1, width = '150px')
  })

#####
  observeEvent(input$paramRes1, {
     if (input$paramRes1 == 1) {
     state$val <- input$paramRes1
      } else {
     state$val <- 0
      }
  })
###### 

   output$value <- renderText({ str(state$val) })
 }