create a dynamical HTML table in render$image

21 views
Skip to first unread message

Jithendra A

unread,
May 24, 2017, 1:27:37 AM5/24/17
to Shiny - Web Framework for R
Hi,

I am trying to display images ( loading image by url ) by selection . since there are more than 1 image per selection I would like to get help to create a dynamical html table / any other method to display images side by side Max 4 images per row.
how can I do that ??
Highlighted in yellow is the logic part that I am looking for,
Since
StateFlagImageurl will have more than 1 url I am not getting the logic... I know i have to use for loop but I am getting how to generate the table dynamically...

CSV Data :
Country

State CountryFlagImageurl StateFlagImageurl
India AP http://mydomain.com/India.jpeg http://mydomain.com/AP.jpeg
India KK http://mydomain.com/India.jpeg http://mydomain.com/KK.jpeg
India UP http://mydomain.com/India.jpeg http://mydomain.com/UP.jpeg
India DL http://mydomain.com/India.jpeg http://mydomain.com/DL.jpeg
India OD http://mydomain.com/India.jpeg http://mydomain.com/OD.jpeg
India KL http://mydomain.com/India.jpeg http://mydomain.com/KL.jpeg
India TS http://mydomain.com/India.jpeg http://mydomain.com/TS.jpeg
India PY http://mydomain.com/India.jpeg http://mydomain.com/PY.jpeg
United States Alabama http://mydomain.com/United States.jpeg http://mydomain.com/Alabama.jpeg
United States Alaska http://mydomain.com/United States.jpeg http://mydomain.com/Alaska.jpeg
United States Arizona http://mydomain.com/United States.jpeg http://mydomain.com/Arizona.jpeg
United States Arkansas http://mydomain.com/United States.jpeg http://mydomain.com/Arkansas.jpeg
United States California http://mydomain.com/United States.jpeg http://mydomain.com/California.jpeg
United States Colorado http://mydomain.com/United States.jpeg http://mydomain.com/Colorado.jpeg
United States Connecticut http://mydomain.com/United States.jpeg http://mydomain.com/Connecticut.jpeg
United States Delaware http://mydomain.com/United States.jpeg http://mydomain.com/Delaware.jpeg
United States Florida http://mydomain.com/United States.jpeg http://mydomain.com/Florida.jpeg
United States Georgia http://mydomain.com/United States.jpeg http://mydomain.com/Georgia.jpeg
United States Hawaii http://mydomain.com/United States.jpeg http://mydomain.com/Hawaii.jpeg
United States Idaho http://mydomain.com/United States.jpeg http://mydomain.com/Idaho.jpeg
United States Illinois http://mydomain.com/United States.jpeg http://mydomain.com/Illinois.jpeg


Here is the code that I am using ...

library(shiny)

data = read.csv("countrydata.csv") 

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Test"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("Country", "Country",
                  choices = c("Enter a Country", data$Country))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      htmlOutput('image'),
htmlOutput('stateimage' ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { c_id <- reactive({ shiny::validate( shiny::need(input$Country, "Select a Country!") ) data[data$Country== input$Country, ] })

getimage <- function(country){

c.info <- subset(data, Country==
country,)
location <- toString(unique(c.info$CountryFlagImageurl))
return(location)
}

getstateimage <- function(state){

c.info <- subset(data, State== state
,)
location <- toString(c.info$StateFlagImageurl)
return(location)
}
output$image <-renderText({

paste0('<h2 align="center">Country Flag</h2><table width="100%"><tr align="center"><td width="33%">
<img src="',getimage(input$
CountryFlagImageurl),'" border="10" alt="Link to this page">
</td></tr></table>')

})

output$stateimage <-renderText({

paste0('<h2 align="center">State Flag</h2><table width="100%"><tr align="center"><td width="33%">
<img src="',getstateimage(input$
StateFlagImageurl),'" border="10" alt="Link to this page">
</td></tr></table>')

})


 }

Joe Cheng

unread,
May 24, 2017, 1:33:10 AM5/24/17
to Jithendra A, Shiny - Web Framework for R
Rather than renderText, try renderUI along with some of the techniques here (try lapply to make lists of these tag objects):

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/8d22448f-a2a5-45e2-a1d8-db9b6944e9f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jithendra A

unread,
May 24, 2017, 2:10:30 PM5/24/17
to Shiny - Web Framework for R, jithe...@gmail.com
I have tried the below codes but nothing is coming... just a blank page... cloud you suggest me or correct me

output$image <-renderUI({
   
   
    lst <- lapply(getimagelist(input$ID), function(x){
      url <- paste0(x)
     
      ## all your other code goes here.
      pg <- read_html(url)
     
      # target only the <td> elements under the bodytext div
      body <- html_nodes(pg, "div#bodytext")
    })
the other approach : output$image <-renderUI({

  for(i in getimagelist(input$ID))

    {

      url <- paste0( i)
      # print (url)
      #pg <- read_html(url)

      ## all your other code goes here.

       print('<h2 align="center"></h2><table width="100%"><tr align="center"><td width="33%">
            <a href="',url,'" target="_blank" ><img src="',url,'" border="10" alt="Image not found" ></a>
           </td></tr></table>')
  }
tried two ways but not working..
Reply all
Reply to author
Forward
0 new messages