Search code examples
rshinydashboardshiny-server

use reactive dataset in shiny R and draw a plot


I want to draw a plot using shiny R when I select items from selectInput .The problem is in the selection of items from select input, I have tried plot(mydata$input$getCol1,mydata$input$getCol2],type="l",col="blue") but i got another error which tells, need finite 'xlim' values. I also checked the NA there where no Na in my dataset.

My UI.R code is as follows

library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title="Engagement and Passionate",titleWidth = 350),

# Sidebar ######################################
dashboardSidebar(
width = 150,
sidebarMenu(id = "mysidebar",
            menuItem("Engagement", 
                     menuSubItem("Bittorent", tabName = 
"subItemOne"),
                     menuSubItem("Twitter", tabName = "subItemTwo"),
                     menuSubItem("Tumblr", tabName = 
 "subItemThree"),
                     menuSubItem("Youtube", tabName = 
 "subItemFour"),
                    menuSubItem("Dailymotion", tabName = 
 "subItemFive")),
            menuItem("Charts",
                     menuSubItem("AP", tabName = "APC"),
                     menuSubItem("Anp", tabName = "ANPC"))
)),
  # Body #######################################


 dashboardBody(
 fluidRow(box(width = 3,h2("Upload file"),fileInput("file2", "Choose 
  CSV File",
                                                    accept = NULL)),
         box(width=8,h2("Raw Data"),DT::dataTableOutput("pltable")
         )
    ),
   tabItems(
   tabItem(tabName = "subItemOne",
         h2("Bittorent Insight"),
         box(title="select the variable",width=3 
 ,uiOutput("getCol1"),uiOutput("getCol2"),status = 
 "warning",solidheader=TRUE),
         plotOutput("graph"))
  ,

  tabItem(tabName = "subItemTwo",
          h2("Twitter Insight")
  ),

  tabItem(tabName = "subItemThree",
          h2("Tumblr Insight")
  ),

  tabItem(tabName = "subItemFour",
          h2("Youtube Insigth")
  ),
  tabItem(tabName = "subItemFive",
          h2("Daily motion Insigth")
  ))))

Server.R

  options(shiny.maxRequestSize=30*1024^2)
  server <- function(input, output) { 

  #Function to read table 
  rawdata <<- reactive({
  file1 <- input$file2
if(is.null(file1)){return()}
read.csv(file=file1$datapath,1,stringsAsFactors=FALSE)
 })
 output$getCol1<-renderUI({
 selectInput("Variable1","Choose 
 Option:",as.list(colnames(rawdata())))
  })
  output$getCol2<-renderUI({
selectInput("Variable2","Choose 
 Option:",as.list(colnames(rawdata())))
})
 #raw Datatable for platform

   output$pltable<-DT::renderDataTable({
  if(is.null(rawdata)){return()}
  DT::datatable(rawdata(),
 extensions="Responsive",options=list(pageLength=3),class='cell- 
  border strip',selection='single')
  })
  #access rawdata 
 output$graph <- renderPlot({
 mydata <- as.data.frame(rawdata())
 # p <- ggplot(mydata, 


 aes(mydata$input$getCol1,mydata$input$getCol2,
color=factor(mydata[3]))) + geom_point()
#ggplotly(p)
plot(x=mydata[input$getCol1],
y=mydata[input$getCol2],type="l",col="blue")
#plot(mydata[2])
 })

 }

The error that I received is as follows: only one column in the argument to 'pairs' I also tried the following code

output$graph <- renderPlot({
    mydata <- as.data.frame(rawdata())
    ggplot(mydata, aes_string(x = input$getCol1, y = input$getCol2))

     })

it does not show any line in the graph however it doesnot show any error as well. Could you please let me know what is this problem.


Solution

  • You need to call id of select input which are Variable1 and Variable2 instead of calling getCol1 and getCol2 Try this :

    output$graph <- renderPlot({
       df<- rawdata()
       plot(df[,input$Variable1],df[,input$Variable2])
    
     })
    

    Or you can try the following code:

    ggplot(df, aes_string(input$Variable1,input$Variable2))+geom_point(colour=‘blue’)