2016-05-25 24 views
0

我試圖從rCharts包中嵌入交互圖表。爲了嵌入圖表,我使用了這裏的例子(Shiny app)。圖表(rCharts)未在網頁中顯示(Shiny)

該示例運行良好,但我的原型沒有圖表輸出(沒有錯誤報告)。我的腳本如下:

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) 

}) 

}) 

回答

1

我查閱了您的代碼,這裏有一些指針:

1)rPlot正在將數據作爲x~y以及color參數

2)它是更好,如果你使用eventReactive,並將其分配給df2(),而不是observe<<-全球賦值運算符

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) 

enter image description here

+0

感謝迅速答覆。這個例子看起來不錯:-) –