I created simple text prediction application with Shiny.Everything works fine but now I need to output HTML sort of links instead of plain text output,where by clicking on the link of the output can help me go to that particular line in the document I uploaded.
ui<- shinyUI(fluidPage(
# Application title
mainPanel(
img(src='image.jpg', align = "right"),
#titlePanel(title=div(img(src="image.jpg"))),
fluidRow(HTML("<strong> Search Bar")),
fluidRow(HTML("<strong>Enter a word.Click \"SEARCH\" after</strong>") ),
fluidRow( p("\n") ),
# Sidebar layout
sidebarLayout(
sidebarPanel(
textInput("inputString","Enter a word here",value = " "),
submitButton("Search")
),
mainPanel(
h4("Are you looking for this ?"),
tags$style("#mytext {white-space: pre-line;}"),
verbatimTextOutput("mytext")
)
)
)))
server <- function(input, output, session) {
output$mytext <- renderPrint({
sentences <- rep(make_sentences(input$inputString))
length(sentences)
cat(paste0(1:length(sentences)," ",sentences,sep= '\n'))
})
}
What could be the possible changes I could do to get the output I am looking for ?
I am not able to figure out how to link the HTML hyper links to my text file
text_sherlock <- HTML(text_sherlock)
text_sherlock <- a(text_sherlock,href ='war.txt')
html_print(text_sherlock) # gave me the output with hyper links but as a full text and not individual words even though i split it before with `strsplit`
but when using the above HTML with markovchainFit i am getting error as : not compatible with STRSXP
It is really difficult to help and show you as I cannot reproduce in the first place.
I don't have much information on your specific case to help further. Here is an example with renderUI to create a HTML link depending on a value you determine on the server side. You can type a text in the input box and it will be used as the text to the link (pointing to shiny website). You can modify to also modify the link.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("text", label = "Write a text and this will be the text for the link below"),
),
mainPanel(
uiOutput("link")
)
)
)
server <- function(input, output, session) {
output$link <- renderUI(a(input$text, href = "https://shiny.rstudio.com"))
}
shinyApp(ui, server)
Thank you for clarification . I had understood how to use it but I guess my algorithm is still giving error saying compatibility issue with -STRSXP or Text to be written must be a length-one character vector- when I want to get hyper link output . I think I would have to redo everything and find another algorithm which can work in this scenario.
One last question is how can I use my uploaded text as href instead of rstudio.com website you gave here ?is there any way for this ? or I could use it to websites only ?
Sorry but I am not sur to have understand it... can you be more precise on what you want ?
This means probably that you are passing a vector to a function that is not vectorize and expect a single value. Try to use *apply family or purrr functions to iterate maybe.
In my code I had uploaded a text file called 'war.txt' through which text search works.Only words searched from this text file will be given output .Words not found in the text file will not give any output.
So clicking on the hyperlink ,user should go to my uploaded text file 'war.txt' instead of a website like google.com or xyz.com for example. Would it be possible ?
I don't know if you do want to link to external texts, in some dynamic way...
at first I thought you just wanted a convenient way to browse to words in a text.
library(shiny)
library(tidyverse)
textcontents <- "1 a text
2 b for because
3 compile vectors with c
4 d for diamond
5 e for example
6 find the f
7 good g "
(split_text_contents <- unlist(strsplit(textcontents,split="\n",fixed=TRUE)))
(locations <- data.frame(
content = c("b","f"),
line = c(2,6)
))
ui <- fluidPage(
selectInput(inputId = "wordchoice",
label="choose a word to navigate to",
choices=locations$content),
verbatimTextOutput(outputId = "viewtext")
)
server <- function(input, output, session) {
output$viewtext<-renderText({
wc <- req(input$wordchoice)
line_index <- filter(locations,
content==wc) %>% pull(line)
if(line_index>1)
min_line <- line_index - 1
else
min_line <- line_index
if(line_index<7)
max_line <- line_index + 1
else
max_line <- line_index
paste0(split_text_contents[min_line:max_line],collapse = "\n")
})
}
shinyApp(ui, server)
I am getting the desired output . As a final use case solution to complete, I needed the sentences output in form of hyper links where by the user can go to that sentence in the text file I uploaded . Similar to ms-word(ctrl +f) or pdf word search feature..