textoutput as hyperlink

library(shiny)

info_360 <- read.csv('data/360_photos.csv')
ui <-
fluidRow(
box(
title = "Key Facts",
closable = FALSE,
width = 9,
status = "primary",
solidHeader = FALSE,
collapsible = TRUE,
textOutput("keyfacts"))

server <- function(input, output,session) {
Keyfactstext <- reactive({
if (input$mySliderText %in% info_360$press )
{
info_360 %>%
filter(press == input$mySliderText)%>%
pull(Key_facts) #this contains a text that includes a website link, I need only the link to appear as a hyperlink?????????????????
}
})

output$keyfacts<- renderText({ Keyfactstext ()})

}

shinyApp(ui = ui, server = server)

This code pulls a single row from the file I already had, so I modified the row selection process to just grab that row. The URL in that file can be identified as the text between single quotes and I used functions from the stringr package to extract it. The csv file looks like this

press,more
"A","window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf')"

Here is the shiny code:

library(shiny)
library(shinydashboard)
library(stringr)
info_360 <- read.csv('Data/360_photos.csv')
ui <-
  fluidRow(
    box(
      title = "Key Facts",
      closable = FALSE,
      width = 9,
      status = "primary",
      solidHeader = FALSE,
      collapsible = TRUE,
      uiOutput("keyfacts")))
    
    server <- function(input, output,session) {
      Keyfactstext <- reactive({
        if (info_360$press == "A")
        {
          info_360 %>%
            filter(press == "A")%>%
            pull(more) |> 
            str_extract(pattern = "'.+'") |>
            str_remove_all("'")
        }
      })
      
      output$keyfacts <- renderUI({
        tagList("URL link:", a("Key Facts", href=Keyfactstext()))
      })
    }
    shinyApp(ui = ui, server = server)

Thank you very much, this is will be definitely helpful, but not the whole text from CSV should be hyperlinked, I mean part of it something like this:

For further information see the film Gezira Irrigation Scheme – A Struggle for Revival:

I cannot tell from your last post what the text in your csv is and what part you want to use as a link. Please post the original text inside of a code block with back ticks on each side, like this
```
the original csv text goes here
```
and then do the same to show the part of the text that you want to use as a link. I can then try touse string manipulation functions, as I did in my previous code to extract the desired text.

I will try to use the same example as you did

press,more
"A"," For further information see the film Gezira Irrigation Scheme – A Struggle for Revival on the following link window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf')"

I modified the csv file to contain the line you provided

press,more
"A"," For further information see the film Gezira Irrigation Scheme – A Struggle for Revival on the following link window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf')"

and my code works as intended. Is there some aspect of the shiny app web page that is not what you want?

the final output should contain both text "For further information see the film Gezira Irrigation Scheme – A Struggle for Revival on the following link" then the hyperlink "window.open('http://nilebasin.org/nileis/system/files/DSS%20Booklet_web.pdf')"
what I understood from your example the final output will be just the hyperlink

I am really grateful for your generous support!

Here is a rough version. You may want to show text other than the actual url after For further information see the film Gezira Irrigation Scheme – A Struggle for Revival on the following link. You can do that by replacing the first Keyfactstext()[2] in a(Keyfactstext()[2], href=Keyfactstext()[2])with alternative text.

library(shiny)
library(shinydashboard)
library(stringr)
info_360 <- read.csv('Data/360_photos.csv')
ui <-
  fluidRow(
    box(
      title = "Key Facts",
      closable = FALSE,
      width = 9,
      status = "primary",
      solidHeader = FALSE,
      collapsible = TRUE,
      uiOutput("keyfacts")))
    
    server <- function(input, output,session) {
      Keyfactstext <- reactive({
        if (info_360$press == "A")
        {
          tmp <- info_360 %>%
            filter(press == "A")%>%
            pull(more) |> 
            str_split("window.open\\(") #tmp is a list containing a vector
          tmp[[1]][2] <- tmp[[1]][2] |> str_extract(pattern = "'.+'") |>
            str_remove_all("'") #cleaning the url, the second element of the vector
          return(tmp[[1]]) #return the vector with the text and the cleaned url
        }
      })
      
      output$keyfacts <- renderUI({
        tagList(Keyfactstext()[1], a(Keyfactstext()[2], href=Keyfactstext()[2]))
      })
    }
    shinyApp(ui = ui, server = server)

Again I am really grateful for your help and generous support.
Your provided code is perfect. But I discovered that for my web application it's not the best idea to pull text from CSV, instead, I am trying to replace this idea by extracting the text from Rmarkdown, I am not sure how this method is going to work
Assuming I have 2 r Rmarkdown files,
When the user selects a certain input for example "1", I want to pull the first Rmarkdown file then,
For each box in my dashboard, I want a specific section to be extracted from the selected Rmarkdown according to the box name.
I would be so happy if you could assist me as usual !

library(shiny)
library(shinydashboard)
library(knitr)
ui <- 
  dashboardPage(
    dashboardHeader(title ='Virtual Excursion'), 
    
    dashboardSidebar( sliderTextInput(
      inputId = "mySliderText", 
      label = "Story line", 
      grid = TRUE, 
      force_edges = TRUE,
      choices = c('1','2')
    )
    ),
    dashboardBody(
      fluidRow(
        column(9,  
               box(
                 title = "Operations ", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("operations")
               )
        )
      ), 
      fluidRow(
        column(9, 
               box(
                 title = "Challenges", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("challenges")
          )
         ) 
        )
       )
      )
       

server <- function(input, output,session) {
 
  ChoseTheFile <- reactive({
    if (input$mySliderText == 1 )
    {
     # chose the first Rmarkdown file 
     else
     # chose the second Rmarkdown file  
      
    }
  }) 
  
  output$operations <- renderUI({
   #1  knit ChoseTheFile()
   #2  chose section 1 from ChoseTheFile()
    
  })

  output$challenges<- renderUI({
    #1  knit ChoseTheFile()
    #2  chose section 2 from ChoseTheFile()
    
  })
  
}

shinyApp(ui = ui, server = server)
1 Like

An rmd file is just plain text. One way to read that in is with the readLines(). that will result in a vector where each element is one line from the file. For example, I have a file named NMDS.rmd and I got the following result.

RMD <- readLines("NMDS.rmd")
> RMD[1:10]
 [1] "---"                               
 [2] "title: \"Pretend NMDS\""           
 [3] "author: \"fjcc\""                  
 [4] "date: \"3/29/2021\""               
 [5] "output: html_document"             
 [6] "---"                               
 [7] ""                                  
 [8] "```{r setup, include=FALSE}"       
 [9] "knitr::opts_chunk$set(echo = TRUE)"
[10] "```"                               

It should be possible to find the elements of the vector that contain the information you want but I cannot be more specific without knowing what you are looking for and how the rmd file is structured. The general method should be much like using a csv file; you just have to find the right vector element.

Assuming I have 2 rmds:

RMD1 <- readLines("virtual_excursion1.rmd")
RMD2 <- readLines("virtual_excursion2.rmd")

 RMD1[1:20]
 [1] "---"                                                                                
 [2] "title: \"Virtual Excursion\""                                                       
 [3] "author: \"Mohamed Osman\""                                                          
 [4] "date: \"3/7/2022\""                                                                 
 [5] "output: html_document"                                                              
 [6] "---"                                                                                
 [7] ""                                                                                   
 [8] "```{r setup, include=FALSE}"                                                        
 [9] "knitr::opts_chunk$set(echo = TRUE)"                                                 
[10] "```"                                                                                
[11] ""                                                                                   
[12] "# Section 1"                                                                        
[13] ""                                                                                   
[14] "Section 1. For more details on using R Markdown see <http://rmarkdown.rstudio.com>."
[15] ""                                                                                   
[16] ""                                                                                   
[17] "# Section 2"                                                                        
[18] ""                                                                                   
[19] "Section 2. For more details on using R Markdown see <http://rmarkdown.rstudio.com>."
[20] ""                                                                                   

for uiOutput("operations") I want the content of section 1 to be inside the box.
and for uiOutput("challenges") I want the content of section 2 to be inside the box.

also before that I need to extract either RMD1 or RMD2 according to (inputId = "mySliderText", '1' and '2')

Thnak you very much !

Using the RMD1 example you provided, we can extract the Section 1. text with grep()

RMD1 <- c(
  "---",                                                                             
  "title: \"Virtual Excursion\"",
  "author: \"Mohamed Osman\"",                                                          
  "date: \"3/7/2022\"",                                                                 
  "output: html_document",                                                              
  "---",                                                                                
  "",                                                                                   
  "```{r setup, include=FALSE}",                                                        
  "knitr::opts_chunk$set(echo = TRUE)",                                                 
  "```",                                                                                
  "",                                                                                   
  "# Section 1",                                                                        
  "",                                                                                   
  "Section 1. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.",
  "",                                                                                   
  "",                                                                                   
  "# Section 2",                                                                        
  "",                                                                                   
  "Section 2. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.",
  "")                      
grep("^Section 1\\.", RMD1, value = TRUE)
[1] "Section 1. For more details on using R Markdown see <http://rmarkdown.rstudio.com>."

To use that idea, I would modify the code you posted above.

library(shiny)
library(shinydashboard)
library(knitr)
ui <- 
  dashboardPage(
    dashboardHeader(title ='Virtual Excursion'), 
    
    dashboardSidebar( sliderTextInput(
      inputId = "mySliderText", 
      label = "Story line", 
      grid = TRUE, 
      force_edges = TRUE,
      choices = c('1','2')
    )
    ),
    dashboardBody(
      fluidRow(
        column(9,  
               box(
                 title = "Operations ", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("operations")
               )
        )
      ), 
      fluidRow(
        column(9, 
               box(
                 title = "Challenges", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("challenges")
          )
         ) 
        )
       )
      )
       

server <- function(input, output,session) {
 
  ChoseTheFile <- reactive({
    if (input$mySliderText == 1 )
    {
     readLines("Path/To/virtual_excursion1.rmd")
     else
     readLines("Path/To/virtual_excursion1.rmd")
      
    }
  }) 
  
  output$operations <- renderUI({
   # Use grep to extract section 1 from ChoseTheFile()
    
  })

  output$challenges<- renderUI({
     # Use grep to extract section 2 from ChoseTheFile()
    
  })
  
}

shinyApp(ui = ui, server = server)

I have not tested any of that, so there are bound to be problems with it.

1 Like

your suggested solution did work. But still, I did not fulfill my purpose of choosing rmd files which to have the same format for the text inside the box as the rmd file itself

I used this code:

library(shiny)
library(shinydashboard)
library(knitr)
ui <- 
  dashboardPage(
    dashboardHeader(title ='Virtual Excursion'), 
    
    dashboardSidebar( sliderTextInput(
      inputId = "mySliderText", 
      label = "Story line", 
      grid = TRUE, 
      force_edges = TRUE,
      choices = c('1','2')
    )
    ),
    dashboardBody(
      fluidRow(
        column(9,  
               box(
                 title = "Operations ", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("operations")
               )
        )
      ), 
      fluidRow(
        column(9, 
               box(
                 title = "Challenges", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("challenges")
          )
         ) 
        )
       )
      )

server <- function(input, output,session){
  
  ChoseTheFile <- reactive({
    if (input$mySliderText == 1 )
    {
      readLines("virtual_excursion1.rmd")
    }
    else
    {
      readLines("virtual_excursion2.rmd")
      
    }
})
  
  output$operations <- renderUI({
    grep("^Section 1\\.", ChoseTheFile(), value = TRUE)
    
  })

  output$challenges <- renderUI({
    grep("^Section 2\\.", ChoseTheFile(), value = TRUE)
    
  })
  
}

shinyApp(ui = ui, server = server)

I would like to combine the following two codes I think they would solve the problem

1st :

  output$operations <- renderUI({
    
    HTML(markdown::markdownToHTML(knit('RMarkdownFile.rmd', quiet = TRUE)))
  })

2nd:

  output$operations <- renderUI({
        grep("^Section 1\\.", ChoseTheFile(), value = TRUE)

  })

I definitely do not know how to do that. I'll play with it and post here if I make progress. What is the formatting you want to bring in from the rmd? In the example, that line looks like plain text with no extra formatting.

What I mean by formatting is to have what exactly inside the rmd for example :
a clickable web links, images, tables...etc

I tried this but with the fixed option, so I need it to be interactive according to the 'mySliderText'


library(shiny)
library(shinydashboard)
library(knitr)
ui <- 
  dashboardPage(
    dashboardHeader(title ='Virtual Excursion'), 
    
    dashboardSidebar( sliderTextInput(
      inputId = "mySliderText", 
      label = "Story line", 
      grid = TRUE, 
      force_edges = TRUE,
      choices = c('1','2')
    )
    ),
    dashboardBody(
      fluidRow(
        column(9,  
               box(
                 title = "Operations ", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("operations")
               )
        )
      ), 
      fluidRow(
        column(9, 
               box(
                 title = "Challenges", 
                 closable = FALSE, 
                 width = 9,
                 status = "primary", 
                 solidHeader = FALSE, 
                 collapsible = TRUE,
                 uiOutput("challenges")
          )
         ) 
        )
       )
      )

server <- function(input, output,session){
  
  ChoseTheFile <- reactive({
    if (input$mySliderText == 1 )
    {
      return ("virtual_excursion1.rmd")
    }
    else
    {
      return ("virtual_excursion2.rmd")
      
    }
})
  
  output$operations <- renderUI({
    
    HTML(markdown::markdownToHTML(knit(ChoseTheFile(), quiet = TRUE)))
     })
  # I need this to be just section 1 that includes both the image and the clickable link as same as the rmd file 
  output$challenges <- renderUI({
    grep("^Section 2\\.", ChoseTheFile(), value = TRUE)
    
  })
  
}

shinyApp(ui = ui, server = server)


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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.