R Shiny: Read CSV and save in a variable with dplyr and ggplot

R Shiny: Read CSV and save in a variable with dplyr and ggplot



I'm trying to add a fileInput into my R Shiny program that users can add their own CSV/Excel files into, however, I'm running into some issues.



If I run the code reading the CSV directly from my PC (no fileInput) I have no problem, but anytime I try to input it I get errors.



I've done some research and tried to copy what I've seen online, but their solutions are not working for me. I've seen this post and this post already, but their methods are not working for. Or I'm still doing something wrong.


ui <- fluidPage(

theme = 'custom.css',

dashboardPage(
dashboardHeader(title = 'Student Grades'),
dashboardSidebar(
fileInput(inputId = 'file', label = 'Select a file...'),
selectInput(inputId = 'periodVar',
label = 'Select Period',
choices = unique(grades$Period)),
uiOutput('secondSelection'),
dataTableOutput('individualSummary'),
img(src='lhimg.JPG', width = '100%')
),

dashboardBody(
useShinyjs(),
tabsetPanel(
tabPanel(
'Test',
tableOutput('contents')
),
tabPanel(
'Graph',
plotOutput('individualGraph')
)
)
)
)
)

server <- function(input, output, session) {

# grades <- eventReactive(input$file,
# read.csv(input$file$datapath)
# )

grades2 <- reactive(
inFile <- input$file
if (is.null(inFile))
return(NULL)
myTbl <- read.csv(inFile$datapath)
return(myTbl)
)

# observe(
# grades2 = input$file
# if (is.null(file))
# return(NULL)
#
# grades2 = read.csv(grades2$datapath)
# )

tidyGrades <- function(data)
data %>%
gather(key = Type, value = Grade, (c('Test', 'Quiz', 'Homework')), na.rm = T) %>%
arrange(Period, Student,Date, Type) %>%
mutate(Weight = ifelse(Type == 'Homework', 30,
ifelse(Type == 'Quiz', 20, 50)),
Date = mdy(Date))


weightedMeanDf <- tidyGrades(grades2)



There is a small snippet of my code. I can post the entire thing if needed, but I don't want to make it difficult to read while trying to get help. The weightedMeanDf is just one thing I need to do, but I have a handful of other plots and tables that I need to generate as well. The commented codes are the different things I've tried based off solutions online. The closest I got was using the uncommented code (first reactive) function inside the server function.



But I get this error with it:


Error in UseMethod("gather_") :
no applicable method for 'gather_' applied to an object of class "c('reactiveExpr', 'reactive')"



I'm really at a loss and now I'm running out of ideas. Maybe I'm going about it entirely the wrong way. Maybe the best way is to use a reactive function for every plot and data frame created? I feel like that would just slow everything down as opposed to reading the csv file once, then generating graphs and plots based off the first reading?





Well, grades2 is a reactive expression. It it not a data.frame. If you want to get the value of that reactive expression, you should use grades2(). But it's unclear exactly how you are using weightedMeanDf. Most likely that would be a reactive expression as well. weightedMeanDf <- reactive( tidyGrades(grades2()) )
– MrFlick
Aug 21 at 19:40



grades2


grades2()


weightedMeanDf


weightedMeanDf <- reactive( tidyGrades(grades2()) )





I'm finding that out. I'm actually able to now print out a datatable after importing the file. But my issue is using that with ggplot and dplyr. Is there a way to convert a renderTable object to a dataframe?
– Thomas Kellough
Aug 21 at 19:51






Well, you have to pass a data.frame to renderTable. Just make that data source a reactive object then you can use the same object in renderTable and renderPlot or whatever else you like. It seems like maybe your question is not doing a good job of actually describing the problem you are facing.
– MrFlick
Aug 21 at 20:00





You're probably right. I apologize for not being clear. But I think you answered my question with your first comment. I wasn't using grades2() and left off the parentheses. I still have a lot of work to figure out to change up my program, but I think I should be able to get it now. Thank you so much for helping me!
– Thomas Kellough
Aug 21 at 20:17




1 Answer
1



How about this???


library(shiny)
library(digest)

# Create data to read
write.csv(file="~/iris.csv",iris)

shinyApp(ui=shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
textInput("path","Enter path: "),
actionButton("readFile","Read File"),
tags$hr()
),
mainPanel(
tableOutput('contents')
)))
),
server = shinyServer(function(input,output,session)
file <- reactiveValues(path=NULL,md5=NULL,rendered=FALSE)

# Read file once button is pressed
observeEvent(input$readFile,
if ( !file.exists(input$path) )
print("No such file")
return(NULL)

tryCatch(
read.csv(input$path)
file$path <- input$path
file$md5 <- digest(file$path,algo="md5",file=TRUE)
file$rendered <- FALSE
,
error = function(e) print(paste0('Error: ',e)) )
)

observe(
invalidateLater(1000,session)
print('check')

if (is.null(file$path)) return(NULL)

f <- read.csv(file$path)
# Calculate ckeksum
md5 <- digest(file$path,algo="md5",file=TRUE)

# If no change in cheksum, do nothing
if (file$md5 == md5 && file$rendered == TRUE) return(NULL)

output$contents <- renderTable(

print('render')
file$rendered <- TRUE
f
)
)

))





Thank you! This is a great example!
– Thomas Kellough
Aug 22 at 17:42





No prob. Please mark it as helpful.
– ryguy72
Aug 22 at 17:45






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)