I have a shiny app hosted on a server, and part of its functionality is to read local files. I realise there is the very helpful fileIput function in shiny package - and I may use it instead - but for now I would like to learn about using file paths. The problem I face follows:
I am using the tm package, which allows users to read text files either from a directory (using DirSource("filePath"))or using inidividual files (using VectorSource("filePath")).
initialCorpus<- reactive({
if(input$confirm==0)
return()
isolate({
if(input$corpusType=="dir"){
myPath<- input$filePath
myCorpus<- Corpus(DirSource(myPath))
myCorpus
}
else if(input$corpusType=="vector"){
myPath<- input$filePath
myFile<- scan(file=myPath,what="character",n=-1, sep="\n")
myCorpus<- Corpus(VectorSource(myFile))
myCorpus
}
...
The same function works fine and reads in text files when I am using my shiny app locally. However, when I upload my app to shinyapp, and then try to upload a local file, I am unable to read in files.
So, why is it not possible to read in local files by shinyApp when the file path is used? It may be a basic question, but I want to learn.
Thanks in advance.
PS. I would be happy to link to my application if required, it's just that I would like to show my application when it's properly functioning.
I think you can solve your problem by isolating the part where you source your files : isolate({DirSource("filePath")})
Related
I'm attempting to make a public shinyapps.io website, and I'm trying to use image_write to create a file into a local directory.
The following code works on my local R studio code:
image_write(im.resized, path = paste0(output_file_directory, file_name), format = "jpg")
When I run the code on the shinyapps.io website, the code runs without error, but I'm not sure where it downloads the file to. I know that the output_file_directory part isn't the issue, so I'm a little lost. Any help would be much appreciated!
On shinyapps.io it is not possibly to store permanently data, due to:
"Shinyapps.io is a popular server for hosting Shiny apps. It is designed to distribute your Shiny app across different servers, which means that if a file is saved during one session on some server, then loading the app again later will probably direct you to a different server where the previously saved file doesn’t exist."
See here:
https://shiny.rstudio.com/articles/persistent-data-storage.html
Im tring to create a shiny app that read and online onedrive xlsx file and show some things, but for the moment Im unable to read the onedrive xlsx file, I already explore the Microsoft365R and I can conect to my onedrive and I even can open the fil but... what it does is from r open a tab in chrome with the excel file.
I need the file in the local enviroment of r.. this its beacause the shiny app must be deploy in a web server, that every time the app runs it reads the update the file.
library(Microsfot365R)
odb <- get_business_onedrive()
odb$open_file("lcursos.xlsx")
Also this its a business account, so I also have to put the username and key to acces each file, that its beacause use the simple url doesnt work, it says Error 403 FORBIDEEN.
Any ideas?
Thank you so much!
Use the download_file() method to download the file to your local machine:
odb$download_file("lcursos.xlsx")
You can set the location of the download with the dest argument. Once it's downloaded, open it with the xls reader package of your choice. I suggest either openxlsx or readxl.
Note that if your file is password protected, your options are limited. See this question for possible solutions.
I have a shiny app that I deployed as a library. Inside my shiny app, I write a json file in real time, I want to write this file in the working directory of each user. Nevertheless, the file is written in the directory of the library.(Please refer the code below)
app.R
library(Tree)
ui <- htmlTemplate("www/Tree.html",
text_output = tableOutput("table2")
)
server <- function(input, output, session){
# This block fires each time we receive a message from JavaScript
output$table2 <- renderTable({
#Write json file
json_value = input$jsonData
write(json_value, paste0(getwd(),"/",fileName, ".json"))
})
}
# Run the application
shinyApp(ui , server)
I though that with the function getwd()I will be able to see my json file in my working directory but taht is not happening. My json file is written in the directory of the library
This directory
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Tree/
How can I change my code to be able to see my jsonfile in the RProject which the user wants to work?
A shiny app is running in your browser and, as a concequence, subject to strict safety regulations imposed by the browser. There are plenty of restrictions regarding the file system to make sure that web pages cannot freely roam your hard drive. That is why there might not be a solution with a good user experience to achieve what you want.
The safest solution would be the download button (as suggested by #HubertL).
If you want to work with the users' home directory you can get the path with file.path(Sys.getenv("HOME")) (at least on Windows it works). Now you can work with that path. Use OutputPath <- file.path(Sys.getenv("HOME"), fileName) to create the path %HOME%/fileName.
The third alternative (I can think of) is a simple textInput. Users can paste a string there that represents the path to the directory where they want the file to be stored. Check the string with dir.exists and use it if it is valid.
I made a relatively elaborate Shiny app for my job that runs great locally. However, I am trying to host the app so that users who don't have R Studio downloaded can access it. I cannot get the app to run through shinyapps.io. It seems this is mostly related to the fact that it cannot find the files that are located on Dropbox. The app is based almost entirely on loading and writing files on Dropbox. I tried to change the file paths and use rdrop2 to load the files, but it changes the formatting of some things and would be pretty complicated to reconcile as far as I can tell. I'm very much a novice programmer and the thought of having to restructure the entire app is giving me a bit of anxiety and will certainly require a fair amount of effort. Does anyone know of a more "simple" way to modify files located on Dropbox through a shiny app hosted on shinyapps.io, preferably while still able to use the "openxlsx" package? Thank you very much in advance.
One workaround I thought might work but didn't was to make the file path to the Dropbox file specific to the user because anyone using the app should have access to Dropbox:
this.data <- as.data.frame(read.xlsx(paste("C:\Users\", Sys.info()[["user"]], "\Dropbox\rest of the file path", sep = "")))
Disclaimer : I would not recommend relying on google-unsubmmitted URLs to guarantee privacy.
Modify the share link copied from DropBox replacing dl=0 by dl=1 to make the download start rather than display in DropBox UI.
You can then download.file() into a tempfile() before read.xlsx() it:
library(shiny)
library(openxlsx)
library(DT)
ui <- fluidPage(
titlePanel("XL Read from dropbox"),
mainPanel( DTOutput("dt"))
)
server <- function(input, output) {
tmpfile <- tempfile(fileext='.xlsx')
download.file(url = "https://www.dropbox.com/s/1v0l...5u803a9hg/my_file.xlsx?dl=1", destfile = tmpfile , mode="wb")
output$dt <- renderDT(read.xlsx(outfile))
}
shinyApp(ui = ui, server = server)
My app runs fine locally and I am able to successfully deploy my app to the shinyapps.io server, but I get the following error message when I try and load the app in my browser using the shinyapps URL: "Error object 'data' not found.' I think this is because the 'data' variable reads from a csv file on my local directory. Is there a way I can upload this csv file to the shinyapps server? I've tried looking this up but I've found nothing.
Here's the code I am using to read in the files. I'm getting the file from the same working directory as my server.R and ui.R. Thanks
server.R
library(shiny)
college = read.csv("college.csv")
ui.R (I added to this to see if it fixes the problem, but it doesn't)
library(shiny)
college = read.csv("college.csv")
Currently I was facing a similar trouble.
Reading here and there, I realized that you can create a script called global.R in the same dir with ui.R and server.R.
On this file (global.R) you can load libraries and, in this case, objects previously saved on a dir, for example, called data.
I created the object and the saved it with saveRDS(df, "./data/df.RDS"). Then loaded it from the data dir with something like
df <- readRDS("data/df.RDS")
on the global.R
That works for me.
Best practice would be to place your data in a folder, say ~/<application name>/data and then call your data from your server.R treating your application's directory (/<application name>/) as the current working directory.
e.g. I save my files as RDS objects in ~/ImputationApp/data/ and then read them in with:
foo.rds <- readRDS("data/foo.rds")
Even though what you describe should run, double check your filepaths for the datafiles you are trying to load and any stray setwd() commands that could be mucking up the works. A common misstep is to put the fully qualified path to your data on your machine in your server.R.
I know it's too late but I believe creating a folder named www in your directory and placing the csv there should solve the problem.