I want to export html objects from shiny app to pdf. In order to export table, I was using .Rmd template (based on How to make pdf download in shiny app response to user inputs?), however I do not know how to pass html object to PDF in shiny app.
Example app:
library(shiny)
ui <- shinyUI(
fluidPage(
fluidRow(
column(width=4,
htmlOutput("Table1"),
htmlOutput("Table2"),
htmlOutput("Table3")
))
)
)
server <- shinyServer(function(input, output, session){
#****************************************
#* Output Components
output$Table1 <- renderUI({
HTML("<div class='progress-group'>
<span class='progress-text'>Add Products to Cart</span>
<span class='progress-number'><b>160</b>/200</span>
<div class='progress sm'>
<div class='progress-bar progress-bar-aqua' style='width: 80%'></div>
</div>
</div>")
})
output$Table2 <- renderUI({
HTML("<div class='progress-group'>
<span class='progress-text'>Complete Purchase</span>
<span class='progress-number'><b>310</b>/400</span>
<div class='progress sm'>
<div class='progress-bar progress-bar-red' style='width: 100%'></div>
</div>
</div>")
})
output$Table3 <- renderUI({
HTML("<div class='progress-group'>
<span class='progress-text'>Visit Premium Page</span>
<span class='progress-number'><b>480</b>/800</span>
<div class='progress sm'>
<div class='progress-bar progress-bar-green' style='width: 10%'></div>
</div>
</div>")
})
})
shinyApp(ui, server)
Related
I'm scrapping through this html and I want to extract the text inside the <span data-testid="distance">
<span class="class1">
<span data-testid="distance">the text i want</span>
</span>
<span class="class2">
<span class="class1"><span>the other text i'm obtaining</span>
</span>
distancia <- hoteles_verdes %>%
html_elements("span.class1") %>%
html_text()
The question would be how to isolate the data-testid="distance" on the html elements to later retrieve the html_text.
It's my first question posting. thanks!
You can use a CSS attribute selector.
For example, the [attribute|="value"] selector to select attribute "data-testid" with value = "distance" (note the single and double quotes):
library(rvest)
hoteles_verdes %>%
html_nodes('[data-testid|="distance"]') %>%
html_text()
Result:
[1] "the text i want"
Data:
hotel_verdes <- read_html('<span class="class1">
<span data-testid="distance">the text i want</span>
</span>
<span class="class2">
<span class="class1"><span>the other text im obtaining</span>
</span>')
I would like to write this code (that works):
tags$li(
class = "dropdown",
tags$a(
height = 40, href = "https://www.google.com.br",
title = "Whatsapp", target = "_blank",
tags$strong(
icon("whatsapp"), ""
)
)
)
in the HTML() form. I tried:
HTML(
"<li class='dropdown'>
<a href='https://www.google.com.br' target='_blank'>Whatsapp</a>
<strong class='fas fa-whatsapp'></strong>
</li>"
)
and this code:
HTML(
'<li class="dropdown">
<a style="height:40px" href="https://www.google.com.br"
title="Whatsapp" target="_blank">
<strong>
<i class="fas fa-whatsapp"></i>
</strong>
</a>
</li>'
)
But an error appear:
An error has occurred!
Expected an object with class 'shiny.tag'.
My app:
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
HTML(
"<li class='dropdown'>
<a https://www.google.com.br' target='_blank'>Whatsapp</a>
<strong class='fas fa-whatsapp'></strong>
</li>"
)
)
sidebar <- dashboardSidebar(
width = 300
)
body <- dashboardBody(
uiOutput(
outputId = "hour"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(input, output) {
}
shinyApp(ui, server)
I would just like to do this transformation to better edit my HTML document. I need to do this with just HTML().
How to solve this? I made two attempts, as I showed, but I couldn't solve it.
Placing the HTML() content inside tags$li() with 'dropdown' class works
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
tags$li(
class = "dropdown",
HTML("
<a https://www.google.com.br' target='_blank'>Whatsapp</a>
<strong class='fas fa-whatsapp'></strong>
")
)
)
sidebar <- dashboardSidebar(
width = 300
)
body <- dashboardBody(
uiOutput(
outputId = "hour"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(input, output) {
}
shinyApp(ui, server)
Welcome, I have been searching for quite a long time but could not find how to manage with this example using html_nodes() from rvest. I would like to extract the data-value from span, but only the first number. For the following html piece, it should return only : "504 012"
<p class="sort-num_votes-visible">
<span class="text-muted">Votes:</span>
<span name="nv" data-value="504012">504 012</span>
<span class="ghost">|</span>
<span class="text-muted">Gross:</span>
<span name="nv" data-value="1 024 560">$1.02M</span>
</p>
I would be glad for any kind of help.
You can specify the name attribute ("nv") and use html_node() to get only the first occurrence.
library(rvest)
p <- '<p class="sort-num_votes-visible">
<span class="text-muted">Votes:</span>
<span name="nv" data-value="504012">504 012</span>
<span class="ghost">|</span>
<span class="text-muted">Gross:</span>
<span name="nv" data-value="1 024 560">$1.02M</span>
</p>'
p %>%
read_html() %>%
html_node("span[name='nv']") %>%
html_text()
[1] "504 012"
I have a Shiny App which I want to implement in a existing website, therefore I need to translate the ui.R into a HTML UI.I got most of the things from ui.R work in HTML UI, but I have questions concerning uiOutput, html Output and the File Upload.
Q1:How do implement an dynamic Slider, that I have created with renderUI({})? The dynamic selection I have created with renderUI({}) works fine, but with the slider I get the following error:min, max, amd value must all be numeric values It seems that no numeric values can be send to server.R from the HMTL UI.
Q2:: How would a working HMTL File Upload would look like? Mine seems to upload files, but I cannot pass it to server.R.
Here is an example:
server.R:
library(shiny)
#sample data
years<-c(1990,1995,2000,2005,2010)
oryear<-years[3]
shinyServer(function(input, output, session) {
#Input uploaded file
inFile<-input$ascii_layer
#make dynamic selection
output$selectUI <- renderUI({
selectInput("test_select", "Test selection", years, selected=oryear)
})
#make dynamic slider
output$slider <- renderUI({
sliderInput("inSlider", "Slider", min=input$min_val, max=input$max_val, value=2000)
})
})
ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Test Shiny App"),
sidebarPanel(
#File Upload
fileInput('ascii_layer', 'Choose ASCII Layer', multiple=FALSE, accept='asc'),
#HTML Selection Output from server.R
htmlOutput("selectUI"),
#Numeric Inputs
numericInput("min_val", "Enter Minimum Value", 1993),
numericInput("max_val", "Enter Maximum Value", 2013)
#display dynamic UI
uiOutput("slider")
),
mainPanel()
))
HMTL UI:
<html>
<head>
<script src="shared/jquery.js" type="text/javascript"></script>
<script src="shared/shiny.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="shared/shiny.css"/>
</head>
<body>
<h1>HTML UI</h1>
<!—- File Upload—->
<p>
<form action="input_file.htm" method="post" enctype="multipart/form-data">
<p>Choose Distance to Road Layer:<br>
<input name="ascii_layer" type="file" size="50" maxlength="100000" accept="*.asc">
</p>
</form>
</p>
<!—-Numeric Inputs—->
<p>
<label>Enter Minimum Value:</label><br />
<input type="number" name="min_val" value="1993" />
</p>
<p>
<label>Enter Maximum Value:</label><br />
<input type="number" name="max_val" value="1993" />
</p>
<!—-Dynamic Selection—->
<div id="selectUI" class="shiny-html-output"></div>
<!—-Dynamic Slider—->
<div id="slider" class="shiny-html-output"></div>
</body>
</html>
There were several errors in your Shiny code. A fixed version is given below. Run this by copy-and-pasting into the R terminal and view the source code from your browser to get your html-ui. I suggest you take a look at the Shiny tutorials online (e.g. http://rstudio.github.io/shiny/tutorial) for further detail on file upload etc.
library(shiny)
#sample data
years <- c(1990,1995,2000,2005,2010)
oryear <- years[3]
server <- function(input, output, session) {
#Input uploaded file
# inFile<-input$ascii_layer
output$contents <- renderTable({
inFile <- input$ascii_layer
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath)
})
#make dynamic selection
output$selectUI <- renderUI({
selectInput("test_select", "Test selection", years, selected=oryear)
})
#make dynamic slider
output$slider <- renderUI({
sliderInput("inSlider", "Slider", min=input$min_val, max=input$max_val, value=2000)
})
}
ui <- pageWithSidebar(
headerPanel("Test Shiny App"),
sidebarPanel(
#File Upload
fileInput('ascii_layer', 'Choose ASCII Layer', multiple=FALSE, accept='asc'),
#HTML Selection Output from server.R
uiOutput("selectUI"),
#Numeric Inputs
numericInput("min_val", "Enter Minimum Value", 1993),
numericInput("max_val", "Enter Maximum Value", 2013),
#display dynamic UI
uiOutput("slider")
),
mainPanel(
tableOutput('contents')
)
)
runApp(list(ui = ui, server = server))
I would like to create a Shiny R application that can take unformatted Stata code input by the user, add html tags, and return the entire block of code for easy copy and paste into an html publishing venue such as blogs or webpages.
I already have the R code that can handle the formatting A Stata HTML syntax highlighter in R. And most of the Shiny implementation seems very easy. The major challenge I am having is creating an html textbox or other object that can easily take a reactive element from the Shiny's server.R and return it to the user without formatting the html tags.
Example:
Stata code input through a text box
clear
set obs 4000
gen id = _n
gen eta1 = rnormal()
gen eta2 = rnormal()
XX Shiny submit button XX
Return in another text box
<span style="color: #9900FF">set</span> <span style="color: #0000CC"><b>obs</b></span> 4000
<span style="color: #0000CC"><b>gen</b></span> id = <span style="color: #9900FF">_n</span>
<span style="color: #0000CC"><b>gen</b></span> eta1 = <span style="color: #9900FF">rnormal</span>()
<span style="color: #0000CC"><b>gen</b></span> eta2 = <span style="color: #9900FF">rnormal</span>()
Overall, I think this is generally a long question for a potentially very simple answer. Thanks for your consideration.
renderText() does not parse HTML tags. E.g. if you do:
output$code <- renderText({
paste0(
'<span style="color: #9900FF">set</span> <span style="color: #0000CC"><b>obs</b></span> 4000',
'<span style="color: #0000CC"><b>gen</b></span> id = <span style="color: #9900FF">_n</span>',
'<span style="color: #0000CC"><b>gen</b></span> eta1 = <span style="color: #9900FF">rnormal</span>',
'<span style="color: #0000CC"><b>gen</b></span> eta2 = <span style="color: #9900FF">rnormal</span>'
)
})
Where this is your ui.R:
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Code"),
sidebarPanel(
),
mainPanel(
verbatimTextOutput("code")
)
))
The content comes out as just text.
But since you haven't posted your ui.R (or index.html) I'm not sure how you are rendering your output. If you are having issues displaying raw text instead of parsed HTML you can always replace < with < and > with > like this:
html <- '<span>text</span>'
x <- gsub('<', '<', html)
gsub('>', '>', x)
Which will produce: <span>text</span> and should not be displayed as parsed HTML in your browser.