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.
Related
server<-function(input,output,session) {
observeEvent(input$do, {
con <- dbConnect(odbc::odbc(), "$$$$", uid = "$$$$", pwd = "$$$$")
result<-dbSendQuery(con,"select * from table_name")
data.frame<-dbFetch(result)
View(data.frame)
I have want the view the table in the R browser (not the console), so what function exactly need to use?
One way would be to use tableHTML which would convert your data.frame into an HTML table to make it possible to view on RStudio's browser (or your browser):
As an example:
library(tableHTML)
tableHTML(mtcars)
I am trying to add padding to a table I am creating in an RMarkdown file that will generate both a pdf and an html flexdashboard. I know that there are a number of functions/packages I could use (pander, xtable, DT, etc.), but I would prefer to use the kable function from the knitr package.
The trouble I am having is that the padding argument does not seem to work. I would appreciate any help in solving this without having to add custom CSS to my document.
As a example, I have tried to run the code with padding set to 0, 10, 20 but the tables all look identical in the html file.
knitr::kable(head(cars), format = "html", padding = 0)
knitr::kable(head(cars), format = "html", padding = 10)
knitr::kable(head(cars), format = "html", padding = 20)
I am using knitr_1.14 and rmarkdown_1.0, and my session information is as follows.
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
The option table.attr='cellpadding="20px"' does not work for me. Using CSS styles and adding a class to the table with table.attr='class="myTable"' leads to all tables having the desired padding property (even if only one table carries the new class).
If I only want to modify one single table I usually go with jQuery:
---
title: "Table Cell Padding"
output: html_document
---
```{r}
knitr::kable(head(cars), format = "html")
```
```{r}
knitr::kable(head(cars), format = "html", table.attr='class="myTable"')
```
<style>
.myTable td {
padding: 40px;
}
</style>
Another option is to use jQuery to edit individual elements. The following example modifies the table in the same way as the CSS styles above.
<script type="text/javascript">
// When the document is fully loaded...
$(document).ready(function() {
// ... select the cells of the table that has the class 'myTable'
// and add the attribute 'padding' with value '20px' to each cell
$('table.myTable td').css('padding','20px');
});
</script>
Here I add the class myTable to the table I want to modify. Afterwards I execute some JavaScript (see comments).
You could add any other CSS property to the table elements (or the table itself $('table.myTable').css(...)) in the same way (e.g. $('table.myTable td').css('background-color','red');)
I'm trying to style a table in a Shiny app using the tableHTML package in R.
When I use the tableHTML() function in R it produces exactly what I want. I use the add_css_column to align the text in the column to the centre. However when I use it in a Shiny app the headers end up left aligned and the rows centre aligned. Any ideas how I can fix this?
output$viewers_website_top <- renderUI({
tableHTML(website_index, rownames = FALSE, widths=c(200,200)) %>%
add_css_column(css = list("text-align", "center"),
column_names = names(website_index))
})
This is a common issue with bootstrap 3 unfortunately. Whenever you use shiny it loads up a bootstrap 3 css (immediately) which makes it difficult to overwrite.
As for the solution to this using add_css_header would probably solve this one. add_css_header would change the th tag of the HTML table to the one you like (whereas add_css_header would change the td tags below the headers):
output$viewers_website_top <- renderUI({
tableHTML(website_index, rownames = FALSE, widths=c(200,200)) %>%
add_css_header(css = list("text-align", "center"),
headers = 1:ncol(website_index))
})
Another thing you can do is to add a separate css file with shiny::includeCSS. There is more info here and here on how to use includeCSS.
In the css file you need to write:
.table_website_index th {
text-align: center;
}
And that should do it!
P.S. table_website_index is the class the package assigns to the table which you can also change with the class argument.
P.S.2 I am the developer - thanks for using the package :)
I have a shiny application that allows my user to explore a dataset. The idea is that the user explores the dataset, and any interesting things the user finds he will share with his client via email. I don't know in advance how many things the user will find interesting. So, next to each table or chart I have an "add this item to the report" button, which isolates the current view and adds it to a reactiveValues list.
Now, what I want to do is the following:
Loop through all the items in the reactiveValues list,
Generate some explanatory text describing the item (This text should preferably be formatted HTML/markdown, rather than code comments)
Display the item
Capture the output of this loop as HTML
Display this HTML in Shiny as a preview
write this HTML to a file
knitr seems to do exactly the reverse of what I want - where knitr allows me to add interactive shiny components in an otherwise static document, I want to generate HTML in shiny (maybe using knitr, I don't know) based on static values the user has created.
I've constructed a minimum not-working example below to try to indicate what I would like to do. It doesn't work, it's just for demonstration purposes.
ui = shinyUI(fluidPage(
title = "Report generator",
sidebarLayout(
sidebarPanel(textInput("numberinput","Add a number", value = 5),
actionButton("addthischart", "Add the current chart to the report")),
mainPanel(plotOutput("numberplot"),
htmlOutput("report"))
)
))
server = shinyServer(function(input, output, session){
#ensure I can plot
library(ggplot2)
#make a holder for my stored data
values = reactiveValues()
values$Report = list()
#generate the plot
myplot = reactive({
df = data.frame(x = 1:input$numberinput, y = (1:input$numberinput)^2)
p = ggplot(df, aes(x = x, y = y)) + geom_line()
return(p)
})
#display the plot
output$numberplot = renderPlot(myplot())
# when the user clicks a button, add the current plot to the report
observeEvent(input$addthischart,{
chart = isolate(myplot)
isolate(values$Report <- c(values$Report,list(chart)))
})
#make the report
myreport = eventReactive(input$addthischart,{
reporthtml = character()
if(length(values$Report)>0){
for(i in 1:length(values$Report)){
explanatorytext = tags$h3(paste(" Now please direct your attention to plot number",i,"\n"))
chart = values$Report[[i]]()
theplot = HTML(chart) # this does not work - this is the crux of my question - what should i do here?
reporthtml = c(reporthtml, explanatorytext, theplot)
# ideally, at this point, the output would be an HTML file that includes some header text, as well as a plot
# I made this example to show what I hoped would work. Clearly, it does not work. I'm asking for advice on an alternative approach.
}
}
return(reporthtml)
})
# display the report
output$report = renderUI({
myreport()
})
})
runApp(list(ui = ui, server = server))
You could capture the HTML of your page using html2canvas and then save the captured portion of the DOM as a image using this answer, this way your client can embed this in any HTML document without worrying about the origin of the page contents
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>")))