R Shiny: htmlOutput removes white spaces I need - html

I have a string like this for example:
SomeName SomeValue
A much longer name AnotherValue
Even longer name than before NewValue
Let's say I have this text correctly in a R variable like
variable<-"SomeName SomeValue<br\>A much longer name AnotherValue<br\>Even longer name than before NewValue<br\>
In my ui.R:
...htmlOutput("TextTable")
And in my server.R
output$TextTable<- renderUI({
HTML(variable)
})
But the output isn't the way I want it. All white spaces are deleted except one. So the "columns" aren't as they should be in my output. How can I avoid this?

If I remember correctly, browsers tend to shorten consecutive whitespaces in HTML code to a single character. Don't put the whitespace in your r variable. Store the data as a matrix, and use renderTable.
##server.R
library(shiny)
shinyServer(function(input, output) {
variable = c("SomeName","SomeValue","A much longer name","AnotherValue","Even longer name than before", "NewValue")
variable = matrix(data = variable, ncol = 2)
output$TextTable<- renderTable({variable},include.rownames = FALSE,include.colnames = FALSE)
})
##ui.R
library(shiny)
shinyUI(fluidPage(titlePanel(""),
sidebarLayout(sidebarPanel(),
mainPanel(uiOutput("TextTable")
)
)
))
Update
On the other hand, if your text is already in that format, you can prevent a browser from collapsing whitespace using the <pre> tag. In your case, you could use the following code:
output$TextTable<- renderUI(HTML(paste0("<pre>",variable,"</pre>")))

Related

How to generate text output in an elegant way in markdown?

How can I generate text for rmarkdown -> HTML file in an elegant way? I need to avoid using the paste() function.
For instance text like this:
df <- data.frame(origin = c("A","B","C","D","E","F","G","H","I","J"),
Percentage = c(23,16,32,71,3,60,15,21,44,60),
rate = c(10,12,20,200,-25,12,13,90,-105,23),
change = c(10,12,-5,12,6,8,0.5,-2,5,-2))
paste("Min change is", min(df$change), "%)")

Shiny renderText: half italicized, half not?

In my shiny app, I have a textOutput named acronym where I would like to renderText some text which is half non-italicized, half-italicized.
I tried doing it like this:
output$acronym_1 <- renderText(paste("SID SIDE:", tags$em("Siderastrea siderea")))
But this did not get the second half in italics. How do I do this?
Thanks in advance.
The following code will produce italicized text
library(shiny)
ui = fluidPage(uiOutput("htmlText"))
server <- function(input, output)
output$htmlText <- renderUI(HTML(paste(
"Non-italic text.", em("Italic text")
)))
shinyApp(ui, server)
I don't think textOutput is capable of text markup since the output string will be created by cat according to the documentation.
renderText(expr, env = parent.frame(), quoted = FALSE,
outputArgs = list())
expr An expression that returns an R object that can be used as an argument to cat.

R Shiny - Using htmlOutput / uiOutput from UI.R in server.R as an input

In my shiny dashboard I am generating complex SQL that takes input from the user and generates the SQL code and executes it after the user has inputted their username / password.
I generate an htmlOutput in UI.R to show the code prior to code runs.
The code, when displayed as a textOutput contains line breaks br(), comments enclosed in em() and shiny-html-output div elements.
My objective is to be able to use the htmlOutput as an input in the sqlQuery function.
Parts of SERVER.R
qry_SQL <- reactive({tempfunc(1, "TD")})
# tempfunc creates my SQL using inputs present in UI.R
output$qry_out_text <- renderText(qry_SQL())
#Generates output in text format that contains <br/>, <em> and <div id="key1A_main_TD_1" class="shiny-html-output"> elements
output$qry_out_text2 <- renderUI(qry_SQL())
# Generates html output that I would ideally like to pass to sqlQuery
output$qry_out <- renderDataTable({
input$Execute
validate(
need(input$Username!=""&&input$Password!="","Please input Username and Password for TERADATA"),
need(input$Execute>0,"Click Execute")
)
ch <- odbcConnect(dsn="CustomDSN",uid=input$Username, pwd = input$Password)
import <<- sqlQuery(ch, PASS_HTML_OUTPUT_HERE, errors = FALSE )
odbcClose(ch)
head(import, n =100)
})
Ideally I need help on what to input in the PASS_HTML_OUTPUT_HERE block above.
qry_SQL() when outputted using textOutput yields the following:
SELECT c1.COL1, c1.COL2
FROM TABLE as c1
WHERE cast(c1.DateNow as date) between '2015-09-19' and '2015-10-16'
and <div id="key1A_main_TD_1" class="shiny-html-output"></div>
<div id="key2A_main_TD_1" class="shiny-html-output"></div>
<div id="key3A_main_TD_1" class="shiny-html-output"></div>
<div id="key4A_main_TD_1" class="shiny-html-output"></div>
<div id="key5A_main_TD_1" class="shiny-html-output"></div>
In this case key1A_main_TD_1 yields "( AND c1.Details like '%unhappy%') "
The other div tags do not yield anything as nothing has been inputted by the user.
qry_SQL() when outputted using htmlOutput yields the following:
SELECT c1.COL1, c2.COL2
FROM TABLE1 as c1
WHERE cast(c1.DateNow as date) between '2015-09-19' and '2015-10-16'
and
(c1.Details like '%unhappy%')
My problem is that I would like to pass qry_SQL() as HTML, with the div tags resolved.
Cheers,
Anand

self created tables with shiny

I am wondering how I can create a (html) table cell by cell with dynamic content in shiny?
right now I am using the following combination:
server.R
output$desc <- renderTable(
hdx.desc()
)
ui.R
tabsetPanel(
tabPanel("Description", tableOutput("desc"))
)
This works well. I would like to set links to some cells and also add some additional layout setting to the table like bold, without border etc. and also don't want to the row numbers at the front.
How can I do this? I tried the HTML() command, but it did't work.
Thanks for your help.
If you want to use renderTable the easiest way to style your table is using css. Removing the row numbers requires passing the option include.rownames = FALSE to print.xtable. There is a ... argument in the renderTable function that does this. You can include html in your table and use the sanitize.text.function argument.
runApp(list(
ui = bootstrapPage(
tableOutput("myTable")
, tags$head(tags$style(type="text/css",
"#myTable table th td {
border: 1px solid black !important;
}
#myTable table th
{
background-color:green;
color:white;
}"
))
),
server = function(input, output) {
output$myTable <- renderTable({
temp = c(runif(4),
as.character(tags$a(id = 'myId', href='http://www.example.com', runif(1)))
)
data.frame(date=seq.Date(Sys.Date(), by=1, length.out=5), temp = temp)
}, include.rownames = FALSE, sanitize.text.function = function(x) x)
}
))
Alternatively look at renderDataTable which allows you to use http://datatables.net/.
If you know that your table will be static and that only the content will be dynamic, you could follow my approach annotated here: Shiny - populate static HTML table with filtered data based on input
In short, I build a static html table and wrap it in a function in a seperated R file, source it in the server and call it in the renderUI() function with the newly filtered data. Thus, the table content gets updated with the user input.
A future project will be a function that allows the user to generate a static html table in a dynamic way, e.g., a function that creates a table with X rows, Y columns, rownames[], colnames[], etc. If I succeed, I will post my solution here.

Putting hyperlinks into an HTML table in R

I am a biologist trying to do computer science for research, so I may be a bit naïve. But I would like to a make a table containing information from a data frame, with a hyperlink in one of the columns. I imagine this needs to be an html document (?). I found this post this post describing how to put a hyperlink into a data frame and write it as an HTML file using googleVis. I would like to use this approach (it is the only one I know and seems to work well) except I would like to replace the actual URL with a description. The real motivation being that I would like to include many of these hyperlinks, and the links have long addresses which is difficult to read.
To be verbose, I essentially want to do what I did here where we read 'here' but 'here' points to
http:// stackoverflow.com/questions/8030208/exporting-table-in-r-to-html-with-hyperlinks
From your previous question, you can have another list which contains the titles of the URL's:
url=c('http://nytimes.com', 'http://cnn.com', 'http://www.weather.gov'))
urlTitles=c('NY Times', 'CNN', 'Weather'))
foo <- transform(foo, url = paste('<a href = ', shQuote(url), '>', urlTitles, '</a>'))
x = gvisTable(foo, options = list(allowHTML = TRUE))
plot(x)
Building on Jack's answer but consolidating from different threads:
library(googleVis)
library(R2HTML)
url <- c('http://nytimes.com', 'http://cnn.com', 'http://www.weather.gov')
urlTitles <- c('NY Times', 'CNN', 'Weather')
foo <- data.frame(a=c(1,2,3), b=c(4,5,6), url=url)
foo <- transform(foo, url = paste('<a href = ', shQuote(url), '>', urlTitles, '</a>'))
x <- gvisTable(foo, options = list(allowHTML = TRUE))
plot(x)