Shiny:改进/格式化 verbatimTextOutput() 的文本输出

发布于 2025-01-13 11:28:49 字数 2689 浏览 0 评论 0原文

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文