【问题标题】:R Shiny Application Conditional calculations and panel with condition on the outputR Shiny Application 条件计算和面板与输出条件
【发布时间】:2020-09-29 10:04:08
【问题描述】:

我是 Shiny 的新手。我想要在我的应用程序中做的是,仅在满足另一个计算的条件时运行和显示部分代码。

conditionalPanel 适用于输入值的条件,但我无法弄清楚如何使用“输出”值执行此操作,即有条件地根据函数的输出值。下面是我的示例代码:

library(shiny)
msLocation <- "msLoc"
searchMWText <- "searchMW"
bid <- "2333333"
fulltext <- "fullDisplay"

ui <- fluidPage(
  titlePanel("Run server codes conditionally"),

  sidebarLayout(
    sidebarPanel(
      helpText("Evaluate input and run different parts of the code depending on the output functions"),
      br(),
      sliderInput("rand", "select seed", min = 1, max = 50, step = 1, value = 1)
     ),

mainPanel(
  fluidRow(conditionalPanel("output.rand == 1"),
           tags$h4("Here comes the default part"),
           br(),
           textOutput("defaultCalc")),
  
  fluidRow(conditionalPanel("output.randomint != 1",
                            tags$h4("I can evaluate if the chosen number is even or odd."),
                            br(),
                            textOutput("evenodd")
                            ),
           fluidRow(conditionalPanel("output.evenodd == 'Number is even'",
                                    tags$h4("Number even calculation "),
                                    textOutput("msLoc"),
                                    br(),
                                    textOutput("searchMW"),
                                    br(),
                                    textOutput("defaultID"),
                                    br()
                                    ),
           fluidRow(conditionalPanel("output.evenodd == 'Number is odd'",
                                     tags$h4("Here is some id:", textOutput("id")),
                                     textOutput("displayFull")
                                     )
           )
  

  )
)
)))
#
server <- function(input, output) {
  rand1 <- reactive({
    if(is.null(input$rand)){return(NULL)}
    rn <- input$rand
    return(rn)
  })

  randomint <- reactive({
    seedn <- rand1()
    set.seed(seedn)
    rint <- sample(1:50, 1)
    return(rint)
  })

  calc1 <- reactive({
    intn <- randomint()
    modn <- intn %% 2
    return(modn)
  })

  evenOdd <- reactive({
    modn <- calc1()
    if(modn == 0){valueText = "Number is even"}
    if(modn != 0){valueText = "Number is odd"}
    return(valueText)
  })

  idtext <- reactive({
    idint <- sample(1:10000, 3)
    idint <- as.character(idint)
    idint <- paste(idint, collapse = "")
    return(idint)
  })

  output$defaultCalc <- renderText({
    as.character(randomint())
  })

  output$evenodd <- renderText({
    evenOdd()
  })

  output$searchMW <- renderText({
    searchMWText
  })

  output$defaultID <- renderText({
    bid
  })

  output$id <- renderText({
    idtext()
  })

  output$displayFull <- renderText({
    fulltext
  })

  }

  shinyApp(ui = ui, server = server)

问题是,默认后的部分总是出现,例如,'Here is some id' 文本总是出现,这不是我想要的。我想显示“这是一些 id”并仅在数字为奇数时运行计算(idtext)。数字不是来自滑块输入,滑块输入仅提供种子。该数字也被计算并取决于其值,其他部分应运行并显示。在用户选择滑块输入值之前,应该只显示“默认部分”而不显示其他任何内容。

我搜索了很多,找不到提及输出条件的解决方案。解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    做:

      randomint <- reactive({
        seedn <- rand1()
        set.seed(seedn)
        rint <- sample(1:50, 1)
        return(rint)
      })
      output$randomint <- reactive(randomint())
      outputOptions(output, "randomint", suspendWhenHidden = FALSE)
    

    那么你可以使用"output.randomint !== 1"

    【讨论】:

    • 我收到一个错误,“随机数不在输出对象列表中”。
    • @maop 是的,对不起。你必须做output$randomint &lt;- reactive......
    • 非常感谢。我摆脱了错误,但我仍然无法获得我想要的输出。无论数字是奇数还是偶数,我仍然有“这是一些 id”。
    • @maop 您的情况缺少单引号。
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2016-06-04
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多