Shiny reactive check if file is choose - r

Good morning. I want to check if file is choose or not in fileInput so I had to create reactive function but this not working.
ui.R
fileInput("file_input","Choose your file in csv")
mainPanel("main panel",textOutput("choose"))
server.R
library(shiny)
isFileChoose<-function(){reactive({
if(is.null(input$file_input))
return (FALSE)
else
return (TRUE)
}) }
server <- function(input, output) {
if(isFileChoose()==FALSE)
{
output$choose<-renderText("Not selected file")
}
}

I do not think you can just use a reactive in a function like that, see here. You could do this:
library(shiny)
ui <- fluidPage(
fileInput("file_input","Choose your file in csv"),
textOutput("choose")
)
server <- function(input, output) {
output$choose <- reactive({
if(is.null(input$file_input))
{
"No input given yet."
}
else
{
"Now we can process the data!"
}
})
}
shinyApp(ui = ui, server = server)
Hope this helps!

Related

Reactive monitoring of a file in Shiny

I have an app where I need to monitor a file for changes. However I am struggling to do this.
Consider the following example:
library(shiny)
A function that returns the modified date of a file:
file_info <- function(){
if(file.exists("example.txt")){
return(as.character(as.Date(file.info("example.txt")[1,4])))
} else {
return("File not present")
}
}
Shiny part:
ui <- shinyUI(fluidPage(
textOutput("file_status"),
actionButton("create_file", "Create file"),
actionButton("delete_file", "Delete file")
))
server <- shinyServer(function(input, output, session) {
file_st <- reactive(file_info()) #what is the correct approach here?
output$file_status <- renderText({
file_st()
})
observeEvent(input$create_file,{
file.create("example.txt")
})
observeEvent(input$delete_file, {
unlink("example.txt")
})
})
I would like the file_status text field to update each time the example.txt file changes - if possible even if this happens outside of the app.
I have tried various combinations of reactive, observe and reactiveValues without finding the adequate combo.
Thank you
I was able to solve this using reactivePoll which is practically tailored for this type of task
server <- shinyServer(function(input, output, session) {
file_st <- reactivePoll(500, session,
checkFunc = function() {
if (file.exists("example.txt"))
file.info("example.txt")$mtime[1]
else
""},
valueFunc = function(){
if(file.exists("example.txt")){
return(as.character(as.Date(file.info("example.txt")[1,4])))
} else {
return("File not present")
}
}
)
output$file_status <- renderText({
file_st()
})
observeEvent(input$create_file,{
file.create("example.txt")
})
observeEvent(input$delete_file, {
unlink("example.txt")
})
})
ui <- shinyUI(fluidPage(
textOutput("file_status"),
actionButton("create_file", "Create file"),
actionButton("delete_file", "Delete file")
))

Action button in Shiny app updates query in url with input from user

I have this app:
library(shiny)
ui <- fluidPage(
textInput("query_text","Type something:"),
actionButton(inputId='query_button',
label="Search",
icon = icon("th"),
onclick = paste("location.href='http://www.example.com?lookfor=",
input$query_text, "'", sep=""))
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
I'd like to update the url with the action button, so when the user types something (for example: paper), it updates the url like this:
http://www.example.com/?lookfor=paper
Any ideias how to do it? Maybe wrapping it on a observeEvent?
Based on your replies to my comment, what you're looking for is the updateQueryString function.
library(shiny)
ui <- fluidPage(
textInput("query_text", "Type something:"),
actionButton(inputId = 'query_button', label = "Search")
)
server <- function(input, output, session) {
observeEvent(input$query_button, {
updateQueryString(paste0("?lookfor=", input$query_text))
})
}
shinyApp(ui, server)

Redirect in Shiny app

I'm trying to make my Shiny app to redirect the user to another page. I'm using httr to send GET requests and see if the user is logged in. If he's not, I want to redirect him to another link.
Can I do that using R / Shiny only, or do I need some extra libraries?
sample:
library(httr)
library(shiny)
shinyServer(function(input, output) {
rv <- reactiveValues()
rv$mytoken = session$request$token
observeEvent(input$button1, {
rv$a <- GET("my.url:3405/authtoken",
add_headers(
.headers = c("token" = rv$mytoken)
))
if (rv$a$status_code == 200) {
} else {
# redirect magic
}
})
}
shinyUI(fluidPage(
actionButton(button1, "btn")
))
Here this will navigate you to google if not true
library(shiny)
jscode <- "Shiny.addCustomMessageHandler('mymessage', function(message) {window.location = 'http://www.google.com';});"
ui <- fluidPage(
tags$head(tags$script(jscode)),
checkboxInput("Redirect","Redirect",value = T)
)
server <- function(input, output, session) {
observeEvent(input$Redirect,{
if(!input$Redirect){
session$sendCustomMessage("mymessage", "mymessage")
}
})
}
shinyApp(ui,server)
Just to update. There is also an easier way...
shinyjs::runjs(paste0('window.location.href = "...";'))
Don't forget useShinyjs() in UI.

How to capture shinyalert input field as variable

I'm trying to use the relatively new shinyAlert package to see if it offers better results than the sweetalert package but I'm unable to figure out how to get this:
Myvar <- shinyalert input text
from this minimal example.
library(shiny)
library(shinyjs)
library(shinyalert)
ui <- fluidPage(
shinyjs::useShinyjs(),
useShinyalert(),
actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {
shinyEnv <- environment()
observeEvent(input$run, {
shinyalert('hello', type='input')
})
}
shinyApp(ui = ui, server = server)
My thanks for any help from you geniouses out there.
Here is how you do it:
library(shiny)
library(shinyalert)
ui <- fluidPage(
useShinyalert(),
actionButton("run", "Run", class = "btn-success")
)
server <- function(input, output, session) {
observeEvent(input$run, {
shinyalert('hello', type='input', callbackR = mycallback)
})
mycallback <- function(value) {
cat(value)
}
}
shinyApp(ui = ui, server = server)
It's done using callbacks. You can assign the value to a reactive variable if you'd like.
I had fully documented the package last month and was about to release it, and then my computer crashed before I had a chance to push to github, and lost all progress. I haven't had a chance to work on it again. So sorry that the documentation isn't great yet, the package is still un-released so use at your own risk for now :)
(Notice that you don't need shinyjs for this)
I have no experience with the package shinyalert, but you can achieve what you want with the widely used and well documented modal dialogs from shiny. Maybe you there is a reason for you to stick with shinyalert that I am unaware off, but if not, example code for achieving what you want with modal dialogs:
ui <- fluidPage(
shinyjs::useShinyjs(),
actionButton("run", "Run", class = "btn-success"),
textOutput("output1")
)
server <- function(input, output, session) {
dataModal <- function(failed = FALSE) {
modalDialog(
textInput("input1", "Enter text:",
placeholder = 'Enter text here'
)
)
}
# Show modal when button is clicked.
observeEvent(input$run, {
showModal(dataModal())
})
output$output1 <- renderText({
input$input1
})
}
shinyApp(ui = ui, server = server)
Let me know if this helps!

updateTabsetPanel now, without delay

In the following minimal example, note that while the file.choose() line is commented out, the code works as expected: clicking the OK button on the Input tab switches to the Results tab.
However, uncomment the file.choose() line and the updateTabsetPanel() function no longer works. Is there some way to get the tab to change on the screen before the file.choose() begins?
ui <- fluidPage(fluidRow(
tabsetPanel(id="main",
tabPanel("Input", actionButton("okButton", "OK")),
tabPanel("Results", h3(textOutput("results")))
)))
server <-function(input, output, session) {
output$results = renderText("Results Tab")
observeEvent(input$okButton, {
updateTabsetPanel(session, "main", "Results")
# file.choose()
})
}
shinyApp(ui = ui, server = server)
You can do it in two steps: 1. Switch the tab; 2. Based on the tab switching event, open the file choose window.
ui <- fluidPage(fluidRow(
tabsetPanel(id="main",
tabPanel("Input", actionButton("okButton", "OK")),
tabPanel("Results", h3(textOutput("results")))
)))
server <-function(input, output, session) {
output$results = renderText("Results Tab")
observeEvent(input$okButton, {
updateTabsetPanel(session, "main", "Results")
})
observeEvent(input$main, {
if (input$main == "Results") {
file.choose()
}
})
}
shinyApp(ui = ui, server = server)

Resources