R Shiny - value of input as a variable - r

I want to use value selected in input (below) in loop.
ui <- fluidPage(
#User dropbox
selectInput("state", "Choose state", choices=ListaKampani$Nazwa)
#Print table to UI
,tableOutput("table1")
ID_kamp <- reactive({input$state})
for (i in ID_kamp) {print(i)}
How I can use value from input as a variable in code?
In next attempt i tried this code:
library(shiny)
ui <- fluidPage(
selectInput("wyb", "Wybierz", c(1,2,3))
)
server <- function(input, output, session) {
wybor <- reactive(input$wyb)
for (a in 1:wybor()) {print(a)}
}
shinyApp(ui, server)
But i have error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.).
How I can make variable for loop make reactive? Reactive() function is not enough ?

Related

using function outside the shiny app which uses reactive function as an argument

I have created the following reprex which tries to use a reaction function price() outside the shiny app as an argument of the function.
When i try to input the values which come out of a function in a variable it doesn't seem to work. It says 'Error in myfunction(argument1) : object 'argument1' not found '.
I want to use the reactive values outside of the shiny app but i am not able to do that
library(shiny)
# in script.R
myfunction <- function(argument1){
return(as.numeric(argument1))
}
f<-myfunction(argument1) #Trying to put output values in a variable
# in app.R
#source("script.R")
ui <- fluidPage(
numericInput("price", "Price", value=1, min=1 , max=10)
,textOutput("text")
)
server <- function(input, output, session) {
price <- reactive(myfunction(argument1 = input$price))
output$text<- renderText(price())
}
shinyApp(ui, server)

Use reactive input into R code calculations for Shiny

I have a shiny application reading an input from ui and I am unable to follow up with the data from the input in my code. As below:
ui <- fluidPage(
...
selectInput("ISvModels", "Choose:",
choices = c(1000,5000)),
)
server <- function(input, output) {
vModels <- reactive({input$ISvModels})
qtModels <- length(vModels)
qtModels
vtModels <- paste0("M",1:qtModels," n = ",vModels," scenarios")
vtModels
}
And I get:
Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'
I tried all sort of things from observe to renders but nothing works. Seems I'm missing some concepts here, hope you can help. Thanks!
Your server needs an output, some way for what you've calculated to be shown to the user. We can use a textOutput to achieve this.
Below is a minimal example, showing a dropdown box linked to a textbox.
library(shiny)
ui <- fluidPage(
#Dropdown
selectInput("ISvModels", "Choose:", choices = c(1000,5000)),
#Textbox
textOutput("mytext")
)
server <- function(input, output, session) {
#Prepare Textbox Content
output$mytext <- renderText({
qtModels <- length(input$ISvModels)
vtModels <- paste0("M", 1:qtModels, " n = ", input$ISvModels," scenarios")
return(vtModels)
})
}
shinyApp(ui, server)

R Shiny - how to run a function with reactives just before session ends

How can I run a function that depends on reactive values just before the session ends?
If I try using 'onStop' it doesn't work, see code below. Start the app, it loads a reactive value - how can this reactive value be used when ending the session?
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
server <- function(input, output, session) {
v <- reactiveValues(user = NULL) # define the reactive value
v$user <- "test_user" # populate the reactive value
observe(cat(v$user)) # print the reactive value to console
onStop(function(){
cat(v$user) # this doesn't work - it errors when exiting the session
})
}
shinyApp(ui, server)

Making a vector reactive

I apologise in advance for not having a minimal reproducible example. I tried to make one but didn't manage to reproduce the error.
So I have 2 scripts (ui.R and server.R) in the same folder. At the very start of the server script I want to make a vector a which will fill itself with inputs from 2 selectInputs in the ui-script.
#ui
shinyUI(fluidPage(
fluidRow(
column(selectInput(inputId = "input1",
label = "This is input 1",
choices = c("bad","neutral","good"))
column(selectInput(inputId = "input2",
label = "This is input 2",
choices = c("bad","neutral","good"))
#server
shinyServer(function(input, output) {
a <- c(input$input1, input$input2)
})
b <- b[a]
The user interface part is not the problem, the server part is. When I run the app I get this error:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
So I realize I have to make this vector a somehow reactive but I fail to see how. I tried using the function reactive:
#server
shinyServer(function(input, output) {
a <- reactive({
c(input$input1, input$input2)
})
})
But off course then I get this error because a is no longer a vector but a function.
invalid subscript type 'closure'

Adding more than one graph to mainPanel in Shiny (R)

I am trying to have multiple html outputs in my shiny App but it seems like it can only show one at a time.
My UI is:
# ui.R
shinyUI(
mainPanel(
tableOutput("view"),
plotOutput("view2")
))
And my server is:
# server.R
library(googleVis)
library(RMySQL)
shinyServer(function(input, output) {
datasetInput <- reactive({
"try2" = subset(try1, idCampaign == input$inputId)
})
output$view <- renderGvis({
gvisTable(datasetInput(),options=list(width=1000, height=270, col='blue'))
})
output$view2 <- renderGvis({
gvisScatterChart(datasetInput2())
})
})
in the output to view2 you use datasetInput2() , this should be datasetInput(). Here datasetInput() just represents a dynamic version of a dataframe, you can use it in as many functions as you want, there is no need to index it.
alternatively i think you can use the tabsetPanel to divide your main page into certain parts and assign output objects to each of your tabPanel.

Resources