How to set reactive value to default in R Shiny? - r

I have a problem with updating a reactive value in shiny.
So what my app basically does, is to save textInputs from the user.
When the user decides to upload all text inputs, I want to reset the textInputs.
Following example code:
ui.R
ui <- fluidPage(
sidebarPanel(
textInput("words", "Please enter a word"),
actionButton("submit_new_word", "Save"), # this submits each single word
textOutput("submitted_new_words"), # this show all submitted words
actionButton("submit_upload", "Upload my Results") # this uploads all submitted words
)
)
server.R
server <- function(input, output, session) {
words_submitted <- paste("") # initial value
w_submitted <- eventReactive(input$submit_new_word, {
words_submitted <- paste(words_submitted, " ", input$words)
words_submitted <<- words_submitted
updateTextInput(session,
inputId = "words",
value = "")
return(words_submitted)
}, ignoreNULL=FALSE)
output$submitted_new_words <- renderText({
w_submitted()
})
observeEvent(input$submit_upload, {
# saveData(data_final) # upload, not needed for example here
words_submitted <<- paste("")
})
}
If you try this minimal example, you will see that the text inputs will be resetted,
but only after the "Save" button is clicked again.
I however would like to have the text inputs to be resetted when the "submit_upload" button is clicked.
Does somebody have an idea?

You probably best to do it with some sort of reactive. The way shiny works is that if there is no reactivity attached to it wont invalidate (refresh) anything on the client side such as renderText
library(shiny)
ui <- fluidPage(
sidebarPanel(
textInput("words", "Please enter a word"),
actionButton("submit_new_word", "Save"), # this submits each single word
textOutput("submitted_new_words"), # this show all submitted words
actionButton("submit_upload", "Upload my Results") # this uploads all submitted words
)
)
v <- reactiveValues()
server <- function(input, output, session) {
v$words_submitted <- paste("") # initial value
observeEvent(input$submit_new_word, {
v$words_submitted <- paste(v$words_submitted, " ", input$words)
updateTextInput(session, inputId = "words",value = "")
}, ignoreNULL=FALSE)
output$submitted_new_words <- renderText({
v$words_submitted
})
observeEvent(input$submit_upload, {
# saveData(data_final) # upload, not needed for example here
v$words_submitted <- paste("")
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

R Shiny won't output a variable using textOutput

I'm trying to write a calculator using Shiny in R for a video game (you input the stats of you and your opponent, and it outputs your odds of winning a match). However, I can't get the Shiny app to output any of my variables. The app runs fine, but nothing outputs when the action button is selected.
Trying to find the issue, I simplified my code into a basic calculator that takes a numeric input, multiplies it by two, and outputs a result. As before, nothing is displayed when the action button is pushed. However, if you directly type a string into the renderText function, it works just fine.
I need to include an action button in my ultimate code because I don't want it to calculate the result until several numerical values have been typed in. Could the action button be causing an issue somewhere, or is it something else?
Below is the simplified code. If I can get this to run, I'm sure I could get my more complicated code to run. Thank you!
library(shiny)
library(shinythemes)
ui <- fluidPage(
titlePanel("Multiply by 2"),
fluidRow(
column(12, textOutput("test"),
numericInput(inputId = "start", "Start", value = 1),
actionButton("go", "Go!") )
)
)
server <- function(input, output) {
myval <- reactiveValues()
observeEvent(input$go, {
reactive ({
if (input$go == 0)
return()
isolate({
myval$calc <- paste("The result is", 2*input$start)
})
})
})
output$test <- renderText({
if (input$go == 0)
return()
isolate({
myval$calc
})
})
}
shinyApp(ui = ui, server = server)
It looks like there is some extra code in there we don't need, for example the isolate function. See the below minimal example:
input$go doesn't tell us what the button is doing. Try running print(input$go) and have a look at the output.
library(shiny)
ui <- fluidPage(
titlePanel("Multiply by 2"),
fluidRow(
column(12,
textOutput("test"),
numericInput(inputId = "start", "Start", value = 1),
actionButton("go", "Go!")
)
)
)
server <- function(input, output) {
myval <- reactiveValues()
#Observe button (will run when the button is clicked)
observeEvent(input$go, {
myval$calc <- paste("The result is", 2 * input$start)
})
#Text output (will run when myval$calc changes)
output$test <- renderText({
myval$calc
})
}
shinyApp(ui = ui, server = server)

Capturing select input in R Shiny?

I am trying to capture the url that has been selected when a person presses the "GET URLS" button on the app.
What should happen is that the event reactive() should look at the input$go_button and see that it has been pressed - it should then perform the expression to take the chosen url from the select input- unfortunately it does nothing.
I have tried debugging with browser() but still had no affect.
All i am trying to do is capture the url that has been selected when a person "presses" the "GET URLS" button.
my sample code is below:
library(shiny)
# Use a fluid Bootstrap layout
ui <- fluidPage(
# Give the page a title
titlePanel("testing select"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("url_selection", "select url:",
choices = c(
'/multi-task/',
"/personal-account",
"/paperless"
)
),
actionButton(inputId = "go_button", label = "Get URLS")
),
# Create a spot for the barplot
mainPanel(
textOutput(outputId = "urls_selected_print")
)
)
)
server <- function(input, output) {
url_capture <- reactive({eventReactive(eventExpr = input$go_button,
valueExpr = {
message("capturing url chosen in selectize input")
chosen_url <- input$url_selection
browser()
return(chosen_url)
})
})
}
shinyApp(ui, server)
You can use observeEvent to capture event when go_button has been pressed. You can store the selection in a reactiveVal which can be displayed in your output.
server <- function(input, output) {
rv <- reactiveVal(NULL)
observeEvent(input$go_button, {
message("capturing url chosen in selectize input")
rv(input$url_selection)
})
output$urls_selected_print <- renderText({rv()})
}
Small alternative to Ben's answer (you just have to remove reactive, because eventReactive is already reactive):
server <- function(input, output) {
url_capture <- eventReactive(eventExpr = input$go_button,
valueExpr = {
message("capturing url chosen in selectize input")
chosen_url <- input$url_selection
return(chosen_url)
})
output$urls_selected_print <- renderPrint({
url_capture()
})
}

r shiny: How to print a message in the app after the user forgets to upload a file?

I am building a rudimentary shiny app.
First, I created a data frame 'x' and saved it in my working directory:
x <- data.frame(a = 1:4, b = 2:5)
write.csv(x, 'x.csv', row.names = F)
In my shiny I'd like to:
Upload file 'x.csv'
Click my action button 'Click Here' and run a few commands upon clicking it.
Get a message printed in the Shiny app itself: "Load a file!" if I click on my button "Click here" after forgetting to upload the file first.
My code works, but I can't figure out how to make my message appear.
My code:
library(shiny)
ui <- fluidPage(
br(),
# User should upload file x here:
fileInput("file_x", label = h5("Upload file 'x'!")),
br(),
# Users clicks the button:
actionButton("do_it", "Click Here"),
br(),
# Print last value of the button 'do_it':
verbatimTextOutput("print_action")
)
server <- function(input, output, session) {
observeEvent(input$do_it, {
# Just a check of my button's actions:
output$print_action <- renderPrint({input$do_it})
# Validating the input - next 5 lines are not working:
# validate(
# need(
# try(is.null(input$file_x), "Load a file!")
# )
# )
# Reading in the file:
fileData <- reactive({
infile <- input$file_x
if (is.null(infile)) {
return(NULL)
}
read.csv(infile$datapath)
})
x <- fileData()
# Writing out the same file - but under a different name:
filename <- paste0("x", input$do_it, ".csv")
write.csv(x, file = filename, row.names = FALSE)
})
}
shinyApp(ui, server)
I think rather than displaying text, maybe modalDialog is better suited for what you are trying to achieve. I have implemented both solutions below, so you can compare.
Note that I also modified the reading of the csv slightly. It is bad practice to set a reactive from inside an observer. In those cases, it is better to use a reactiveVal, and update that from an observer.
Hope this helps!
library(shiny)
ui <- fluidPage(
br(),
# User should upload file x here:
fileInput("file_x", label = h5("Upload file 'x'!")),
br(),
# Users clicks the button:
actionButton("do_it", "Click Here"),
br(),
br(),
# Print last value of the button 'do_it':
verbatimTextOutput("print_action")
)
server <- function(input, output, session) {
observeEvent(input$do_it, {
if(is.null(input$file_x))
{
# show pop-up ...
showModal(modalDialog(
title = "Oh no!",
paste0("You have not uploaded a file, silly person!"),
easyClose = TRUE,
footer = NULL
))
# ... or update the text
my_text('Please upload a file.')
}
else
{
# Reading in the file:
infile <- input$file_x
if (is.null(infile)) {
return(NULL)
}
x <- read.csv(infile$datapath)
fileData(x) # set the reactiveVal called fileData to the file inputs.
# Writing out the same file - but under a different name:
filename <- paste0("x", input$do_it, ".csv")
write.csv(x, file = filename, row.names = FALSE)
my_text('Succes!')
}
})
fileData <- reactiveVal()
my_text <- reactiveVal('')
output$print_action <- renderText({my_text()})
}
shinyApp(ui, server)

Unexpected behavior of req()

I experienced an unexpected behavior of my R shiny code and I'm asking myself if this is bug or if I don't understand how req() works.
I have an app where the user first uploads a *.txt file containing data with a location id, a date, and other data. Then the user has to choose two numerical values. The data is checked for NAs in the col date. If there are no NAs a text should appear telling the user everything is fine.
Below are two versions of my code. In output$txt <- renderText({ I use req() to test if all inputs are set and the file is loaded.
The differences between the two codes are the ordering of the last two arguments in req. Whereas in the first version, the green text appears already when both numeric inputs are set even the file is not uploaded, the second code behaves as expected; the user has to choose the numeric value and has to choose a file before the green bar with text appears.
Why makes the ordering of the arguments in req() a difference?
Code 1:
library(shiny)
library(lubridate)
# UI
ui <- fluidPage(
fluidRow(
column(4,
wellPanel(
fileInput("file1", label = h4("file upload")),
numericInput("in1", label = h4("first input"),value = 2.5, step=0.5),
numericInput("in2", label = h4("second input"),value = NULL, step=0.5)
)
),
column(8,
h4(textOutput("txt"), tags$style(type="text/css", "#txt {vertical-align:top;align:center;color:#000000;background-color: #4cdc4c;}")),
h2(""),
textOutput("fileinfo"),
tableOutput("tab_data")
)
)
)
# SERVER
server <- function(input, output) {
output$txt <- renderText({
req(input$in1, input$in2, fl_data(), input$file1)
"your data is ok and you have chosen input 1 and 2"
})
fl_data <- reactive({
validate(
need(input$file1 != "", "upload data and choose input 1 and 2...")
)
inFile <- input$file1
if (is.null(inFile)) {
return(NULL)
} else {
dd <- read.table(inFile$datapath, sep=";", stringsAsFactors=FALSE, header=TRUE)
dd[,2] <- ymd(dd[,2])
if (sum(is.na(dd[,2]))>0) dd <- NULL
}
})
output$tab_data <- renderTable({head(fl_data()[,1:4])})
output$fileinfo <- renderPrint({input$file1})
}
# Run the application
shinyApp(ui = ui, server = server)
Code 2:
library(shiny)
library(lubridate)
# UI
ui <- fluidPage(
fluidRow(
column(4,
wellPanel(
fileInput("file1", label = h4("file upload")),
numericInput("in1", label = h4("first input"),value = 2.5, step=0.5),
numericInput("in2", label = h4("second input"),value = NULL, step=0.5)
)
),
column(8,
h4(textOutput("txt"), tags$style(type="text/css", "#txt {vertical-align:top;align:center;color:#000000;background-color: #4cdc4c;}")),
h2(""),
textOutput("fileinfo"),
tableOutput("tab_data")
)
)
)
# SERVER
server <- function(input, output) {
output$txt <- renderText({
req(input$in1, input$in2, input$file1, fl_data())
"your data is ok and you have chosen input 1 and 2"
})
fl_data <- reactive({
validate(
need(input$file1 != "", "upload data and choose input 1 and 2...")
)
inFile <- input$file1
if (is.null(inFile)) {
return(NULL)
} else {
dd <- read.table(inFile$datapath, sep=";", stringsAsFactors=FALSE, header=TRUE)
dd[,2] <- ymd(dd[,2])
if (sum(is.na(dd[,2]))>0) dd <- NULL
}
})
output$tab_data <- renderTable({head(fl_data()[,1:4])})
output$fileinfo <- renderPrint({input$file1})
}
# Run the application
shinyApp(ui = ui, server = server)
req short-circuits, just like the && and || operators. As soon as it comes across an unavailable value (args evaluated left-to-right), it stops the reactive chain and doesn't care about any further args.
In the second example, input$file1 prevents fl_data() from ever executing if missing, so the validation in fl_data never occurs. But rather than order the req args like in the first example, I would just remove the check for input$file1 in output$txt, as it's already being checked in fl_data.

Notifying dependent functions by using reactive values in Shiny

I understand that reactive values notifies any reactive functions that depend on that value as per the description here
based on this I wanted to make use of this property and create a for loop that assigns different values to my reactive values object, and in turn I am expecting another reactive function to re-execute itself as the reactive values are changing inside the for loop. Below is a simplified example of what i am trying to do:
This is the ui.R
library(shiny)
# Define UI
shinyUI(pageWithSidebar(
titlePanel("" ,"For loop with reactive values"),
# Application title
headerPanel(h5(textOutput("Dummy Example"))),
sidebarLayout(
#Sidebar
sidebarPanel(
textInput("URLtext", "Enter csv of urls", value = "", width = NULL, placeholder = "Input csv here"),
br()
),
# Main Panel
mainPanel(
h3(textOutput("caption"))
)
)
))
This is the server file:
library(shiny)
shinyServer(function(input, output) {
values = reactiveValues(a = character())
reactive({
url_df = read.table(input$URLtext)
for (i in 1:5){
values$a = as.character(url_df[i,1])
Sys.sleep(1)
}
})
output$caption <- renderText(values$a)
})
This does not give the expected result. Actually when I checked the content of values$a
it was null. Please help!
Rather than using a for loop, try using invalidateLater() with a step counter. Here's a working example that runs for me with an example csv found with a quick google search (first column is row index 1-100).
library(shiny)
# OP's ui code
ui <- pageWithSidebar(
titlePanel("" ,"For loop with reactive values"),
headerPanel(h5(textOutput("Dummy Example"))),
sidebarLayout(
sidebarPanel(
textInput("URLtext", "Enter csv of urls", value = "", width = NULL, placeholder = "Input csv here"),
br()
),
mainPanel(
h3(textOutput("caption"))
)
)
)
server <- function(input, output, session) {
# Index to count to count through rows
values = reactiveValues(idx = 0)
# Create a reactive data_frame to read in data from URL
url_df <- reactive({
url_df <- read.csv(input$URLtext)
})
# Reset counter (and url_df above) if the URL changes
observeEvent(input$URLtext, {values$idx = 0})
# Render output
output$caption <- renderText({
# If we have an input$URLtext
if (nchar(req(input$URLtext)) > 5) {
# Issue invalidation command and step values$idx
if (isolate(values$idx < nrow(url_df()))) {
invalidateLater(0, session)
isolate(values$idx <- values$idx + 1)
}
}
# Sleep 0.5-s, so OP can see what this is doing
Sys.sleep(0.5)
# Return row values$idx of column 1 of url_df
as.character(url_df()[values$idx, 1])
})
}
shinyApp(ui = ui, server = server)

Resources