Shiny:改进/格式化 verbatimTextOutput() 的文本输出
我
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Interactive CLicks"),
br(),
fluidRow(
br(),
verbatimTextOutput("info_hover"),
plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
br(),
verbatimTextOutput("info_click"),
verbatimTextOutput("info_db_click"),
verbatimTextOutput("double_to_single_click"))
)
## server.R
server <- function( input, output, session){
#Click Points
single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
observeEvent(input$plot_click, {
single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
})
observeEvent(input$plot_db_click, {
double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
})
## RenderPlot
output$distPlot <- renderPlot({
plot(1, 1,
xlim=c(0,10), ylim=c(0,10))
#Click points
points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17)
points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17)
segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 = double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)
})
output$info_click <- renderText({
paste0("Single Click Multiple", "\nX=", single.source_coords$xy[2,1], " Y= ",single.source_coords$xy[2,2], "\nX=", round(single.source_coords$xy[2,1],4), " Y= ",round(single.source_coords$xy[2,2], 5))
})
output$info_db_click <- renderText({
paste0("Double Multiple", "\nX=", double.source_coords$xy[2,1], " Y= ",double.source_coords$xy[2,2], "\nX=", round(double.source_coords$xy[2,1],2), " Y= ",round(double.source_coords$xy[2,2],4))
})
output$info_hover <- renderText({
paste0("Hover Multiple", "\nX=", input$plot_hover$x*2, " Y= ",input$plot_hover$y*2, "\nThis is X=", round(input$plot_hover$x*2,5), " not A but Y= ",round(input$plot_hover$y*2,4))
})
}
### Run Application
shinyApp(ui, server)
在 verbatimTextOutput()
的 3 个输出区域中,由于标签和输出的长度不同,verbatimTextOutput()
的结果看起来很糟糕
有什么方法可以对齐标签和输出,使其更美观吗?我喜欢 verbatimTextOutput() 的紧凑性,我不想转到表输出。我只是想找到一种方法来改进 verbatimTextOutput()
的格式。
I have
library(shiny)
ui <- fluidPage(
# App title ----
titlePanel("Interactive CLicks"),
br(),
fluidRow(
br(),
verbatimTextOutput("info_hover"),
plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
br(),
verbatimTextOutput("info_click"),
verbatimTextOutput("info_db_click"),
verbatimTextOutput("double_to_single_click"))
)
## server.R
server <- function( input, output, session){
#Click Points
single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1), y=c(1,1)))
observeEvent(input$plot_click, {
single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
})
observeEvent(input$plot_db_click, {
double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
})
## RenderPlot
output$distPlot <- renderPlot({
plot(1, 1,
xlim=c(0,10), ylim=c(0,10))
#Click points
points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17)
points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17)
segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 = double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)
})
output$info_click <- renderText({
paste0("Single Click Multiple", "\nX=", single.source_coords$xy[2,1], " Y= ",single.source_coords$xy[2,2], "\nX=", round(single.source_coords$xy[2,1],4), " Y= ",round(single.source_coords$xy[2,2], 5))
})
output$info_db_click <- renderText({
paste0("Double Multiple", "\nX=", double.source_coords$xy[2,1], " Y= ",double.source_coords$xy[2,2], "\nX=", round(double.source_coords$xy[2,1],2), " Y= ",round(double.source_coords$xy[2,2],4))
})
output$info_hover <- renderText({
paste0("Hover Multiple", "\nX=", input$plot_hover$x*2, " Y= ",input$plot_hover$y*2, "\nThis is X=", round(input$plot_hover$x*2,5), " not A but Y= ",round(input$plot_hover$y*2,4))
})
}
### Run Application
shinyApp(ui, server)
In the 3 output areas of verbatimTextOutput()
because of the different length of the labels and the outputs the result of the verbatimTextOutput()
looks awful
Is there any way to align Labels and Outputs so that it is more aesthetically pleasing? I like the compactness of verbatimTextOutput()
I would not like to go to a table output. I would just like to find a way to improve the formatting of verbatimTextOutput()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论