【问题标题】:R (Shiny) grid.circle and viewport output text onlyR(闪亮)grid.circle 和视口仅输出文本
【发布时间】:2017-04-07 02:33:00
【问题描述】:

我试图在viewport 上画一个圆圈,但我得到的唯一输出是文本。我的最终目标是在情节顶部画一个圆圈。这是我的代码:

library(reshape)
library(grid)

ui <- fluidPage(
  titlePanel("The Bomb Problem"),
  fluidRow(
   column(2, numericInput("numberOfPoints", "Number Of Points:", 0)),
   column(2, numericInput("radius", "Radius:", 0.5, min = 0, max = 1)),
   column(2, actionButton("btnRun", "Run"))

  ),
  mainPanel(
    vp <- viewport(x=0.5,y=0.5,width=0.9, height=0.9),
    pushViewport(vp),
    plotOutput(outputId = "points", width = "100%"),
    grid.circle(x=0.6, y=0.4, r=0.3, draw = TRUE)
  )
)
server <- function(input, output) {
  observeEvent(input$btnRun, {
    x<-sample(0:input$numberOfPoints, input$numberOfPoints, rep=TRUE)
    y<-sample(0:input$numberOfPoints, input$numberOfPoints, rep=TRUE)
    output$points <- renderPlot(plot(x, y), height = 800, width = 800)
    df <- melt(data.frame(x,y))
  })
}
shinyApp(ui, server)

但我得到的唯一输出是:

 0.5npc 0.5npc 0.9npc 0.9npc centre FALSE 0 0 0 0.5 GRID.VP.13

 0.6npc 0.4npc 0.3npc GRID.circle.10

换句话说,grid.circleviewport 没有绘制任何对象,它们只是输出它们的属性。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我终于想通了;通过在observerEvent 中的renderPlot 方法中绘制圆圈和绘图:

    require(plotrix)
    require(grid)
    require(reshape)
    
    ui <- fluidPage(
      titlePanel("The Bomb Problem"),
      fluidRow(
       column(2, numericInput("numberOfPoints", "Number Of Points:", 0)),
       column(2, numericInput("radius", "Radius:", 0.5, min = 0, max = 1)),
       column(2, actionButton("btnRun", "Run"))
    
      ),
      mainPanel(
        plotOutput(outputId = "points", width = "100%")
      )
    )
    server <- function(input, output) {
      observeEvent(input$btnRun, {
        x<-sample(0:input$numberOfPoints, input$numberOfPoints, rep=TRUE)
        y<-sample(0:input$numberOfPoints, input$numberOfPoints, rep=TRUE)
        output$points <- renderPlot({
          plot(x, y)
          draw.circle(0.5, 0.5, (input$numberOfPoints / 2) * input$radius, nv = 1000, border = NULL, col = NA, lty = 1, lwd = 0.5)
        }, height = 800, width = 800)
        df <- melt(data.frame(x,y))
    
      })
    }
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2016-01-28
      • 2017-02-27
      • 2020-06-12
      相关资源
      最近更新 更多