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. 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, i.e. not equal. 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. 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. 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.
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")
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)