简体   繁体   English

解析变量到R Shiny中的HTML文件

[英]Parse variables to HTML file in R Shiny

I'm trying to create a Shiny App, where I want a part of the output to be data sheet based on input.我正在尝试创建一个 Shiny 应用程序,我希望 output 的一部分成为基于输入的数据表。 In order to make it look nice, my idea is to generate it based on some HTML code.为了让它看起来好看,我的想法是根据一些 HTML 代码生成它。 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.然而,我对 HTML 不是很熟悉,但我需要将输入的值和在 R 代码(“varA”、“varB”和“varC”)中计算的值解析为 HTML 代码。 Any suggestions on how to do that smart?关于如何做到这一点有什么建议吗?

I've attached an example showing my idea我附上了一个例子来展示我的想法

app.R申请.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我的表.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.我会做以下事情。

  1. Take the following css code取以下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;  }

and put it in a separate file.并将其放在一个单独的文件中。 Then load it via one of the methods explained here然后通过此处解释的方法之一加载它

  1. Create a template function in R.在R中创建模板function。

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.此 function 将您要在模板 HTML 中替换的值作为 arguments,将它们插入正确的位置,并返回填充的模板字符串。 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.确保只使用 和 标签之间的代码,而不是 HTML 文件中包含的所有内容。

  1. Pass the result of the call to that function to renderUI() (make sure it is reactive) and then keep the htmlOutput() as is.将调用 function 的结果传递给renderUI() (确保它是反应性的),然后保持htmlOutput()

Please let me know if this doesn't work or if you need extra clarification:)如果这不起作用或者您需要额外说明,请告诉我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM