Как сравнить TextInput в Shiny для равенства с другим объектом? - PullRequest
0 голосов
/ 14 мая 2019

Я пишу блестящее приложение, которое включает, в частности, сравнение ввода текста пользователя с вектором или строкой, записанной в коде. Я не смог заставить это работать, однако, и я в растерянности относительно того, почему, поскольку это не выдает никаких ошибок. Он просто напечатает / вставит условие, указанное для сравнения, когда FALSE, т.е. не равно. Когда я выполняю все шаги по базе R (за исключением всего, что связано с Shiny), он возвращает TRUE, поэтому я не уверен, что теряется при переводе между вводом и сравнением с объектом R. Я пробовал и isTRUE (all.equal (...)), и идентичные (...) и isTRUE (идентичные (...)), и ни один из них, кажется, не работает или возвращает условие 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)

1 Ответ

0 голосов
/ 14 мая 2019

Вот то, что я думаю, что вы после:

(попробуйте ввести «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)
...