Shiny - How to test fileInput() - r

I'm trying to write tests in order to check if a shiny function fileInput() is reading files correctly.
My problem is that I don't know what to write in session$setInputs() in order to grab the file from my system.
Here is an example app:
library(shiny)
ui <- fluidPage(
tagList(
fileInput("file", "Please upload a file"),
tableOutput("text")
)
)
server <- function(input, output, session){
file <- reactive({input$file})
output$text <- renderTable({
req(file())
read.csv(file()$datapath)
})
}
shinyApp(ui, server)
Now, I want to be able to use testServer() in order to set a file address and see if my app loads it correctly, but I can't figure out how to do it:
address <- "path/to/text.csv"
testServer(server, {
session$setInputs(file = address)
print(file())
})
I think it has to do with the fact that fileInput() uploads the file to a temp folder and returns to shiny a dataframe where you can get the datapath, but I'm unable to simulate this pass in order to make the test work

I have the same question as you do, I did some investigating and could not find any way of testing fileInput with testServer or testthat. The best solution that I found was testing fileInput by taking a snapshot when recording a test with recordTest() of the shinytest package.

Sorry for answering this late.
I asked the same question at rstudio's forums and got an answer here
The basics of it are setting the file's datapath as a list:
address <- "path/to/text.csv"
testServer(server, { session$setInputs(file= list(datapath = address)) })

Related

R shiny save a file path for next session

I've been making an app in shiny, and it is meant to use/edit the same file for multiple different people.
The idea is that when you open the app for the first time, it'll prompt you to select the file, and save the location of the file so you don't have to select it every time you open the program.
I can't seem to find a way to do this, only saving edits to the file. Is there a way, or would you just have to select the file every time?
EDIT: here is some simple code to give you an idea of what i've been working with
library(shiny)
setwd("~/myfiles")
ui <- fluidPage(
fileInput("user_file", "Please enter a file"),
textOutput("txt_param")
)
server <- function(input, output, session) {
file_path <- input$user_file$datapath
output$txt_param <- renderText(as.character(input$user_file$datapath))
### Store the file's path in a csv to access later ###
as.data.frame(file_path)
write.csv(file_path, "user_file_path.csv")
}
shinyApp(ui, server)

Avoid Overwriting in file.copy in R

Can I avoid overwriting in case of file.copy() in Shiny in R. I know how to save file in a particular folder. But my code is overwriting uploaded file every case. I want that copied file should be saved with different sames. Can I do this in R? If yes, then how? I am posting my code. But my effort is just uploading files with same name i.e. "0.png". Please suggest the way of uploading files with different names. Thank you
server <- function(input, output) {
observeEvent (input$uploaded_file, {
file.copy(input$uploaded_file$datapath, "www")
})
}
ui <- fluidPage(
fileInput("uploaded_file", "Upload", multiple = FALSE)
)
shinyApp(ui = ui, server = server)

display.mode = 'showcase' in shinyApp() call - no code shown

I would like to be able to use display.mode = 'showcase' in an app run with the shinyApp() function call. According to the docs I should be able to pass any arguments that go runApp() through the options argument. The showcase mode works (the window is split) but it does not show the code. What's interesting is that if I run runExample("01_hello") everything works fine. I'm using shiny 1.0.5.
Code:
library(shiny)
ui <- fluidPage(
titlePanel("Sample App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("mtcars", "iris"))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
data <- reactive({
get(input$data, 'package:datasets')
})
output$table <- renderTable({
head(data())
})
}
shinyApp(ui, server, options = list(display.mode = 'showcase'))
Output:
I was having the same issue. I had the app.R file and created the DESCRIPTION file using notepad but couldn't deploy a shinyApp with the code. Then I copied the DESCRIPTION file from shiny\examples\01_hello and noticed this:
Turns out my file had a TXT extension, so Shiny wasn't reading it as a metadata file. Once I used the correct DESCRIPTION file (which you can edit using notepad), everything worked out fine.
This is more of an addendum to Gus_est's answer, since I had the same problem and wasn't able to get it run right from there.
Create a file inside the directory your app.R-file resides in, e.g. a txt-file. Write into the file what display mode is to be used using Debian Control file format. In our case it would look like that (Title is not necessary):
Title: My App
DisplayMode: Showcase
Then rename the file DESCRIPTION without providing a file ending. Ignore the warning.
When you run the app now, it will always be in display mode "showcase", you can override this only inside the runApp()-statement. So I find the documentation to be misleading.
Check your current working directory. This problem seems to occur, if the working directory is not set to the folder with the app code.

Shiny reactive UI not running server code

I'm trying to get a Shiny reactive UI running. It is getting quite complex (in terms of lines of code) so I thought refactoring was a good idea. To put it short, this is my server code:
require(ggplot2)
require(h2o)
shinyServer(function(input, output, session) {
#stop()
localH2o <<- h2o.init(nthreads = 3) #Global variable
source("BuilderServer.R", local = TRUE)[1]
source("ReviewerServer.R", local = TRUE)[1]
# CleanupFUnctions
session$onSessionEnded(function() {
rm(list=ls())
})
})
where I assumed source with local = TRUE was just like copy-paste the content of the R files. So they contain functions of the form output$functionName <- renderUI({code}). The ui code depends on these functions, most of them are reactive, the ui code looks like this:
shinyUI(navbarPage("Metamodel",
tabPanel("Build Custom Model",
fluidRow(
column(12,align="center",
uiOutput("BuilderUpTitle")
)
),
fluidRow(
column(3,
uiOutput("BuilderAxisSelector")
)
)
)
))
In this particular case, the "BuilderUpTitle" function looks like this:
output$BuilderUpTitle <- renderUI({
inFile <- input$BuilderInputFile
if(is.null(input$BuilderInputFile)){
fileInput("BuilderInputFile", "Upload a xlsx file")
} else {
#R Stuff done here with the file
textInput("text", label = h3("Model Title"), value = "Enter text...")
}
})
I wrote the code yesterday and it was working. Today I turned on the computer again, and when launching the app, not even the dependencies from the server.R appear to load (ggplot2 and h2o). The download button from the "BuilderUpTitle" function doesn't appear at all and shiny appears to only execute the ui.R code. I set the workspace to the folder of the sourcefiles and it doesn't help. Even if I uncomment the stop() function from the server, nothing seems to change. Setting breakpoints in RStudio doesn't stop the code inside the server, so that is why I think shiny is not calling the server function. However, the code was working before and I did not modify a single file. I even copied the content of the source files to the server.R code and still they do not load. Is there something obvious that I'm missing? Thank you in advance!
Ok, once again, I found myself what the problem is, and none of the things I said would've made anyone find what was wrong. Here is the tiniest possible code that reproduces the problem:
ui.R
shinyUI(fluidPage(
fluidRow(
uiOutput("itWillLock"),
uiOutput("itWillLock")
)
))
server.R
shinyServer(function(input, output) {
output$itWillLock <- renderUI({
sliderInput("slider","Slider",min=0,max=1,value=0)
})
})
I guess R gets stuck in an infinite loop and never reaches the server.R file. Is this a bug that I should report? Or just common sense will keep people out of this trouble. Thank you!

writing wav-file in shiny app with seewave and tuneR

I have a shiny app with a number of tabPanels.
In one of these panels I want to write a time signal generated in seewave and tuneR to a www subdirectory.
If I run the commands on the R-promt, everything works, but I can't get it running in my server.R.
I tried observe, but then the input$ variables are not updated:
observeEvent(input$button,{
cat("Writing wav file")
})
eventReactive(input$button,{
"Inbutton"
pi<-4*atan(1)
s5<-synth(44100,5,input$freq1,input$amp1*2000,"sine",shape=NULL,p=0,
am=c(0,0,0),fm=c(0,0,0),harmonics=1,plot=FALSE,output="Wave")
s6<-synth(44100,5,input$freq2,input$amp2*2000,"sine",shape=NULL,
p=input$phase/180*pi,am=c(0,0,0),fm=c(0,0,0),
harmonics=1,plot=FALSE,output="Wave")
s7<-s5+s6
str(s7)
Wobj<-s7
wav_dir<-"./www"
wav_file<-file.path(wav_dir,"howling2.wav")
writeWave(Wobj,filename=wav_file)
play(s7)
})
I've managed somehow to save the file. I defined function that generates the sound and load it beforehand. Then in server function I call it like that:
server <- function(input, output){
observeEvent(input$button, {
sound <- sonify(input$name) # this is already a Wave object
wvname <- paste0("sound", input$button,".wav")
writeWave(sound, paste0("www/", wvname))
})
}
Then to implement it in UI follow this and this

Resources