简体   繁体   English

如何比较闪亮的TextInput与另一个对象的平等?

[英]How to Compare TextInput in Shiny for Equality with Another Object?

I have been writing a Shiny App which involves, in part, the comparison of a user's text input with a vector or string, written into the code. 我一直在编写一个Shiny App,它部分涉及用户的文本输入与写入代码的向量或字符串的比较。 I have not been able to get this to work, however, and I am at a loss as to why, as it throws no errors. 然而,我无法让它工作,我不知道为什么,因为它没有错误。 It will simply print/paste the condition specified for when the comparison is FALSE, ie not equal. 它将简单地打印/粘贴比较为FALSE时指定的条件,即不相等。 When I run all the steps through base R (minus anything to do with Shiny), it does return TRUE, so I am not sure what is getting lost in the translation between input and comparing to an R object. 当我通过基本R运行所有步骤(减去与Shiny有关的所有步骤)时,它确实返回TRUE,所以我不确定在输入和比较R对象之间的转换中会丢失什么。 I have tried both isTRUE(all.equal(...)) and identical(...) and isTRUE(identical(...)), and none of it seems to work or is returning a FALSE condition. 我已经尝试了两个isTRUE(all.equal(...))和相同的(...)和isTRUE(相同(...)),它们似乎都没有工作或返回一个FALSE条件。 I've included code below which includes a variation of it - I have been using "ding" as a comparison to the input, just as something short and easy to type in to test it. 我已经包含了下面的代码,其中包含了它的一个变体 - 我一直在使用“ding”作为输入的比较,就像一些简短的输入来测试它。

Any help would be much appreciated, at my wit's end! 在我的智慧结束时,我将非常感谢任何帮助!


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)

Here is what I think you are after: 以下是我认为您的追求:

(try typing in "ding" and press "go") (尝试输入“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)

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

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