简体   繁体   中英

rshiny: output doesn't change

I am having trouble with the drop-down menu options and their outputs. Although I can see the list of options the output remains the same and doesn't change even though the user can pick a different person. Any suggestions are welcome: My code is below (I removed some sensitive information):

server.r

senators <- read.csv("senators.csv")
output$senator <- renderUI({
  selectInput("variablex",
              #inputID = "senator",
              label = "Choose a U.S Senator from the list",
              selected = senators$name,
              choices = senators$name)
  })

  senTweets <- read.csv("person.year.count.csv")
  
  person <- reactive({
    #req(variablex)
    df <- senTweets %>% 
      group_by(input$variablex, year) %>% 
      top_n(input$a, n) %>%
      ungroup() %>%
      arrange(word, -n)
  })

  output$plot <- renderPlot({
    person () %>%  mutate(word = reorder(word, n))
      ggplot(data = person(), aes(word, n, fill = factor(year))) +
      geom_col(show.legend = FALSE) +
      facet_wrap(~ year, scales = "free") + scale_fill_viridis_d() +
      coord_flip() + labs(y="Word frequency", x="Term", title = paste("Top words used in 2020"))
    
  })
  
}

ur.r

ui <- dashboardPage(
  dashboardHeader(title = ""),
  dashboardSidebar(sidebarMenu(
    menuItem("Main", tabName = "Main", icon = icon("r-project")),
    menuItem("ReadMe", tabName = "ReadMe", icon = icon("readme"))
    )
  ),
  
  dashboardBody(
  tabItems(
    tabItem(
      tabName = "Main",
  
      sidebarPanel(
        helpText("text"),
        uiOutput('senator'),
        
        sliderInput(
          "a",
          label = "Select value to view top common words",
          min = 1,
          max = 10,
          value = 5
          ),
        ),
      
      
    mainPanel(
      plotOutput("plot")
    )
     
     
    ),
    tabItem(tabName = "ReadMe", 
            includeMarkdown("README.md"))
    ),
  )
)

Let's try some debugging. I don't have data which you have, so I don't see other possibilities. After reading your code there are two parts which I'm not sure if there are correct. Here is the first part:

person <- reactive({
    #req(variablex)
    df <- senTweets %>% 
      group_by(input$variablex, year) %>% 
      top_n(input$a, n) %>%
      ungroup() %>%
      arrange(word, -n)
  })

First of all, input$variablex is a name (senator's name), right? (Or should be at least). So I assume that senTweets data contains columns with the names which are present in senators$name . Otherwist it won't be possible to group by. input$variablex , as all inputs from shiny, is of type character , so the first thing is that you should probably use some knowledge about programming with dplyr ( Programming with dplyr ) and use .data[[input$variablex]] :

person <- reactive({
    #req(variablex)
    df <- senTweets %>% 
      group_by(.data[[input$variablex]], year) %>% 
      top_n(input$a, n) %>%
      ungroup() %>%
      arrange(word, -n)
  })

This should not be a problem with input$a because it is just a number and even if top_n() function expects number, probably there are a implicit type conversion.

You can change the code according my adive or first you can check if you really get what you want when user chooses senator's name. To do this, add browser() function here:

person <- reactive({
    #req(variablex)
    df <- senTweets %>% 
      group_by(input$variablex, year) %>% 
      top_n(input$a, n) %>%
      ungroup() %>%
      arrange(word, -n)
browser()
  })

(you may need to add req(input$variablex) at the beginning, otherwise it can be that you do not choose any senator and after opening the app, you will immediately will be moved to the console in RStudio)

When you open the app, choose senator, then go back to RStudio and you should be in debugging mode. Type df in the console and check if this table looks as you think it should.

The second part of your code which seems suspicious is this one:

output$plot <- renderPlot({
    person () %>%  mutate(word = reorder(word, n))
      ggplot(data = person(), aes(word, n, fill = factor(year))) +
      geom_col(show.legend = FALSE) +
      facet_wrap(~ year, scales = "free") + scale_fill_viridis_d() +
      coord_flip() + labs(y="Word frequency", x="Term", title = paste("Top words used in 2020"))
    
  })

Especially this: person () %>% mutate(word = reorder(word, n)) (it changes person() data, but do not save those changes.) doesn't seem to do anything useful: More sense would be to have:

output$plot <- renderPlot({
    person () %>%  mutate(word = reorder(word, n)) %>%
      ggplot(aes(word, n, fill = factor(year))) +
      geom_col(show.legend = FALSE) +
      facet_wrap(~ year, scales = "free") + scale_fill_viridis_d() +
      coord_flip() + labs(y="Word frequency", x="Term", title = paste("Top words used in 2020"))

  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM