Shiny R Add tooltips while hovering over the map - html

Image of the mapSo I created a map with shiny r. However, I am trying to add tooltips that will display the information when a person hovers over the country. For instance: when it goes on to russia it will display the data on russia as a pop up or tooltip.
I couldn't succeed in doing it. I found couple of codes that adds tooltips to the sidebars however I want to display it on the map while hovering over the countries.
> library(shiny)
> library(shinyBS)
> ui<-fluidPage(
+ mainPanel(
+ tabsetPanel(id="tp",
+ tabPanel("Map View", plotOutput("mPlot", height="560px", width="950px"))
+ )
+ )))
> sPDF <- joinCountryData2Map(data, joinCode='NAME', nameJoinColumn='Country.code')
> server<- function(input, output, session){
+ output$mPlot <- renderPlot({
+ mapParams <- mapPolys(sPDF,
nameColumnToPlot="Truth.Commission.Total", mapRegion="world",
+ missingCountryCol="dark grey", catMethod =
"fixedWith", numCats=20,
+ colourPalette=brewer.pal(20,"YlOrRd"),
+ addLegend=TRUE,
+ oceanCol="light blue")
+ mtext("[Grey Color: No Data Available]",side=1,line=-1)
+ })
+ }
> shinyApp(ui=ui, server=server)
The prior general solutions was made with
>addTooltip(session, id = "someInput", title = "This is an input.",
placement = "left", trigger = "hover")
However, as the countries on map does not have explicit id's displayed on the page, but has the data when plotting the map this solution did not work for me.

Related

How can I render HTML content in an R Shiny Popify (ShinyBS) tooltip?

I'm building out a datatable in R Shiny and part of it will include tooltips unique to each cell. I've accomplished that, however, I seem to be unable to insert HTML content into the tooltip itself. In the example below, I'm inserting HTML content into a cell in the datatable, and then aim to insert that same content into a tooltip, but the HTML only renders in the datatable, and not in the tooltip.
I've played around with a few ideas but can't find any that work. I can get the HTML to appear (as text) in the tooltip by removing the HTML function, but then, obviously, it's escaped and is just text. I am able to bold text within the tooltip using tags$b(), however, I am hoping for a solution more similar to my example below as I have more complex HTML content I would like add to the tooltip beyond just text.
Any ideas? Thanks very much!
library(shiny)
library(shinyBS)
library(DT)
ui <- fluidPage(
bsTooltip('tbutton',''),
mainPanel(dataTableOutput('df'))
)
server <- function(input, output) {
df <- data.frame(A = c(1:5), B = c(LETTERS[1:5]))
output$df <- renderDataTable({
cell <- paste0('<svg width="30" height="30">',
'<text x="1%" y="75%" font-weight="bold" font-size="16" >B</text>',
'</svg>')
df[2,2] <- as.character(popify(tags$div(HTML(cell)),
title = 'Tooltip:',
placement = 'left',
content = paste0(tags$div(HTML(cell))),
trigger = c('hover', 'click')))
datatable(df, escape=FALSE)
})
}
shinyApp(ui = ui, server = server)
To attach a popover to a cell, you can use bsPopover if this cell has an id. To set an id to the cells, you can use the datatables option createdCell.
Then the HTML code works in the popover content, but not the SVG (or at least I didn't manage to make it work).
library(shiny)
library(shinyBS)
library(DT)
df <- data.frame(A = 1:5, B = LETTERS[1:5])
css <- "
.red {color: red;}
"
ui <- fluidPage(
tags$head(tags$style(HTML(css))),
mainPanel(
DTOutput('df'),
bsPopover(
id = "id2",
title = "test",
content = '<p class="red">TEST</p>'
)
)
)
server <- function(input, output) {
output$df <- renderDT({
datatable(
df,
options = list(
columnDefs = list(
list(
targets = 2,
createdCell = JS(
"function (td, cellData, rowData, row, col) {",
" $(td).attr('id', 'id' + (row+1));",
"}"
)
)
)
)
)
})
}
shinyApp(ui = ui, server = server)

how to add horizontal lines through barplot using ggplot2?

I would like the lines to look exactly like this
I've been able to replicate everything else thus far. just not the actual lines through the bars.
library(ggplot2)
plot <-ggplot(data=df, aes(x=chromosomes, y=size)) +
geom_bar(stat="identity", width=0.1) +
scale_x_discrete(position = "top") + theme(axis.ticks.x = element_blank())+
expand_limits(y=c(0,180)) + scale_y_reverse() '''

Using R code to scrape data from a webpage into an Excel file

I have written a code in R which is supposed to retrieve certain information from a website and import it into an Excel file. I have used it for one website and it works, but for this particular website, it has an issue, it returns N/A values in excel, and I don't know why.
library(tidyverse)
library(rvest)
library(string)
library(rebus)
library(lubridate)
library(xlsx)
library(reader)
setwd("C:/Users/user/Desktop/Tenders")
getwd()
ran=seq(300100,300000,-1)
result = data.frame(matrix(nrow = length(ran), ncol = 1))
colnames(result) <- c("111")
for (i in ran){
url <- paste0("http://tenders.procurement.gov.ge/public/?go=", i)
download.file(url, destfile = "scrapedpage.html", quiet=TRUE)
content <- read_html("scrapedpage.html")
#111
status=content %>% html_nodes("#print_area tr:nth-child(1) td + td")%>% html_text()
status[length(status) == 0] <- NA
status=as.data.frame(status)
status=(if (nrow(status)>1){
a=as.matrix(paste(unlist(status), collapse =" "))
} else {as.matrix(status)
})
result[i, 1]=status
}
s=as.data.frame(ran)
final=result[-c(1:s[nrow(s),]),]
#Excel
write.xlsx(final,"C:/Users/user/Desktop/Tenders.xlsx", sheetName = "111")
I am using selector gadget tool, which is a chrome extension for identifying HTML parts that the code is supposed to use to gather the information (for example, in the code above it is "#print_area tr:nth-child(1) td + td", which is the first entry in the link).
Can someone help me find out what the issue might be?

Extend width of column with renderDataTable in Shiny

I having trouble understanding the behavior of renderDataTable function using Shiny.
I am trying to extend the width of one specific column.
When I am not using Shiny, and just trying to visualize the output of the table, I write the below and I get the expected output in the plot (Amazon Title column is extended):
Category <- c("Tools & Home Improvement", "Tools & Home Improvement")
AmazonTitle <- c("0.15,Klein Tools NCVT-2 Non Contact Voltage Tester- Dual Range Pen Voltage Detector for Standard and Low Voltage with 3 m Drop Protection", " ABCDFGEGEEFE")
ASIN_url <- c("<a href='https://www.amazon.com/dp/B004FXJOQO'>https://www.amazon.com/dp/B004FXJOQO</a>", "<a href='https://www.amazon.com/dp/B004FXJOQO'>https://www.amazon.com/dp/B0043XJOQO</a>")
ASIN <- c("B004FXJOQO", "B0043XJOQO")
All_ASIN_Information <- data.frame(Category, AmazonTitle, ASIN_url, ASIN)
DT::datatable(All_ASIN_Information, escape=FALSE,
options = list(
pageLength = 20, autoWidth = TRUE,
columnDefs = list(list( targets = 2, width = '600px'))
)
)
But when I use this exact block inside a DT::renderDataTable function for Shiny, the result is different and the column width is not extended....
See behavior for Shiny with below code:
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
DT::dataTableOutput("Table_ASIN")))
server <- function(input, output){
output$Table_ASIN <- DT::renderDataTable(
DT::datatable(All_ASIN_Information, escape=FALSE,
options = list(
pageLength = 20, autoWidth = TRUE,
columnDefs = list(list( targets = 2, width = '600px'))
)))
}
shinyApp(ui, server)
I don't know if this behavior is caused by the hyperlinks created in column 'ASIN_url' but I would really need them anyway.
Any help much appreciated on this !
One option would be to shorten the link like this:
ASIN_url <- c("<a href='https://www.amazon.com/dp/B004FXJOQO'>Link</a>", "<a href='https://www.amazon.com/dp/B004FXJOQO'>Link</a>")
Another would be to add a scroll bar by including scrollX = TRUE in the option list

How to I render an image from the web in a Shiny table?

I am working on a Shiny app that gets information from an API and displays that information in a table. Here is the code I have right now:
output$ItemInfo <- renderTable({
base <- "http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item="
item.id <- item.codes %>% filter(name == input$item)
url <- paste0(base, item.id$id)
item.data <- fromJSON(url)
table.data <- runescape.data %>% filter(ItemName == input$item)
image.url <- item.data[[1]]$icon_large[1]
Info <- c('Description', 'Image', 'Current Price (GP)', '% Change in Last 30 Days', '% Change in Last 90 Days', '% Change in Last 180 Days', 'Members Only', 'Low Alch', 'High Alch')
Data <- c(item.data[[1]]$description[[1]], paste0('<img src="', image.url, '"></img>'), item.data[[1]]$current$price, item.data[[1]]$day30$change, item.data[[1]]$day90$change, item.data[[1]]$day180$change, table.data$MembersOnly[[1]], table.data$LowAlch[[1]],
table.data$HighAlch[[1]])
#Display Both columns
return(data.frame(Info, Data))
})
I am getting a correct image.url, I just cannot figure out how to render it. Currently this just displays code like <img src="http://services.runescape.com/m=itemdb_rs/1480946739712_obj_big.gif?id=560"></img> where the actual image should be.
I also tried using this code as a replacement for the paste0 bit in on the Data line: img(src='http://services.runescape.com/m=itemdb_rs/1480946739712_obj_big.gif?id=560', width = '300px', height = '100px'), but this broke the entire table for some reason.
How can I go about rendering the images I get from this URL?