R闪亮错误“ 3个参数传递给'$',需要2

时间:2019-07-19 14:38:15

标签: r shiny

我正在尝试复制this交互式堆积条形图

运行以下代码时,出现错误“ 3个参数传递给'$',需要2个”。我是R Shiny的新手,但无法修复它。如果有人有任何想法,我将不胜感激。

对不起,我无法在这篇文章中包含我的excel文件...

1)导入并准备数据框

df_shiny1 <- read.xlsx(paste(import_filepath," df_shinyChart.xlsx",sep=""), sheetName = "Sheet1")

df_shiny1 <- t(df_shiny1)

df_shiny1 <- data.frame(rownames(df_shiny1),df_shiny1)
df_shiny1 <- df_shiny1[1:nrow(df_shiny1)-1,]
colnames(df_shiny1) <- c("topics","1985",
                         "1986",
                         "1987",
                         "1989",
                         "1990",
                         "1993",
                         "1995",
                         "1996",
                         "1997",
                         "1998",
                         "1999",
                         seq(2000,2018))

df_shiny1 <- df_shiny1[-c(1),]
rownames(df_shiny1) <- seq(1,10)


df_restructured <- df_shiny1 %>%
  tidyr::gather(key="year",value="df_shiny1",2:32)

df_restructured$df_shiny1 <- as.numeric(as.character(df_restructured$df_shiny1))

categoryorder <- c("supervision_regulation",
                   "financial_stability",
                   "community",
                   "MP",
                   "economy",
                   "international",
                   "financial_cond_housing",
                   "payments",
                   "research",
                   "Unmatched")

2)构建UI部分

# Shiny app = UI.R contains UI widget + server.R that responds when a user interacts with the UI


ui <- shinyUI( 
    fluidPage( # header panel
    fluidRow(column(width = 10, style="font-size : 25pt;
                                       line-height: 40pt;
                                       width = 100 ",
                    tags$strong ("Speeches by Board Members and regional Feds presidents")),
             column(width = 2)),

    # Sidebar with user input options

  sidebarPanel(#style = "background-color: #FFFFFF;",
               #tags$style(type='text/css',
                        #  ".selectize-input {
                         #     font-size: 12pt;
                          #    line-height: 13pt;}
                           # .selectize-dropdown {
                            #  font-size: 12pt;
                             # line-heigth: 13pt;}"),
              # width = 3,

               # Check box for categories
               checkboxGroupInput("topics",
                                  label = HTML(
                                    '<p style = "color:white;
                                    font-size:12pt">
                                    Choose high level topics: 
                                    </p>'),
              choices = categoryorder,
              selected = categoryorder),

              # Text with additional information
              helpText(
                HTML('<p style="color:black;
                     font-size: 9pt" > Choose the topic you want to plot.
                                       Proportion of topics j = Nber of words from
                                       topic j / Total Nber of matched words </p>'))
              ),

              # Main panel with output 
              mainPanel(plotOutput("mplot"),
                        style = "width:100%")
              )
)

3)构建服务器部分

# Build the server part

server <- shinyServer(function(input,output){
data <- reactive ({# reactive part = the code is repeated when user input changes

  validate( # define error message if user doesn't choose any thing
    need(input$topics !="","Please select at least one topic"))

  # Filter data according to user input
  plotdata <- df_restructured %>% 
    as.data.frame() %>% 
    filter(topics %>% input$topics)

  # Define a reactive scale for y-axis
  scalecalc <- plotdata %>% group_by(topics) %>% summarize(value = max(df_shiny1))
  scalemax <- (scalemax$value)
  scalesteps <- round(scalemax/5, digits = -1)

  list(plotdata = plotdata,
       scalemax = scalemax,
       scalesteps = scalesteps)
  })

# Build plot output with ggplot2
output$mplot <- renderPlot({
  mplot <- ggplot(data = data()$plotdata,
                   aes(as.factor(year), y = df_shiny1 ,fill=factor(topics),
                       order=topics)) + 
    scale_y_continuous(breaks = 
                         seq(0,data()$scalemax,
                             data()$scalesteps),
                      labels = abs(seq(0,
                                       data()$scalemax,
                                       data()$scalesteps)))+
    geom_bar(stat="identity") + 
    labs(title = "Speeches by Board Members and Regional Fed presidents",
         x="",
         y="Number of speeches/year",
         legend="") +
    theme_gray() +
    #scale_fill_manual(values=c("#99CCFF","#0066CC","#FF9933",
    #"#FF6666","#006600","#FF0000",
    #"#990033","#FF99FF","#333333",
    #"#FF9999")) +
    scale_fill_brewer(palette = "Paired") +
    theme(legend.title=element_blank(),
          plot.title = element_text(hjust = 0.2))
    #legend.position = "bottom") +
print(mplot)
})
})

shinyApp(ui = ui, server = server )

最后,我想制作一个响应式堆叠条形图,用户可以在其中选择要绘制的主题。

0 个答案:

没有答案