Manu Sharma
Manu Sharma

Reputation: 1739

Shiny: reactive() check on read.csv

I am writing a server script of shiny where I want reactive() to first check the data on my c drive, if it's there then well and good else do the other data processing and save it for the next iteration. And the name of data is dependent on input$var

So that next time it will be really quick to create the charts
Following is just a running example of my big problem

library(shiny)
library(datasets)
library(ggplot2)
mt=mtcars
shinyServer(function(input, output) {
data1 =reactive({
 if(file.exists("input$var.csv")
   {data=read.csv(input$var.csv)})
  else{    
    data=mt[mt$cyl==input$var,] 
  write.csv(data,file="c:\\input$var.csv")
    }
  })  
  output$Plot1 <- renderPlot({
    data2=data1()
    ggplot(data2$d,aes(x=gear,y=wt))+geom_boxplot() })

})

Upvotes: 0

Views: 1908

Answers (1)

Smich7
Smich7

Reputation: 460

Use paste0 as timfaber said you. In R functions which deals with files you have to give a complete string then paste0 allows you to give a string like "name_with_what_is_in_input$var.csv".

ibrary(shiny)
library(datasets)
library(ggplot2)
mt=mtcars
shinyServer(function(input, output) {
data1 =reactive({
 if(file.exists(paste0(input$var,".csv"))
   {data=read.csv(paste0(input$var,".csv"))})
  else{    
    data=mt[mt$cyl==input$var,] 
  write.csv(data,file=paste0("c:\\",input$var,".csv"))
    }
  })  
  output$Plot1 <- renderPlot({
    data2=data1()
    ggplot(data2$d,aes(x=gear,y=wt))+geom_boxplot() })

})

Upvotes: 1

Related Questions