r - 在 Shiny 中使用 IF 创建函数
问题描述
我有一个函数可以突出显示与用户在两个 textInput 小部件中输入的内容相对应的引号中的文本。如果第二个 textInput 字段中没有文本输入,则返回的结果会突出显示第一个空格,这是不可取的。
我试图在我的函数中使用 if else 语句但没有成功。
highlight <- function(text, search1, search2) if (search2 != ""){
x <- unlist(strsplit(text, split = " ", fixed = T))
x[tolower(x) %in% tolower(c(search1, search2))] <- paste0("<mark>",
x[tolower(x) %in% tolower(c(search1, search2))], "</mark>")
paste(x, collapse = " ")
} else
x <- unlist(strsplit(text, split = " ", fixed = T))
x[tolower(x) %in% tolower(c(search1))] <- paste0("<mark>", x[tolower(x) %in% tolower(c(search1))], "</mark>")
paste(x, collapse = " ")
library(shiny)
library(shinydashboard)
highlight <- function(text, search1, search2) {
x <- unlist(strsplit(text, split = " ", fixed = T))
x[tolower(x) %in% tolower(c(search1, search2))] <- paste0("<mark>",
x[tolower(x) %in% tolower(c(search1, search2))], "</mark>")
paste(x, collapse = " ")
}
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("TexSearch", tabName = "Tabs", icon = icon("object-ungroup"))
)
),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="Search ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
textInput("quoteSearch1", " Search ", '', placeholder = "Type keyword/statement"),
textInput("quoteSearch2", " Search ", '', placeholder = "Type keyword/statement"),
submitButton("Search")
)
),
column( width=9,
tabBox(
width="100%",
tabPanel("tab1",
htmlOutput("quotesearchdetails")
)))))))
server <- function(input, output) {
output$quotesearchdetails <-renderUI({
outputed=""
author <- c('John Cage','Thomas Carlyle','Elbert Hubbard', 'Albert Einstein')
quote <- c('I cant understand why people are frightened of new ideas. Im frightened of the old ones.','The tragedy of life is not so much what men suffer, but rather what they miss.','The greatest mistake you can make in life is to be continually fearing you will make one.', 'Anyone who has never made a mistake has never tried anything new.')
quotes <- data.frame(author, quote)
if(input$quoteSearch1!="" | input$quoteSearch2!=""){
words<-strsplit(input$quoteSearch1,input$quoteSearch2,",")
words<-as.character(words[[1]])
words<-tolower(words)
for(i in 1:length(words)){
quotes<-quotes[
grepl(words[i],quotes$quote),]
}
if (dim(quotes)[1]>0){
for(i in seq(from=1,to=dim(quotes)[1])){
outputed<-paste(outputed,
paste("Author: ",quotes[i,"author"]),
sep="<br/><br/>")
outputed<-paste(outputed,
highlight( paste("Quote: ",quotes[i,"quote"]), input$quoteSearch1, input$quoteSearch2),
sep="<br/><br/>")
}
} else {outputed- "No quotes found."}
}
HTML(outputed)
})
}
shinyApp(ui, server)
理想的情况是,如果用户仅从 input$quoteSearch1 进行搜索,则显示的结果不会突出显示引号的第一个空格,而仅显示带有搜索词的引号。
即使 input$quoteSearch2 中没有文本,关于如何编写我的函数以使其工作的任何见解?
解决方案
search2
我认为如果它包含一个空字符串,你可以简单地用 NA 覆盖:
highlight <- function(text, search1, search2) {
if (search2 == "") search2 <- NA
x <- unlist(strsplit(text, split = " ", fixed = T))
x[tolower(x) %in% tolower(c(search1, search2))] <-
paste0("_", x[tolower(x) %in% tolower(c(search1, search2))], "_")
paste(x, collapse = " ")
}
请注意,我用下划线填充了所选单词以检查该功能是否有效。当然,您可以将其更改为您喜欢的任何内容。
> highlight("I like cats", "cats", "")
[1] "I like _cats_"
推荐阅读
- python-3.x - 检查主机是否连接到局域网
- php - 在结帐提交时提取 woocommerce 结帐名字字段值
- sql - 具有非唯一索引的 CREATE TABLE 查询
- python-3.x - 安装 IJulia 的问题
- node.js - 我想删除一个帖子,但有一些我似乎无法弄清楚的错误。如何使用 DELETE 从数据库中删除数据?
- lambda - NetLogo 匿名程序的问题
- python - 在 Datagrip 中将 Snowflake 数据库连接到 R 和 Python
- ios - Xcode 11 Swift 5 - 无法安装应用程序(无法加载 info.plist 文件)
- android - Android grpc 错误:TLS ALPN 协商失败,协议:[grpc-exp,h2]
- excel - 文件保存读取正确但抛出没有描述的错误