[英]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.