How to include code-block in Shiny App helpText() - html

I have the following ui.R
library(datasets)
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Telephones by region"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)),
hr(),
helpText("Some text and then some code.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("phonePlot")
)
)
)
Where the helpText() produces:
<span class="help-block">
Some text and then some code.
</span>
How can I modify helpText so that in can include code block:
<span class="help-block">
Some text and then <code>some code</code>.
</span>

Use
helpText("Some text and then ", code("some code"), ".")

Related

Rvest html_nodes span div other items

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>')

shiny htmlOutput() – export to pdf

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)

Html radio buttons in a shiny APP

Since yesterday I run a shiny app with radiobuttons in a HTML table. I used a code like this:
ui.r
shinyUI(pageWithSidebar(
headerPanel('Download Example'),
sidebarPanel(),
mainPanel(
fluidRow(
HTML('<div class="attr-col">
<ul>
<li>
<input type="radio" name="var" id="var1" value="A" checked="checked"/>
Option 1
</li>
<li>
<input type="radio" name="var" id="var2" value="B" />
Option 2
</li>
</ul>
</div>'
)
),
fluidRow(
verbatimTextOutput("sel")
)
)
))
server.r
shinyServer(function(input, output) {
output$sel<-renderText ({ input$var })
})
The code above works good in R 3.0.2 and shiny 0.11.1 ! But with R 3.2.0 and shiny 0.12.0 it doesn't.
I need to keep the HTML because I use a CSS to format a big table with other objects. I don't understand why in the new version the input$var can't reach the server. It is changed something in the code?
Sorry about that. Can you try changing the outer div to <div class="attr-col shiny-input-radiogroup" id="var">?

R Shiny: Translate into HTML UI uiOutput, htmlOutput and File Upload

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))

Shiny html output object that takes html code for easy copy and paste

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.