How to create HTML links for text prediction app ?

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 think you should try shiny::renderUI() to create some HTML output to add in UI using shiny::uiOutput(), instead of Verbatim output and renderPrint

in renderUI you can render

expression that returns a Shiny tag object, HTML() , or a list of such objects.

You should create your text as link using HTML() or building one using shiny::a("my_doc", href = "link") for example.

See the help page for more example.

I think those tools is what to use to solve your issue.

Hope it helps.

any chance you could edit the code pls ? I am getting no output with changes

What did you try already ?

You need to use renderUI in the UI side, and uiOutput in the server side.
Then before that, try build your HTML code as is using HTML or shinyTag

If you share what you tried, I can maybe advice.

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

server <- function(input, output, session) {
  output$mytext <- renderUI({
    
    sentences <- rep(make_sentences(input$inputString))
    length(sentences)
    cat(paste0(1:length(sentences),"  ",sentences,sep= '\n'))
  })
}

ui<- shinyUI(fluidPage(
  
  # Application title
  mainPanel(

  fluidRow(HTML("<strong>  Guide Search Bar")),

  
  fluidRow(
    br(),
    p("User Name : ")),
  br(),
  br(),
  
  fluidRow(HTML("<strong>Enter a word.Click \"Next words\" after</strong>") ),
  fluidRow( p("\n") ),
  
  # Sidebar layout
  sidebarLayout(
    
    sidebarPanel(
      textInput("inputString","Enter a word here",value = " "),
      submitButton("Next words")
    ),
    
    mainPanel(
      h4("Are you looking for this ?"),
      tags$style("#mytext {white-space: pre-line;}"),
             uiOutput("mytext")
      
    )
  )
)))

I was trying to put HTML for make_sentences( after the alogrithm) but getting error there too

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)

I hope it helps understand.

1 Like

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 ?

Do you want user to be able to download the file ? Like File Downloads — downloadHandler

Or you can also serve your file as a static file by putting it in www folder I think.
See Resource Publishing — addResourcePath

Static files under the www/ directory are automatically made available under a request path that begins with / .

That way you can create a link to the file hosted by your app.

1 Like

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)
1 Like

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..

txt files dont support jumping to specific parts based on url hrefs. it would have to be a html file to support such anchors.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.