我已经阅读了使用HTML在Shiny中打印单独行的各种方法但不满意。我的目标是将各种自变量并入,并将代码作为SQL语句提供,然后用户可以使用该语句在SAS中进行存储。我最终希望在我的闪亮应用程序中看到的文本是执行以下代码的结果:
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"))
}
我闪亮的应用程序中生成的文本应为:
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"))
)
)
问题是我想像上面的输出一样打印。相反,我的输出看起来都是乱七八糟的。它仍然会执行,但我希望它看起来很漂亮(即不像这样:
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
答案 0 :(得分:3)
HTML默认忽略换行符;您可以使用pre
元素来保留换行符和间距。替换这个:
textOutput("SQL_pp")
有了这个:
textOutput("SQL_pp", container = pre)