'Why are the objects I create of type closure?
I've been trying to create a shiny app which would plot a quadratic bezier curve with the points provided by the user, but no matter what I try, some my objects, which should've been numeric vectors, are of type 'closure'.
Here's the code in question:
library(shiny)
dat11 <- data.frame(t=seq(0, 1, 0.01))
ui <- fluidPage(
titlePanel("proj"),
sidebarLayout(
sidebarPanel(
numericInput("xcrd1", "input 1st x coordinate", value=-1),
numericInput("ycrd1", "input 1st y coordinate", value=0),
numericInput("xcrd2", "input 2nd x coordinate", value=0),
numericInput("ycrd2", "input 2nd y coordinate", value=1),
numericInput("xcrd3", "input 3rd x coordinate", value=1),
numericInput("ycrd3", "input 3rd y coordinate", value=0)
),
mainPanel(
plotOutput("plt1")
)
)
)
server <- function(input, output) {
xbez <- function(t) {
reactive({
input$xcrd1*(1-t)^2+2*input$xcrd2*t*(1-t)+input$xcrd3*t^2
})
}
ybez <- function(t) {
reactive({
input$ycrd1*(1-t)^2+2*input$ycrd2*t*(1-t)+input$ycrd3*t^2
})
}
xc = xbez(dat11$t)
yc = ybez(dat11$t)
xcrd = as.numeric(unclass(xc))
ycrd = as.numeric(unclass(yc))
output$plt1 <- renderPlot(
{plot(xcrd,ycrd)}
)
}
shinyApp(ui=ui, server=server)
That's the latest version. I've tried a few things, but the problem always seems to revolve around the 'closure' type. Granted, I'm not that experienced in shiny, so my question is what here is of type 'closure' and how do I fix it so I can plot it/dataframe it/work with it or whatever?
Solution 1:[1]
You are mixing up functions and reactives in a wrong way. To achieve your your desired result you could rewrite your code like so:
library(shiny)
dat11 <- data.frame(t = seq(0, 1, 0.01))
ui <- fluidPage(
titlePanel("proj"),
sidebarLayout(
sidebarPanel(
numericInput("xcrd1", "input 1st x coordinate", value = -1),
numericInput("ycrd1", "input 1st y coordinate", value = 0),
numericInput("xcrd2", "input 2nd x coordinate", value = 0),
numericInput("ycrd2", "input 2nd y coordinate", value = 1),
numericInput("xcrd3", "input 3rd x coordinate", value = 1),
numericInput("ycrd3", "input 3rd y coordinate", value = 0)
),
mainPanel(
plotOutput("plt1")
)
)
)
server <- function(input, output) {
bez <- function(t, crd1, crd2, crd3) {
crd1 * (1 - t)^2 + 2 * crd2 * t * (1 - t) + crd3 * t^2
}
xcrd <- reactive({
bez(dat11$t, input$xcrd1, input$xcrd2, input$xcrd3)
})
ycrd <- reactive({
bez(dat11$t, input$ycrd1, input$ycrd2, input$ycrd3)
})
output$plt1 <- renderPlot({
plot(xcrd(), ycrd())
})
}
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stefan |

