R get value from selectInput updated - r

I have a selectInpunt updated, and with this last value, I want to get information from a dataframe, but I can't get the value of the last selectInput, I have just the result "character(0)". The dataframe open but I can't get the value corresponding to input...The values of the first selectInput are the names of different data.frames. I can get the data.frame, but I can not extract the information corresponding to the input of the second selectInput.
library(shiny)
liste<-c("BCE","FED")
ui<-tagList(
navbarPage(
"Evolutions Economiques",
tabPanel("Observation",
# Application title
titlePanel("Evolutions Economiques"),
# Sidebar with a slider input for number of bins
#sidebarLayout(
sidebarPanel(
h1("Selection des donnees"),
selectInput("Source","Source :",
choices =liste),
selectInput("indic","Indicateur :",
choices = NULL)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type="tabs",
tabPanel("Description",verbatimTextOutput("summary"),
)
))
)
))
library(shiny)
library(dplyr)
BCE<-data.frame(Indice=c("A","B"),Description=c("Alors","Pouipoui"))
FED<-data.frame(Indice=c("C","D"),Description=c("So","Birdyy"))
# Define server logic required to draw a histogram
shinyServer(function(input, output,session) {
observeEvent(input$Source,{
data<-get(input$Source)
updateSelectInput(session,"indic",
choices = data$Indice)
})
output$summary<-renderPrint({
data<-get(input$Source)
data<-filter(data,Indice==input$indic)
data<-data$Description
data
})
})

Related

No output in shiny

I try to print out text in information section, to plot graphs separately for cases and deaths of cover 19 and make a summary table for each month in summary section.
But now, only sidebar shows, text and graphs don't show up at all.
I guess there are mistakes in server.R
But,I check code several times and still have no idea what the problem is. Does anyone can help?
ui.R
library(shiny)
library(ggplot2)
library(dplyr)
data<-read.csv('https://raw.githubusercontent.com/lingling1123/covid-19-data/master/us.csv')
data$date<-as.numeric(as.Date(data$date,origin='2020-01-21'))-18281
data$cases<-data$cases/1000
data$deaths<-data$deaths/1000
Jan<-c(1:11)
Feb<-c(12:40)
Mar<-c(41:71)
Apr<-c(72:101)
May<-c(102:132)
Jun<-c(133:162)
Jul<-c(163:193)
Aug<-c(194:224)
Sep<-c(225:254)
Oct<-c(255:285)
Nov<-c(286:298)
# Define UI for application that draws a histogram
shinyUI(navbarPage('All you can know',
# Information
tabPanel('Information',
mainPanel(
textOutput("info"),
)
),
# Summaries
tabPanel('Summaries',
sidebarLayout(
sidebarPanel(
h4('You can create plots using the radio buttons below.'),
radioButtons('var',h5('Select the variable '),choices=list('cases','deaths')),
h4('You can create numerical summaries below.'),
selectizeInput("mon", h5('Select the month you want to check'),selected = "Jan", choices = c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct'))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
tableOutput("table")
)
)
)))
server.R
shinyServer(function(input,output,session) {
output$info <- renderPrint({
print('For this project, I use Covid19 dataset that incluses date, cases and deaths to make a exposure notification app.')
})
value<-reactive({
variables<-input$var
})
output$distPlot <- renderPlot({
if(value()=='cases'){
ggplot(data,aes(x=date,y=cases))+geom_point()
}else{
ggplot(data,aes(x=date,y=deaths))+geom_point()
}
}
)
}
)
Move the data load section from your ui.r to server.r, e.g.
ui.r
library(shiny)
library(ggplot2)
library(dplyr)
# Define UI for application that draws a histogram
shinyUI(navbarPage('All you can know',
# Information
tabPanel('Information',
mainPanel(
textOutput("info"),
)
),
# Summaries
tabPanel('Summaries',
sidebarLayout(
sidebarPanel(
h4('You can create plots using the radio buttons below.'),
radioButtons('var',h5('Select the variable '),choices=list('cases','deaths')),
h4('You can create numerical summaries below.'),
selectizeInput("mon", h5('Select the month you want to check'),selected = "Jan", choices = c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct'))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
tableOutput("table")
)
)
)))
server.r
data<-read.csv('https://raw.githubusercontent.com/lingling1123/covid-19-data/master/us.csv')
data$date<-as.numeric(as.Date(data$date,origin='2020-01-21'))-18281
data$cases<-data$cases/1000
data$deaths<-data$deaths/1000
Jan<-c(1:11)
Feb<-c(12:40)
Mar<-c(41:71)
Apr<-c(72:101)
May<-c(102:132)
Jun<-c(133:162)
Jul<-c(163:193)
Aug<-c(194:224)
Sep<-c(225:254)
Oct<-c(255:285)
Nov<-c(286:298)
shinyServer(function(input,output,session) {
output$info <- renderPrint({
print('For this project, I use Covid19 dataset that incluses date, cases and deaths to make a exposure notification app.')
})
value<-reactive({
variables<-input$var
})
output$distPlot <- renderPlot({
if(value()=='cases'){
ggplot(data,aes(x=date,y=cases))+geom_point()
}else{
ggplot(data,aes(x=date,y=deaths))+geom_point()
}
}
)
}
)

R reactive shiny with an updateSelectInput

I make an updateSelectInput on shiny, it's working. But after I can't use the new input as a variable for an output... The input is always empty. I give you the code for the SelectInput in Ui.R and the update in server.R. I can't give more, because the updating is made via an access database. And if I create data.frame just for the example, it will work...
selectInput("indic","Indicateur :",
choices = NULL,selected = NULL),
observeEvent(input$Source,{
indicateurs<-as.character(voila_source(input$Source)$Indice)
updateSelectInput(session,"indic",
choices = indicateurs)
})
output$summary<-renderTable({
information<-voila_source(input$Source)
information<-information[,-1]
indica<-input$indic ##here is empty...
print(indica)
description<-filter(information,Indice==indica)
description
})
Maybe I forgot something, I don't know. I want select an input and print a data.frame corresponding at the input selected.
EDIT : Answer found
Ok my code and your code work... It have to push on the submitbutton... But I don't want to push on submitbutton for that, I want just to click on selectInput to print my output, that is a description of the selectInput, and if I want this one, I push on the button to display a graph.
I found the error, the submitbutton, I replaced by actionbutton and it's working... I was not aware about the submitbutton and actionbutton.
If it could help you, This is my code for call the access database and all the server.R code and ui.R code :
library(shiny)
library(anytime)
library(plotly)
library(ggplot2)
library(dplyr)
library(RODBC)
library(ecb)
channel<-odbcConnectAccess("H:\\Analyse Macro\\Base Macro live.mdb")
listee<-sqlQuery(channel,paste("Select * from Liste_source"))
liste_server<-list()
for (i in 1:length(listee$Table)){
liste_server[i]<-as.character(listee$Table[i])
}
names(liste_server)<-as.character(listee$Table)
for (i in 1:length(listee$Table)){
liste_server[[i]]<-sqlQuery(channel,paste("Select * from ",liste_server[i]))
}
voila_source<-function(selection){
x<-as.character(selection)
liste_donnee<-liste_server[[x]]
#liste_donnee<-as.character(liste_donnee$Indice)
liste_donnee$Indice<-as.character(liste_donnee$Indice)
liste_donnee$Description<-as.character(liste_donnee$Description)
liste_donnee$Unite<-as.character(liste_donnee$Unite)
liste_donnee$Frequence<-as.character(liste_donnee$Frequence)
liste_donnee$Code<-as.character(liste_donnee$Code)
liste_donnee$Pays<-as.character(liste_donnee$Pays)
liste_donnee
}
# Define server logic required to draw a histogram
shinyServer(function(input, output,session) {
observeEvent(input$Source,{
indicateurs<-as.character(voila_source(input$Source)$Indice)
updateSelectInput(session,"indic",
choices = indicateurs)
})
output$summary<-renderTable({
information<-voila_source(input$Source)
information<-information[,-1]
reactives$indica<-input$indic
print(reactives$indica)
description<-filter(information,Indice==reactives$indica)
description<-data.frame(test=indica)
description
})
})
ui.R
library(shiny)
#library(quantmod)
library(lubridate)
library(plotly)
library(ggplot2)
library(RODBC)
channel<-odbcConnectAccess("H:\\Analyse Macro\\Base Macro live.mdb")
liste<-sqlQuery(channel,paste("Select * from Liste_source"))
liste<-as.character(liste$Table)
# Define UI for application that draws a histogram
#shinyUI(fluidPage(
ui<-tagList(
navbarPage(
"Evolutions Economiques",
tabPanel("Observation",
# Application title
titlePanel("Evolutions Economiques"),
# Sidebar with a slider input for number of bins
#sidebarLayout(
sidebarPanel(
h1("Selection des donnees"),
selectInput("Source","Source :",
choices =liste),
selectInput("indic","Indicateur :",
choices = NULL,selected = NULL),
selectInput("pays","Pays :",
choices = NULL),
selectInput("partenaire","Partenaire :",
choices = NULL),
#### replace by actionbutton submitButton("Ajouter"),
actionButton("add","Ajouter"),
hr(),
img(src="logo.png",height=80,width=200),
br(),
br(),
helpText("Application realisee pour l'exploration des donnees macroeconomiques")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type="tabs",
tabPanel("Description",tableOutput("summary"))
#,
#plotlyOutput("graph"))
))
),
tabPanel("Extraction",
sidebarPanel(
selectizeInput("Index","Indice",c("ok")),
textInput("Nom","Nom fichier"),
actionButton("save","Sauver"),
hr(),
img(src="logo.png",height=80,width=200),
br(),
br(),
helpText("Application realisee pour l'exploration des donnees macroeconomiques")
),
mainPanel(
tabsetPanel(type="tabs",
tabPanel("liste",tableOutput("source")))
)
))
)
Judging from your example, it seems you have not initialised your indicateurs or indica variables is that correct?
If so you would need a couple of extra lines. The reason your solution (creating data.frame) works is that when you're testing your app, the variable already exists for the observeEvent or renderTable functions to act on. So simply add some lines in your script to do so before they are called.
Here is an example using reactiveValues (which would be better to work with when using a shiny app):
selectInput("indic","Indicateur :",
choices = NULL,selected = NULL),
# goes in your server.R
reactives <- reactiveValues(indicateurs = NULL, indica = NULL)
observeEvent(input$Source,{
reactives$indicateurs <-as.character(voila_source(input$Source)$Indice)
updateSelectInput(session,"indic",
choices = reactives$indicateurs)
})
output$summary<-renderTable({
information<-voila_source(input$Source)
information<-information[,-1]
reactives$indica<-input$indic ##here is empty...
print(reactives$indica)
description<-filter(information,Indice==reactives$indica)
description
})

r shiny - How to display a variable number of numeric inputs

I would like to display a variable number of inputs based on a numeric input "number of lines". As there can be potentially a large number of lines, I don't want to use a conditional panel for each line and would rather iterate with functions as below:
ui <- fluidPage(
lapply(1:3,function(iter1){
fluidRow(
column(2,
h4(paste0("line", iter1))
),
lapply(1:4,function(iter2){
column(2,
h4(paste0("Wind speed bin", iter2)),
column(6, numericInput(paste0("v", iter1,"bin",iter2,"min"),"Vmin",5)),
column(6, numericInput(paste0("v", iter1, "bin",iter2,"max"),"Vmax",6))
)
})
)
})
)
The code above allows to display 4 wind speed bins definitions per line j (numericInput: vjbin1min, vjbin1max, vjbin2min, vjbin2max, ...) on 3 lines.
Overview of sidebar layout
I would like now to define the number of bins [of lines] with a variable bin_nb [line_nb] as below:
lapply(1:bin_nb,function(iter1){...
lapply(1:line_nb,function(iter2){...
With two numeric inputs
numericInput("bin_nb","number of wind speed bins",value=4),
numericInput("line_nb","number of lines",value=3)
I have tried to get the value from server side
output$bin_nb <- renderText(input$bin_nb)
And use
lapply(1:textOutput("bin_nb"),function(iter1){...
Which is of course not working as a numeric value is requested. How could I define and use these two numeric variables bin_nb and line_nb?
Based on BigDataScientist's comment, here is the detailed solution:
ui <- fluidPage(
numericInput("line_nb","number of lines",value=3),
numericInput("bin_nb","number of wind speed bins",value=4),
uiOutput("input_panel")
)
And server side:
server <- function(input, output) {
output$input_panel <- renderUI({
lapply(1:input$line_nb,function(iter1){
fluidRow(
column(2,
h4(paste0("line", iter1))
),
lapply(1:input$bin_nb,function(iter2){
column(2,
h4(paste0("Wind speed bin", iter2)),
column(6, numericInput(paste0("v", iter1,"bin",iter2,"min"),"Vmin",5)),
column(6,numericInput(paste0("v", iter1, "bin",iter2,"max"),"Vmax",6))
)
})
)
})
})
}

Read in multiple values in R shiny textInput

Is it possible to read in multiple values in textInput in R shiny? Look at a simple example below. I want to be able to produce a sum (or another R function) on the user entering valueS. Can a user enter: 1, 2, 3, 4 and the see the sum of these numbers below?
shinyUI(fluidPage(
# Copy the line below to make a text input box
textInput("text", label = h3("Text input"), value = "Enter text..."),
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
))
shinyServer(function(input, output) {
# You can access the value of the widget with input$text, e.g.
output$value <- renderPrint({ input$text })
})

get all choices from Select input and save it as a vector

I have a select input As following:
UI.R
ui <- fluidPage(
selectInput("state", "State",choices=c("within becnhamrk","out of Benchmark")),
selectInput("Action","Actions",choices=c("choose number of wokers precisely","choose arbitrarily the amount workers","Use Experinced supplier","Use The nearest Supplier")),
fileInput('file2', 'Upload Probability Matrix',accept=c('.csv','.xlsx')),
actionButton("submit6", "Add",icon = icon("plus-circle"),class = "btn-primary")
)),
I want to get all choices of selectInput with the Id of state and save it as a separate vector.
Server.R
observe({
states<-reactive({
c(input$state)})
})
How can I save all choices of state in a vector without user selection?

Resources