2

I am currently trying to teach myself shiny, and am having a little trouble. I want to make an app that has a menu for which dataset the user would like to plot from, followed by another menu for which variable they would like to plot (against the date column)

I have managed to get the reactive menus working, but i am struggling to work out how to 'call' the selected dataset. I have read a lot of posts (e.g 1,2) but none seem to deal with the multiple dataset problem.

My code currently looks like this, with my issues being on the last few lines (how do I get to the mydata within outVar()?!?).

If anyone could point me in the right direction that would be great!

library(shiny)
library(ggplot2)

datafiles<-c("Data/try2.txt","Data/Poland.txt","Data/Romania.txt","Data/Hungary.txt","Data/KHR.txt")


runApp(list(
  ui = bootstrapPage(
    selectInput('dataset', 'Choose Dataset', datafiles),
    selectInput('columns', 'Columns', ""),
    plotOutput('graph')
  ),

  server = function(input, output, session){
    outVar = reactive({
      mydata = read.table(paste0(input$dataset), sep = '\t', header=TRUE)
      names(mydata)

    })
    observe({
      updateSelectInput(session, "columns",
                        choices = outVar()
      )})
    output$graph<-renderPlot({data<- outVar$mydata
    plot(data$Date,data$paste0(input$columns))})
  }
))

shinyApp(ui=ui, server=server)    
3
  • are you getting an error message in your app? if so, what is it saying? have you tried data <- outVar()$mydata in the output$graph ? Commented Jan 26, 2017 at 14:34
  • just tried adding the outVar()$mydata, still getting the same error (Error in $: $ operator is invalid for atomic vectors). Commented Jan 26, 2017 at 14:49
  • I couldn't get your code to work with the data I used. I kept getting a different error however. I took out the observe event in the answer below. You should be able to do what you want without that. Commented Jan 26, 2017 at 16:06

1 Answer 1

2

I didn't have your data, but, I used the wine quality data to build this. https://archive.ics.uci.edu/ml/datasets/Wine+Quality

Hope this helps

library(shiny)
library(tidyverse)
library(ggplot2)


datafile1 <- read_delim(file = "Data/winequality-red.csv", delim = ";")
datafile2 <- read_delim(file = "Data/winequality-white.csv", delim = ";")
# datafiles<-c("Data/try2.txt","Data/Poland.txt","Data/Romania.txt","Data/Hungary.txt","Data/KHR.txt")

datafiles <- list(datafile1, datafile2)

ui <- fluidPage(
  selectInput('dataset', 'Choose Dataset', choices = c("Red" = "1", "White" = "2")),
  selectInput('column', 'Choose column', choices = c("2" = "2", "3" = "3")),
  plotOutput('graph')
)

server = function(input, output, session){

  outVar <- reactive({
    temp <- datafiles[[as.numeric(input$dataset)]]
    temp <- temp[,c(1, as.numeric(input$column))]
  })

  output$graph <- renderPlot({
    plot(outVar())
  })
}


shinyApp(ui=ui, server=server)  
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks for your help! In the first temp<- as.data.frame() line, is the as.numeric necessary? the first column is a column of dates and so i'm getting the error 'need finite xlim values'. If i remove the as.numeric then i get 'undefined columns selected'.
as.numeric would be necessary because when you're selecting from the input your choices are being defined as a character rather than a numeric. you want to change to a numeric so that you can use that to select your column. the first temp line is only selecting the dataset from the list of datasets. the second temp line has a 1, and the selected column (I assumed that column one was your date column). So it's going in steps, first taking the right data, making it a dataframe, then taking the 1st column and the column you select from your drop down list.
I guess the question is how is your data structured? based on your question, I assumed that each dataset had several columns, with column 1 being the date column and columns 2, ..., n being some value that you want to plot against the date? I realized that you don't need to include the as.data.frame above, so I've taken that out.
Hi, sorry i wrote a reply but forgot to send... It's been a long day. You correctly assumed the date is always the first column. It's just that when i run the code it gives the error NAs introduced by coercion ... need finite xlim values. When i run it with two numeric columns plotted against each other it works perfectly!
I think that'll be more an issue with the plot than the app itself. if you include: output$table <- renderTable({ outVar() }) in your server and tableOutput('table') in your UI below the plotOutput you should see how the data is structured. Then it will be a matter of updating the plot function or using ggplot to do what you want. I don't have access to time series data so I can't be of much more help but I think you're 95% of the way there. If this solves your question make sure to mark and upvote if helpful
|

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.