繁体   English   中英

server.R 中的条件语句

[英]Conditional statement in server.R

不知道我在这里做错了什么。 由于长度(键)为 0,我需要将selectedInput的值分配给key_as 如果长度(键)不是0,那么key_as应该是“a”

例如,在这种情况下,由于 length(key) = 0,无论用户从下拉列表中选择什么(比如“A”),key_as 都应该是“A”。 但是如果 length(key) !=0 那么 key_as 的值应该是“a”(key_1 的值)

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            uiOutput("edit_condition")
        )
    )
))

server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

    columnNames <- c("A","B","C")
    key <- c()  # This is just an example
    key_1 <- c("a")
    # print(length(key))

    if(length(key) == 0){
        output$edit_condition <- renderUI({
            selectInput("ed", "Condition", choices = c(columnNames), selected = columnNames[[1]])
        })
        key_as <- input$ed
    } else {
        key_as <- key_1
    }

    print(key_as)
})

尝试过的解决方案


    key_as <- reactive({
        if(length(key) == 0){
            input$ed
        } else {
            key_1
        }
    })

我不确定这是您的想法,但也许这将有助于进一步讨论。

您可以使用将在input$ed更改时调用的observeEvent 您还可以为key_ascolumn_names设置reactiveValues ,这样两者都可以随输入而改变。

让我知道这是否更接近。

shinyServer(function(input, output) {

  rv <- reactiveValues(key_as = NULL, column_names = c("A","B","C"))

  observeEvent(input$ed, {
    key <- c()
    key_1 <- c("a")

    if(length(key) == 0){
      rv$key_as <- input$ed
    } else {
      rv$key_as <- key_1
    }

    print(rv$key_as)
  })

  output$edit_condition <- renderUI({
    selectInput("ed", "Condition", choices = rv$column_names, selected = rv$column_names[[1]])
  })

})

这是一个完整的示例,它将在selectInput更改时打印 output 中的rv$key_as

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      uiOutput("edit_condition"),
      textOutput("text_output")
    )
  )
)

server <- function(input, output) {

  rv <- reactiveValues(key_as = NULL, column_names = c("A","B","C"))

  observeEvent(input$ed, {
    key <- c()
    key_1 <- c("a")

    if(length(key) == 0){
      rv$key_as <- input$ed
    } else {
      rv$key_as <- key_1
    }

    print(rv$key_as)
  })

  output$text_output <- renderText({
    rv$key_as
  })

  output$edit_condition <- renderUI({
    selectInput("ed", "Condition", choices = rv$column_names, selected = rv$column_names[[1]])
  })

}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM