[英]How to Compare TextInput in Shiny for Equality with Another Object?
我一直在编写一个Shiny App,它部分涉及用户的文本输入与写入代码的向量或字符串的比较。 然而,我无法让它工作,我不知道为什么,因为它没有错误。 它将简单地打印/粘贴比较为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)
以下是我认为您的追求:
(尝试输入“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.