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

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)

Related

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")

Is there an R function for creating a dataframe in R shiny

I tried to create a dataframe in sidebarlayout. But not to create with this code
library(shiny)
ui <- fluidPage(
sidebarLayout(
data.frame(a=(1,2),b=c(3,4)),
mainPanel(h6("This is my dataframe"))
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Does this help you?:
ui <- fluidPage(sidebarPanel(tableOutput('tab')),mainPanel())
server <- function(input, output, session) {output$tab=renderTable(data.frame(a=c(1,2),b=c(3,4)))}
shinyApp(ui, server)
It creates the data frame in the server- in the renderTable command rather than in the ui.

How to render something first in shiny before excuting the rest of code?

I want to render a text to notify the user that a task is going to run, but it seems that shiny executes all code in server first then it moves to UI.
Here is an example:
library(shiny)
ui <- fluidPage(
mainPanel(
textOutput("ptext")
))
server <- function(input, output) {
output$ptext <- renderText("creating a dataframe")
df <- matrix(rnorm(10000),nrow = 10) # a large dataset
output$ptext <- renderText("dataframe created !!")
}
shinyApp(ui = ui, server = server)
In the above example, I never see "creating a dataframe", How to render that text first before executing the rest of the code.
It's not the most beautiful, but if you can use an input for status messages like this, you can relay what's going on ...
library(shiny)
ui <- fluidPage(
mainPanel(
textInput("notice", "Status", "creating a dataframe"),
textOutput("ptext")
)
)
server <- function(input, output, session) {
dat <- reactive({
Sys.sleep(3)
matrix(rnorm(10000), nrow = 10)
})
output$ptext <- renderText({
req(dat())
updateTextInput(session, "notice", value = "dataframe created !!")
return("hello world")
})
}
shinyApp(ui = ui, server = server)
(Note the addition of session to the arguments to server, necessary to use updateTextInput(session, ...).)
You could get more complex by using dynamic UI creation and deletion, or object hiding (perhaps using shinyjs), but that is getting a bit more complex than I think you may want.

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)

Can I generate several uiOutput-s from the same R Shiny module?

Here is my code in R Shiny using modules.
I created a module named MyModule and want to generate two UI elements: selectInput and textInput. This code is just an example - in my real application second element require the result from the first element, so I want to generate them separately.
I don't understand why the second uiOutput doesn't generate the UI element it indended to:
library(shiny)
# Define UI
ui <- shinyUI(fluidPage(MyModuleUI("one")))
# Define server logic
server <- shinyServer(function(input, output, session) {callModule(MyModule, 'one')})
#Here is my UI Module
MyModuleUI <- function(id) {
ns <- NS(id)
fluidRow(
uiOutput(ns('ChooseNumber')),
uiOutput(ns('EnterText'))
)
}
#Here is my server Module
MyModule <- function(input, output, session) {
output$ChooseNumber <- renderUI({
# In my bigger program I need this UI to be generated with some database values,
# thats why it is in the Server part of the Module
ns <- session$ns
selectInput(ns("TheNumber"), label = 'Select a number', c(1,2,3))
})
# Same here
output$EnterText <- renderUI({
ns <- session$ns
textInput(ns('TheText'),label = 'Enter a text:',value = 'ABC')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Thank you!

Resources