R-Shiny Application - r

I am trying to build a shiny application where a user can input a date through the shiny application to then to trigger an R script to leverage that date.
I was wondering how do you link the date input from shiny to be leverage in an R script?
function(input, output) {
# You can access the value of the widget with input$date, e.g.
output$value <- renderPrint({ input$date })
}

Here's a minimal working example:
library(shiny)
ui <- fluidPage(
dateRangeInput("date","Date:"),
textOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({
x <- input$date
# script using x goes here
paste("choice:",x[1],"to",x[2]) # example output
})
}
shinyApp(ui = ui, server = server)

Related

Update scrolling choice without clicking using select2input function in R shiny

I created an application to auto-complete postal addresses. The application works but the problem is that the list of choices only updates when you click to select the text. Would it be possible to update the list of choices automatically by typing the text?
Here is the code :
library(devtools)
install_github("AnalytixWare/ShinySky")
library(shinysky)
library(shiny)
library(httr)
library(jsonlite)
library(shinyjs)
GetAddress<-function(input=""){
if(input==""){
return("")
}else{
query<-paste("https://api-adresse.data.gouv.fr/search/?q=", URLencode(input),sep="")
res<-GET(query)
data<-fromJSON(rawToChar(res$content))
return(data$features$properties$label)
}
} #F
ui <- fluidPage(
select2Input("txt","Saisir votre adresse",choices = NULL,multiple=TRUE),
verbatimTextOutput("selected_txt"),
)
server <- function(input, output,session) {
# Create object for reactive values
rv <- reactiveValues(
value_store = character()
)
# When input changes -> update
observeEvent(input$txt, {
output$selected_txt <- renderText({
paste(input$txt)
})
rv$value_store <- input$txt
output$test <- renderText({
paste(rv$value_store)
})
pubs <- GetAddress(rv$value_store)
updateSelect2Input(session, 'txt', choices =pubs,label="")
})
}
shinyApp(ui = ui, server = server)

Create output spaces in R Shiny UI in server function dynamically

I want create output spaces in UI part of Shiny conditionally. If the variable in server changed, the output spaces should be defined in UI.
library(shiny)
ui <- fluidPage(
mainPanel("main panel", textOutput("ts_txt_main"))
)
server <- function(input, output) {
observe({
for (i in 1:10) {
output[[paste0("ts_txt",i)]<- renderText({ "Some_text" })
}
})
}
shinyApp(ui = ui, server = server)
I need textOutput("ts_txt_main") to be changed or extended to textOutput("ts_txt1"), textOutput("ts_txt2"), textOutput("ts_txt3"), textOutput("ts_txt4"), textOutput("ts_txt5"), textOutput("ts_txt6"), textOutput("ts_txt7"), textOutput("ts_txt8"), textOutput("ts_txt9"), textOutput("ts_txt10")

R Shiny How to select input form data frame column (reactive)

I want to selectInput from reactive data frame column as code below but it is not showed anything:
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choice=tableOutput('region'),selected=NULL)
)
server <- function(input, output, session){
data<- reactive(fread('murders.csv')) # this file contain 'region' column
output$region <- renderTable(data()$region)
}
shinyApp(ui = ui, server = server)
But when I read data outside server function (not reactive) the selectinput is working normal:
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choice=data$region,selected=NULL)
)
data<- fread('murders.csv') # this file contain 'region' column
server <- function(input, output, session){
}
shinyApp(ui = ui, server = server)
I think it is better to read file in reactive mode in server function, could you show me how to select input from data column in reactive mode ?
Unless you are planning on changing the murder csv file, there is no need for it to be reactive, and it can be a global value. If you are keen on it not being a global you can transform the ui in a function and load the data inside.
Version 1
library(shiny)
library(data.table)
ui <- function(request){
data <- fread("murders.csv")
fluidPage(
selectInput('region','Select region',choice=data$region,selected=NULL)
)
}
server <- function(input, output, session){
}
shinyApp(ui = ui, server = server)
If you really want, by some reason, to load it inside the server what you are looking for is updateSelectInput. See version below
Version 2
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choices=NULL, selected=NULL)
)
server <- function(input, output, session){
data <- fread("murders.csv")
updateSelectInput(session, "region", choices=data$region)
}
shinyApp(ui = ui, server = server)
And as I said there is no need for it to be reactive, but if you really want it to be reactive, you have to access the reactive inside a reactiveEnvironment, in this case observeEvent seems the most adequate:
Version 3
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choices=NULL, selected=NULL)
)
server <- function(input, output, session){
data <- reactive(fread("murders.csv"))
observeEvent(data(),updateSelectInput(session, "region", choices=data()$region))
}
shinyApp(ui = ui, server = server)

R Shiny: How to Save Returned Value from a Module?

I have a simple shiny module where I want to get the sum from two slider inputs:
The module codes are:
Module
custSliderGroupInput <- function(id,slider1Name,slider2Name){
ns <- NS(id)
tagList(sliderInput(ns("slider1"),slider1Name,1,100,50),
sliderInput(ns("slider2"),slider2Name,1,20,10))
}
custSliderGroup <- function(input,output,session){
rv <- reactiveVal()
observeEvent(c(input$slider1,input$slider2),{
rv <- reactive({input$slider1 + input$slider2})
print(rv())
return(list(result = rv()))
})
}
In my app.R, I want to display the result on using textOutput, but it doesn't work and no error is displayed. (the value does get printed in the console though.)
App
library(shiny)
ui <- fluidPage(
custSliderGroupInput("myslider","A","B"),
textOutput("text")
)
server <- function(input, output,session){
output$text <- renderText({
callModule(custSliderGroup,"myslider")$result
})
}
shinyApp(ui = ui, server = server)
I searched on Google and StackOverflow, but all solutions just don't work.
The message does get printed in the console:
But nothing displayed on UI:
I somehow solved it by doing this:
Module:
custSliderGroupInput <- function(id,slider1Name,slider2Name){
ns <- NS(id)
tagList(sliderInput(ns("slider1"),slider1Name,1,100,50),
sliderInput(ns("slider2"),slider2Name,1,20,10))
}
custSliderGroup <- function(input,output,session){
rv <- input$slider1 + input$slider2
return(rv)
}
App
ui <- fluidPage(
custSliderGroupInput("myslider","A","B"),
textOutput("text")
)
server <- function(input, output,session){
output$text <- renderText({
callModule(custSliderGroup,"myslider")
})
}
shinyApp(ui = ui, server = server)
I don't know why, but it seems like using functionalities such as reactive() or observeEvent() makes the module environment too complicated and does more harm than good. It just works by simplifying the codes. If anyone knows how this theoretically works or doesn't work please post your answer!
Thanks a lot!

Displaying data pulled from googlesheets in Shiny

I'm trying to access data from a Google spreadsheet and display it as a table in a Shiny app. After confirmation that the spreadsheet was accessed, the app continues to run without displaying anything. Printing the data to the console works, however.
server.R
library(shiny)
library(googlesheets)
shinyServer(function(input, output) {
sheet <- gs_title("Google Sheet")
data <- gs_read_csv(sheet)
output$table <- renderTable{
data
}
})
ui.R
library(shiny)
shinyUI(pageWithSidebar(
mainPanel(
dataTableOutput('table')
)
))
In server.Ruse renderDataTable({}) if you use dataTableOutput()
This code works:
library(shiny)
library(googlesheets)
server <- function(input, output) {
sheet <- gs_title("Google Sheet")
data <- gs_read_csv(sheet)
output$table <- renderDataTable({
data
})
}
ui <- fluidPage(sidebarLayout(sidebarPanel("Test"),
mainPanel(dataTableOutput('table'))
)
)
shinyApp(ui = ui, server = server)

Resources