我正在尝试从 rCharts 包中嵌入交互式图表。为了嵌入图表,我使用了这里的例子(Shiny app)。
该示例运行良好,但我的原型在没有图表输出的情况下工作(没有报告错误)。我的脚本如下:
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)
})
})
答案 0 :(得分: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)