Marco De Virgilis
Marco De Virgilis

Reputation: 1089

Shiny R, Plot, ShinyApp

I am trying to write a simple app in Shiny R. I would like to have two inputs (x and y) and plot the relative scatter plot. The code is the following

library(shiny)

ui<-fluidPage(
headerPanel('Plot'),

sidebarPanel(
sliderInput(inputId = 'x',label='X', value = 1,min=1,max=3),


sliderInput(inputId = 'y',label='Y', value = 1,min=1,max=3)
),

mainPanel(
plotOutput('plot')
)
)

server<-function(input,output) {
x <- reactive({input$x})

y <- reactive({input$y})

output$plot <- renderPlot({plot(x,y)}) 
}


shinyApp(ui=ui, server=server)

The code produce an error,

cannot coerce type 'closure' to vector of type 'double'

How can I correct this?

Thank you very much

Upvotes: 0

Views: 151

Answers (3)

SibyllWang
SibyllWang

Reputation: 111

input values are already reactive so there's no need to wrap them in reactive() function. Here's your shiny in a neater, working way:

library(shiny) {

    ui<-fluidPage(
    headerPanel('Plot'),

    sidebarPanel(
        sliderInput(inputId = 'x', label= 'X', value = 1, min= 1, max= 3),
        sliderInput(inputId = 'y', label= 'Y', value = 1, min= 1, max= 3)
    ),

    mainPanel(plotOutput('plot'))

    server<-function(input, output) {
        output$plot<- renderPlot({
        plot(input$x, input$y)
    }) 

}

shinyApp(ui= ui, server= server)

Upvotes: 1

SANBI samples
SANBI samples

Reputation: 2118

You could use this server argument instead:

server <- function(input,output) {
  output$plot <- renderPlot(plot(input$x,input$y)) 
}

Upvotes: 1

Pork Chop
Pork Chop

Reputation: 29407

X and Y are functions so add () to them

output$plot <- renderPlot({plot(x(),y())})

Upvotes: 4

Related Questions