【问题标题】:How to Compare TextInput in Shiny for Equality with Another Object?如何将 Shiny 中的 TextInput 与另一个对象进行比较?
【发布时间】:2019-09-30 20:52:35
【问题描述】:

我一直在编写一个闪亮的应用程序,其中部分涉及将用户的文本输入与写入代码的向量或字符串进行比较。然而,我无法让它工作,我不知道为什么,因为它没有引发错误。当比较为 FALSE(即不相等)时,它将简单地打印/粘贴指定的条件。当我通过基础 R 运行所有步骤(减去与 Shiny 相关的任何步骤)时,它确实返回 TRUE,所以我不确定在输入和与 R 对象之间的转换中丢失了什么。我已经尝试过 isTRUE(all.equal(...)) 和 same(...) 和 isTRUE(identical(...)),但它们似乎都不起作用或返回 FALSE 条件。我在下面包含了代码,其中包含它的一个变体 - 我一直使用“ding”作为输入的比较,就像一些简短且易于输入的内容来测试它。

不胜感激,不胜感激!


library(shiny)
library(stringr)

site <- c(rep("A",5), rep("B",5), rep("C",5), rep("D",5))

my.num <- 1:20

temp <- rnorm(20, 5, 1)

growth <- 5*temp + rnorm(20,0,2)


my.data <- data.frame(site=site, my.num=my.num, temp=temp, growth=growth)

my.data

ui <- pageWithSidebar(
  headerPanel('Data Wrangler')
  ,
  sidebarPanel(
    textInput("combination", "Combine the several variables into one data frame with R code:", NULL),
    actionButton("go5", "GO!")
  )
  ,
  mainPanel(

    tableOutput("display1"),
    textOutput("text.dsp")

  ))

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

  buttonValue <- reactiveValues(go5=FALSE)


  observeEvent( input$go5, {

str.input <- str_extract(input$combination, "data.frame(site=site, my.num=my.num, temp=temp, growth=growth)")
str2.input <- as.character(str_extract(input$combination, "ding")
comparestring <- "ding"

isolate({

  buttonValue$go5 = FALSE


})
output$display1 <- renderTable({


  if (isTRUE(identical(str.input, "data.frame(site=site, my.num=my.num, temp=temp, growth=growth)")) & buttonValue$go5) {
    my.data
  } else if(isTRUE(all.equal(str2.input,comparestring)) & buttonValue$go5){
    my.data
  } else {
    NULL
  }

})

  })


  session$onSessionEnded({
    print("stop")
    stopApp   
  }) 

}



  shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r string shiny comparison


    【解决方案1】:

    这就是我认为你所追求的:

    (尝试输入“ding”并按“go”)

    library(shiny)
    
    site <- c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5))
    my.num <- 1:20
    temp <- rnorm(20, 5, 1)
    growth <- 5*temp + rnorm(20, 0, 2)
    
    my.data <- data.frame(site = site, my.num = my.num, temp = temp, growth = growth)
    
    ui <- pageWithSidebar(
      headerPanel('Data Wrangler'), 
      sidebarPanel(
        textInput("combination", "Combine the several variables into one data frame with R code:", NULL), 
        actionButton("go5", "GO!")
      ), 
      mainPanel(
        tableOutput("display1"), 
        textOutput("text.dsp")
      ))
    
    server <- function(input, output, session) {
    
      tableData <- eventReactive(input$go5, {
        if(input$combination == "ding"){
          return(my.data)
        } else if(input$combination == "data.frame(site = site, my.num = my.num, temp = temp, growth = growth)"){
          return(my.data)
        } else {
          return(NULL)
        }
      })
    
      output$display1 <- renderTable({
        tableData()
      })  
    
      session$onSessionEnded({
        stopApp
      }) 
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 非常感谢 - 成功了!我没有想到“observeEvent”可能是问题所在——我对 Shiny 还比较陌生,并且仍在尝试充分理解 observeEvent、eventReactive 和 co.. 之间的区别。
    • 当然,您可能对this 感兴趣。
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2015-05-23
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2020-03-24
    相关资源
    最近更新 更多