Reputation: 153
I have read the various ways to print separate lines in Shiny using HTML but was not satisfied. My goal is to bin various independent variables and provide the code as an SQL statement that the user can then use to bin in SAS. The text that I would eventually like to see printed in my shiny app is the result from executing the code below:
binbreaks <- c(17,41,65,89,113)
x_var <- "IAG01"
for (i in 1:length(binbreaks)) {
if (i==1) {
cat(paste("select ",x_var, ", case", "\nwhen ", x_var," <= ", binbreaks[1], "then 1"))
}
if (i>1 & i<=(length(binbreaks)-1)) {
cat(paste("\nwhen ",binbreaks[i-1], " < ", x_var, " <= ", binbreaks[i], "then ", i))
}
if (i==length(binbreaks)) {
cat(paste("\nelse ", i))
cat(paste("\nend as ", x_var, "_bin"))
}
The resulting text in my shiny app should be:
select IAG01 , case
when IAG01 <= 17 then 1
when 17 < IAG01 <= 41 then 2
when 41 < IAG01 <= 65 then 3
when 65 < IAG01 <= 89 then 4
else 5
end as IAG01 _bin
server.R:
shinyServer(function(input, output) {
output$SQL_pp <- renderPrint({
binbreaks<-seq(min(input_data_NA[[input$x_var]]),max(input_data_NA[[input$x_var]]),length.out=input$bins)
cat(
for (i in 1:length(binbreaks)) {
if (i==1) {
cat(paste("select ",input$x_var, ", case", "\nwhen ", input$x_var," <= ", binbreaks[1], "then 1"))
}
if (i>1 & i<=(length(binbreaks)-1)) {
cat(paste("\nwhen ",binbreaks[i-1], " < ", input$x_var, " <= ", binbreaks[i], "then ", i))
}
if (i==length(binbreaks)) {
cat(paste("\nelse ", i))
cat(paste("\nend as ", input$x_var, "_bin"))
}
}
)
})
ui.R:
shinyUI(fluidPage(
navbarPage("EDA Tool",
tabPanel("SQL code", textOutput("SQL_pp"))
)
)
The issue is that I would like to print like the output above. Instead, my output looks all jumbled. It will still execute but I would like it to look presentable (i.e. not like this:
select IAG01 , case when IAG01 <= 17 then 1 when 17 < IAG01 <= 41 then 2 when 41 < IAG01 <= 65 then 3 when 65 < IAG01 <= 89 then 4 else 5 end as IAG01 _bin
Upvotes: 2
Views: 6091
Reputation: 8812
HTML ignores linebreaks by default; you can use a pre
element to preserve linebreaks and spacing. Replace this:
textOutput("SQL_pp")
With this:
textOutput("SQL_pp", container = pre)
Upvotes: 4