Reputation: 464
I am trying to embed the interactive chart from rCharts package. To embed the chart I have used the example from here (Shiny app).
The example works well but my prototype works without the chart output (no errors have been reported). My script is as follows:
ui.r:
library(shiny)
require(rCharts)
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
showOutput("myChart", "polycharts")
)
)
))
server.r:
library(shiny)
require(rCharts)
shinyServer(function(input, output) {
observeEvent(input$bins,{
df2 <<- data.frame(x=c(1:input$bins),y=c(1:input$bins))
})
output$myChart <- renderChart({
print(df2)
p1 <- rPlot(df2$x,df2$y, data = df2, color='green', type = 'point')
p1$addParams(dom = 'myChart')
return(p1)
})
})
Upvotes: 0
Views: 33
Reputation: 29387
I have reviewed your code and here are some pointers:
1) rPlot
is taking data as x~y
along with color
argument
2) It is better if you use the eventReactive
and assign it to df2()
instead of observe
and <<-
global assignment operator
rm(list = ls())
library(shiny)
require(rCharts)
server <- function(input, output) {
df2 <- eventReactive(input$bins,{data.frame(x=c(1:input$bins),y=c(1:input$bins))})
output$myChart <- renderChart({
p1 <- rPlot(x~y, data = df2(), color='green', type = 'point', color = 'x')
p1$addParams(dom = 'myChart')
return(p1)
})
}
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(sidebarPanel(sliderInput("bins","Number of bins:", min = 1,max = 50,value = 30)),
# Show a plot of the generated distribution
mainPanel(showOutput("myChart", "polycharts"))
)
)
shinyApp(ui, server)
Upvotes: 1