I'm trying to create a Shiny App, where I want a part of the output to be data sheet based on input. In order to make it look nice, my idea is to generate it based on some HTML code. However I'm not very familiar with HTML, but I would need to parse values from input and values calculated in the R code ( “varA”, “varB” and “varC”) to the HTML code. Any suggestions on how to do that smart?
I've attached an example showing my idea
app.R
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("MyTable"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("filetable")
)
)
)
server <- function(input, output) {
Reactive_Var<-reactive({
#Variables I vould like to parse to HTML
varA <- input$bins
varB <- varA^2
varC <- varB^2
pbhtml <- paste(
readLines(paste(getwd(),'/mytable.html',sep=""))
)
HTML(pbhtml)
})
output$filetable <- renderUI({Reactive_Var()})
}
# Run the application
shinyApp(ui = ui, server = server)
mytable.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title></title>
<style type="text/css">
body,div,table,thead,tbody,tfoot,tr,th,td,p { font-family:"Calibri"; font-size:x-small }
a.comment-indicator:hover + comment { background:#ffd; position:absolute; display:block; border:1px solid black; padding:0.5em; }
a.comment-indicator { background:red; display:inline-block; border:1px solid black; width:0.5em; height:0.5em; }
comment { display:none; }
</style>
</head>
<body>
<table cellspacing="0" border="0">
<colgroup width="175"></colgroup>
<colgroup span="3" width="64"></colgroup>
<colgroup width="103"></colgroup>
<tr>
<td style="border-bottom: 1px solid #000000" height="20" align="left" valign=bottom bgcolor="#A9D18E"><b><font color="#000000">My Table</font></b></td>
<td style="border-bottom: 1px solid #000000" align="left" valign=bottom bgcolor="#A9D18E"><b><font color="#000000"><br></font></b></td>
<td style="border-bottom: 1px solid #000000" align="left" valign=bottom bgcolor="#A9D18E"><b><font color="#000000">Value</font></b></td>
<td style="border-bottom: 1px solid #000000" align="left" valign=bottom bgcolor="#A9D18E"><b><font color="#000000"><br></font></b></td>
<td style="border-bottom: 1px solid #000000" align="left" valign=bottom bgcolor="#A9D18E"><b><font color="#000000">Value Squared</font></b></td>
</tr>
<tr>
<td height="20" align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
</tr>
<tr>
<td height="20" align="left" valign=bottom bgcolor="#C5E0B4"><b><font color="#000000">Numbers of bins</font></b></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="right" valign=bottom bgcolor="#C5E0B4" sdval="2" sdnum="1033;"><font color="#000000">varA</font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="right" valign=bottom bgcolor="#C5E0B4" sdval="4" sdnum="1033;"><font color="#000000">varB</font></td>
</tr>
<tr>
<td height="20" align="left" valign=bottom bgcolor="#C5E0B4"><b><font color="#000000"><br></font></b></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
</tr>
<tr>
<td height="20" align="left" valign=bottom bgcolor="#C5E0B4"><b><font color="#000000">Numbers of bins squared</font></b></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="right" valign=bottom bgcolor="#C5E0B4" sdval="4" sdnum="1033;"><font color="#000000">varB</font></td>
<td align="left" valign=bottom bgcolor="#C5E0B4"><font color="#000000"><br></font></td>
<td align="right" valign=bottom bgcolor="#C5E0B4" sdval="16" sdnum="1033;"><font color="#000000">varC</font></td>
</tr>
</table>
<!-- ************************************************************************** -->
</body>
</html>
I would do the following.
body,div,table,thead,tbody,tfoot,tr,th,td,p { font-family:"Calibri"; font-size:x-small }
a.comment-indicator:hover + comment { background:#ffd; position:absolute; display:block; border:1px solid black; padding:0.5em; }
a.comment-indicator { background:red; display:inline-block; border:1px solid black; width:0.5em; height:0.5em; }
comment { display:none; }
and put it in a separate file. Then load it via one of the methods explained here
This function takes as arguments the values that you want to replace in your template HTML, inserts them in the correct places, and returns the filled template string. The following is a minimal example (yours is going to be bigger)
my_template <- function(name, lastname, age) {
glue::glue(
"<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>{name}</td>
<td>{lastname}</td>
<td>{age}</td>
</tr>
</table>"
)
}
my_template("Michael", "Jordan", "50")
Make sure you only take the code between and tags and not everything included in your HTML file.
renderUI()
(make sure it is reactive) and then keep the htmlOutput()
as is.Please let me know if this doesn't work or if you need extra clarification:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.