0

The solution to my problem must be a one-liner, but I am making my baby steps with shiny. Please consider the example at

https://shiny.rstudio.com/gallery/telephones-by-region.html

Which I have modified so that it uses ggplot2 to generate the plot (and a scatterplot instead of a barplot). I paste the code below. Obviously there is a bug since ggplot2 does not plot the values of each columns on the y axis. Can anyone help me out? Thanks!

 library(tidyverse)
 library(shiny)


 # Rely on the 'WorldPhones' dataset in the datasets
 # package (which generally comes preloaded).
 library(datasets)

 nn <- row.names(WorldPhones) %>% as.numeric

 ww <- WorldPhones %>%
    as_tibble %>%
    mutate(year=nn) %>%
    select(year, everything())

 mycolnames <- colnames(WorldPhones)

 # Use a fluid Bootstrap layout
 ui <- fluidPage(    

     # Give the page a title
     titlePanel("Telephones by region"),

     # Generate a row with a sidebar
     sidebarLayout(      

    # Define the sidebar with one input
     sidebarPanel(
     selectInput("region", "Region:", 
              choices=mycolnames),
      hr(),
      helpText("Data from AT&T (1961) The World's Telephones.")
     ),

     # Create a spot for the barplot
     mainPanel(
      plotOutput("phonePlot")  
    )

    )
    )


 # Define a server for the Shiny app
  server <- function(input, output) {

  # Fill in the spot we created for a plot
   output$phonePlot <- renderPlot({

  ## plot(ww[,input$region]*1000, 
   ##         main=input$region,
   ##         ylab="Number of Telephones",
   ##      xlab="Year")

  ggplot(ww, aes(x=year, y=input$region
                      )) +
  geom_point(size=3, shape=1, stroke=2)  
  })
   }

  shinyApp(ui = ui, server = server)

1 Answer 1

1

The problem is you are passing a length 1 character vector here y=input$region. You could do this to actually pass the column:

ggplot(ww, aes(x=year, y=ww[[input$region]])) +
  geom_point(size=3, shape=1, stroke=2)  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.