【发布时间】: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