1

我对在闪亮中使用 R 时打印动态输出有点困惑。以下是我的闪亮代码,它可以打印出 ggplot2 图表,但不能与新数据框一起打印。我只想知道如何同时打印出“mydata”。谢谢。

library("reshape2")
library("ggplot2")

服务器.R

shinyServer(function(input, output) {

output$main_plot <- renderPlot({
    #Draw gragh to compare the forecast values with real data
    mydata <- data.frame(years, A, B)   
    df <- melt(mydata,  id = 'years', variable = 'series')      
    print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))
    
})

用户界面

shinyUI(pageWithSidebar(

# Sidebar with controls to select a dataset
# of observations to view
 
sidebarPanel(
 
selectInput("dataset", "Choose a dataset:", 
      choices = c("Netflix"))),
  
plotOutput(outputId = "main_plot", height = "500px")
 
))
4

1 回答 1

1

将数据框拉出到反应式表达式中,以便您可以从两个不同的输出中使用它。(这是引入和重用变量的反应模拟。)

服务器.R

shinyServer(function(input, output) {

    mydata <- reactive({
        data.frame(years, A, B)
    })

    output$main_plot <- renderPlot({
        #Draw gragh to compare the forecast values with real data
        df <- melt(mydata(),  id = 'years', variable = 'series')      
        print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))

    })

    output$data <- renderTable({ mydata() })
})

用户界面

shinyUI(pageWithSidebar(

# Sidebar with controls to select a dataset
# of observations to view

sidebarPanel(

selectInput("dataset", "Choose a dataset:", 
      choices = c("Netflix"))),

plotOutput(outputId = "main_plot", height = "500px"),
tableOutput(outputId = "data")

))
于 2013-08-05T04:21:14.990 回答